I've looked at most of the other threads but I can't figure out what is causing my application to crash. I keep getting "attempt to invoke virtual method Android.view.View android.view.View.findViewById(int) on a null reference object. This error occurs at line 25 or
"View ObjectView = inflater.inflate(R.layout.fragment_layout_biking,container,false); "
How do you inflate a view for a newly created object in Android?
Thank you.
public class Fragment1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,
#Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
View ObjectView = inflater.inflate(R.layout.fragment_layout_biking, container, false);
final EditText input1 = (EditText)getView().findViewById(R.id.input);
final ScrollView scroll = (ScrollView)getView().findViewById(R.id.scrolling);
Button addbox = (Button)getView().findViewById(R.id.CheckBoxAdd);
addbox.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
for (int box = 0; box <25; box++){
CheckBox newcheckbox = (CheckBox)getView().findViewById(R.id.NewCheckBox);
newcheckbox.setText(input1.toString());
scroll.addView(newcheckbox);
}
}
});
return ObjectView;
}
}
Here is the xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearlayout"
android:orientation="vertical" >
<EditText
android:id="#+id/input"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</EditText>
<Button
android:id="#+id/CheckBoxAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Checkbox" />
<ScrollView
android:id="#+id/scrolling"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearlayout2"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</LinearLayout>
getView returns the View you inflated in onCreateView, before it returns null. In your code you have two options, the first is to use ObjectView.findViewById, to look for the view you need. The latter is to override onViewCreated, and use the first parameter, which in turn is the view you returned in onCreateView, to look for the view you need
Here is the final code that produces the desired result.
package fragments_for_app;
import android.app.adventureprototype.R;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
public class Fragment1 extends Fragment {
LinearLayout layout;
CheckBox newcheckbox;
//18
#Override
public View onCreateView(LayoutInflater inflater,
#Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
final View ObjectView = inflater.inflate(R.layout.fragment_layout_biking, container, false);
final EditText input1 = (EditText)ObjectView.findViewById(R.id.input);
layout = (LinearLayout)ObjectView.findViewById(R.id.linearlayout2);
//final ScrollView scroll = (ScrollView)ObjectView.findViewById(R.id.scrolling);
final Button addbox = (Button)ObjectView.findViewById(R.id.CheckBoxAdd);
layout.removeAllViews();
addbox.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
newcheckbox = new CheckBox(ObjectView.getContext());
newcheckbox.setText(input1.getText().toString());
layout.addView(newcheckbox);
//work on this thursday
}
});
return ObjectView;
}
}
Related
I am creating a app.I've used a tablayout with fragments,extended one of the fragments with ListFragment and wanted to give intent inside that fragment,i used the following...
home_fragment.java
package com.example.mymehnat;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class home_fragment extends ListFragment implements AdapterView.OnItemClickListener {
ListView lst;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.home_fragment, container, false);
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayAdapter adapter = ArrayAdapter.createFromResource(home_fragment.this.getActivity(), R.array.songs, android.R.layout.simple_list_item_1);
setListAdapter(adapter);
getListView().setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> adapter, View view,int position, long l) {
Intent intent = new Intent(home_fragment.this.getActivity(), Lyrics.class);
intent.putExtra("SongName", lst.getItemAtPosition(position).toString());
lst.getContext().startActivity(intent);
}
}
home_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background">
<ListView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#android:id/list">
</ListView>
</LinearLayout>
but whenever I click any item on the list it doesn't work,shows nothing and prompted me an error...
E/MessageQueue-JNI: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.widget.ListView.getItemAtPosition(int)' on a null object reference
at com.example.mymehnat.home_fragment.onItemClick(home_fragment.java:36) and so on
please help me.
your help is highly appreciated.
You didn't initialize the ListView lst
Try to do like this.
This need to be declared under the OnCreateView
lst = v.findViewById(R.id.list)
I want to set an onClickListener on a button, but I am facing a problem.
I can't get the context I need after the button is clicked.
I tried by view but didn't work, it doesn't work but also doesn't throw an error.
The xml file in fragment:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="go to test" />
</RelativeLayout>
Java fragment code :
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import java.util.Objects;
public class Homefragment extends android.support.v4.app.Fragment {
Button test;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
test = (Button) view.findViewById(R.id.test);
test.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("Clicked go to test");
Toast.makeText(getContext(), "clicked", Toast.LENGTH_SHORT).show();
Intent gotest;
gotest = new Intent(getActivity().getApplicationContext(),testing.class);
startActivity(gotest);
}
});
return inflater.inflate(R.layout.fragment_home,null);
}
}
My logcat gives no error and I use print command when the button is clicked but print does not work, like if the button is not clicked.
Use this return your view.
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
test = (Button) view.findViewById(R.id.test);
test.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("Clicked go to test");
Toast.makeText(getContext(), "clicked", Toast.LENGTH_SHORT).show();
Intent gotest;
gotest = new Intent(getActivity().getApplicationContext(),testing.class);
startActivity(gotest);
}
});
return view;
}
I have there sets of drawable in int array. Is it possible to change the image resource of the image view in the fragment on button click? for example i have there a method onclicklistener for button benefit_babies... i want to set the image resource into the int array benefitBaby on click of the button.
InformationTab fragment
package com.example.aldrinjohn.sample;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* Created by Aldrin John on 3/20/2017.
*/
public class InformationTab extends Fragment{
ViewPager viewPager;
CustomSwipeAdapter adapter;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.informationtab, container, false);
viewPager = (ViewPager)rootView.findViewById(R.id.view_pager);
adapter = new CustomSwipeAdapter(this.getActivity());
viewPager.setAdapter(adapter);
return rootView;
}
}
XML File:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="#+id/trivia"
android:text="BreastFeeding Trivia"/>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="#+id/benefit_babies"
android:text="Benefits to Babies"/>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="#+id/storing"
android:text="Storing Breastmilk"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="#+id/pump"
android:text="How to Pump Breastmilk"/>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="#+id/benefit_mother"
android:text="Benefits to Mothers"/>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="#+id/tips"
android:text="Tips and Information"/>
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/image_view"/>
</LinearLayout>
JAVA Code:
package com.example.aldrinjohn.sample;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.google.android.gms.tagmanager.Container;
/**
* Created by Aldrin John on 3/26/2017.
*/
public class CustomSwipeAdapter extends PagerAdapter implements View.OnClickListener{
private int[] trivia = {R.drawable.c1,R.drawable.c2,R.drawable.c3,R.drawable.c4, R.drawable.c5,R.drawable.c6};
private int[] benefitBaby = {R.drawable.c7,R.drawable.c8,R.drawable.c9};
private int[] benefitMother = {R.drawable.c91,R.drawable.c92};
private int[] info = {R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4,R.drawable.a5,R.drawable.a6,R.drawable.a7,
R.drawable.a8,R.drawable.a9,R.drawable.a91,R.drawable.a92};
private int[] storing = {R.drawable.b1,R.drawable.b2,R.drawable.b3,R.drawable.b4};
private Context ctx;
private LayoutInflater layoutInflater;
Button btnbenefitB;
public CustomSwipeAdapter(Context ctx)
{
this.ctx = ctx;
}
#Override
public int getCount() {
return trivia.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return (view==(LinearLayout)object);
}
#Override
public Object instantiateItem(ViewGroup container, int position){
layoutInflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item_view = layoutInflater.inflate(R.layout.swipe_layout,container,false);
btnbenefitB = (Button)item_view.findViewById(R.id.benefit_babies);
btnbenefitB.setOnClickListener(this);
ImageView imageView = (ImageView)item_view.findViewById(R.id.image_view);
imageView.setImageResource(trivia[position]);
container.addView(item_view);
return item_view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object){
container.removeView((LinearLayout)object);
}
public int getCount(int[] x) {
return x.length - 1;
}
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.benefit_babies:
break;
}
}
}
imageview.setImageResource(id) where id is in your int array. See the documentation for ImageView for details. It is the same as you have in your code just in the onClick() handler.
setImageResource
void setImageResource (int resId)
Sets a drawable as the content of this ImageView.
This does Bitmap reading and decoding on the UI thread, which can cause a latency hiccup. If that's a concern, consider using setImageDrawable(android.graphics.drawable.Drawable) or setImageBitmap(android.graphics.Bitmap) and BitmapFactory instead.
Hi everyone thanks for the help. I solved it by using view page adapter. Every onclick button I will change the adapter. Thank you!!
I am working on a simple app to convert the temperature. I am trying to figure out how to process a number that a user inputs and then process it and give out the conversion. I searched different places and found part of the following code but my app crashes when I actually click on the button.
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View.OnClickListener;
import org.w3c.dom.Text;
public class CelsiusFragment extends Fragment implements OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.celsiusfragment, container, false);
Button celsiusbutton = (Button) view.findViewById(R.id.button_celsius);
celsiusbutton.setOnClickListener(this);
return view;
}
Button mButton;
EditText mEdit;
TextView mText;
int output;
#Override
public void onClick(View view) {
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
int number = 0;
int number1 = number;
output = number1 + 2;
TextView result = (TextView) view.findViewById(R.id.result);
result.setText(String.valueOf(output));
}
});
}
}
Is there a tutorial that you would recommend to accomplish this?
Thanks!
Try to change your code like this:
fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:layout_gravity="center_horizontal" />
<TextView
android:id="#+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/calculate_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Calculate"
android:layout_gravity="center_horizontal" />
</LinearLayout>
CelsiusFragment.java
public class CelsiusFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container, false);
Button calculateButton = (Button) view.findViewById(R.id.calculate_button);
calculateButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TextView result = (TextView) view.findViewById(R.id.result);
EditText value = (EditText) view.findViewById(R.id.value);
int output = Integer.parseInt(value.getText().toString()) + 2;
result.setText(String.valueOf(output));
}
});
return view;
}
}
I have a big request for any of You. I would like to create a third Fragment in my simple application. Everything works fine, no error. I would like to make the last Fragment it is: "TestFragment" when I click that button inside that Fragment I'd like to refresh its View (on click button) and load another view inside this fragment (the same fragment). Please help me do this, show how to get it. I have read in Google but can not find the solution appropriate for this application.
Here is simple code:
MAIN ACTIVITY:
package com.example.viewpagerexample;
import com.example.viewpagerexample.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
public class MainActivity extends FragmentActivity {
private MyAdapter mAdapter;
private ViewPager mPager;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = new MyAdapter(getSupportFragmentManager());
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
}
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return 2;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new TestFragment();
// case 1:
// return new ImageFragment();
case 1:
return new TestFragment();
//
default:
return null;
}
}
}
}
TEST FRAGMENT:
package com.example.viewpagerexample;
import com.example.viewpagerexample.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class TestFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("Test", "hello");
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.test, container, false);
// TextView textView = (TextView) view.findViewById(R.id.detailsText);
// textView.setText("Testing");
return view;
}
}
TEST LAYOUT:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="QUIZ IMPORTANT TAKE A LOOK AT THIS!!!!!"
android:textSize="30dp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_marginLeft="30dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="asdjiasdjasoldjasjdasjdlasjdksahfkadshfalidshfladshfladsfhlasdfgjl;adkglkafdgnklfdsgjpfasmga.dsnvlkasdnvkldsnflkdasjflkasdnvlkadjvodsijfosdjv;lasdvlasdjvladosijao" />
DETAILS FRAGMENT:
package com.example.viewpagerexample;
import com.example.viewpagerexample.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class Details extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("Test", "hello");
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details, container, false);
TextView textView = (TextView) view.findViewById(R.id.detailsText);
textView.setText("Testing");
return view;
}
}
ACTIVITY_MAIN LAYOUT:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
DETAILS LYOUT:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
How to do this refresh inside the same fragment?