Button click throws nullpointerexception in fragment - android

Im getting an error when I press the button in my fragment view. The button is rendered but when I click it I get the following error:
09-23 18:29:01.242 9332-9332/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at foit.startingpoint.nl.androiddrawer.LocatiesFragment.onCreateView(LocatiesFragment.java:28)
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Button button = (Button) getView().findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(LocatiesFragment.this.getActivity(), SecondActivity.class);
startActivity(myIntent); }
});
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_locaties, container, false);
}

your initializing is incorrect, you must do as following code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_locaties, container, false);
Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(getActivity(),SecondActivity.class);
startActivity(myIntent);
}
});
return view;
}
as onCreateView not finished getView is null in your code so you must create one view and use that.

You cannot simply getView() from nowhere when finding button id. First layout is inflated.
View view = inflater.inflate(R.layout.fragment_locaties, container, false);
Button button = (Button) view.findViewById(R.id.button);
...
return view;

Related

imagebutton inside fragment nullPoint Exception

I am putting ImageView inside Fragment. I have other question also but cannot find useful.
this is my code:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.fragment_fragment_contect,container,false);
ImageButton imageButton = (ImageButton)getView().findViewById( R.id.ib);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity( new Intent( getActivity(), Enquiry.class ) );
}
});
return v;
}
You need to do like this in getview() you getting null pointer exception use view.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.fragment_fragment_contect,container,false);
ImageButton imageButton = v.findViewById(R.id.ib);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity( new Intent( getActivity(), Enquiry.class ) );
}
});
return v;
}
You got NullPointException because of using getview():
ImageButton imageButton = (ImageButton)getView().findViewById( R.id.ib);
Solution: use v instead of getView()
ImageButton imageButton = (ImageButton) v.findViewById( R.id.ib);
Android Fragment Referance

Intent from Fragment to a MapsActivity from onCreateView

this is my code of my third tab in which i have put a button to intent from this current fragment to my MapsActivity, but as soon as i click my button, the app stops running.
This is my code:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.fragment_tab3, container, false);
mapbtn = (Button) v.findViewById(R.id.mapButton);
mapbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), MapsActivity.class);
startActivity(intent);
}
});
return v;
}
Please someone help!!!

NullPointerException when I don't access the Button view in my fragment through rootView view

Why do I get NullPointerException when I don't access the Button view in my fragment through rootView view?
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
Button buttonClick =(Button)rootView.findViewById(R.id.button);
buttonClick.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
//Do something when button is clicked
}
});
findViewById() looks for the requested view on the content of the calling view.
So if you call it from the activity it will look for the view on the contentView you set with setContentView(), thats why its returning null.
You need to call findViewById() on the view that holds the view you want.
Hope this helps.
You are inflating to the variable rootView, so the only way to get ahold of your button is through your variable.
Declare Button in Fragment
Button buttonClick;
And then Write below code in Fragment.
buttonClick = (Button) rootView.findViewById(R.id.button);
buttonClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
you can initialize button onClick() inside the fragment
initialize like this
public class Yourclassname extends Fragment implements View.OnClickListener
{
Button button1
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootview=inflater.inflate(R.layout.postpaid, container, false);
button1 = (Button) rootview.findViewById(R.id.button);
button1.setOnClickListener(this);
return rootview;
}
public void onClick(View view)
{
//Do something when button is clicked
}
}

Adding a button listener in a fragment

I am trying to add a button listener to a fragment view in Android. When I tap the "Like" or "Dislike" buttons, nothing appears to happen. I don't get the "dislikeClicked" or "likeClicked" in my Logcat view.
Here is my code:
public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState )
{
queryForData();
View rootView = inflater.inflate(R.layout.fragment_view1, container, false );
likeButton = (Button) rootView.findViewById(R.id.view1_likeButton);
likeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d("likeClicked", "");
like();
}
});
dislikeButton = (Button) rootView.findViewById(R.id.view1_dislikeButton);
dislikeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d("dislikeClicked", "");
dislike();
}
});
return rootView;
}
Any help is appreciated.
Thanks!

Null Pointer Exception when setting new text to text view

I am trying to run an AndroidApp on my phone. I am able to show my welcome fragment, moreover I can trigger the log-message. Unfortunately I am getting a null pointer exception if I want to change the text value from 'welcome' to 'Neuer Text'. What went wrong? I am quite new to android development.
public class WelcomeFragment extends Fragment implements OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_welcome, container, false);
Button button1 = (Button) view.findViewById(R.id.button1);
button1.setOnClickListener((OnClickListener) this);
return view;
}
#Override
public void onClick(View v) {
//Log.d("JobaApp", "Logtext"); // see LogCat
TextView text1 = (TextView) v.findViewById(R.id.welcome_text);
text1.setText("NeuerText");
}
}
In the onClick(), v is the view item that was clicked, e.g. the button, not the view that is inflated in onCreateView().
You should use getView():
public class WelcomeFragment extends Fragment implements OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_welcome, container, false);
Button button1 = (Button) view.findViewById(R.id.button1);
button1.setOnClickListener((OnClickListener) this);
return view;
}
#Override
public void onClick(View v) {
//Log.d("JobaApp", "Logtext"); // see LogCat
switch (v.getId) {
case R.id.button1:
TextView text1 = (TextView) getView().findViewById(R.id.welcome_text);
text1.setText("NeuerText");
break;
}
}
}
Also, if you don't want that action taking place for every button you may want to consider using a switch statement in your onClick().
You cant get the TextView from the button's onClick passed parameter ("View v"), since this is the actual button's view.
You should do something like:
public class WelcomeFragment extends Fragment implements OnClickListener {
View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_welcome, container, false);
Button button1 = (Button) view.findViewById(R.id.button1);
button1.setOnClickListener((OnClickListener) this);
return view;
}
#Override
public void onClick(View v) {
//Log.d("JobaApp", "Logtext"); // see LogCat
TextView text1 = (TextView) view.findViewById(R.id.welcome_text);
text1.setText("NewerText"); //Also fixed typo
}
}
In OnClick(View v) , v is the button that you are clicking on. The TextView does not belong to Button, it belongs to R.layout.fragment_welcome. So you find and initialize the TextView inside onCreateView() of the fragment, and then use it inside onClick();
Some what like this:
public class WelcomeFragment extends Fragment implements OnClickListener {
TextView tv;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_welcome, container, false);
Button button1 = (Button) view.findViewById(R.id.button1);
button1.setOnClickListener((OnClickListener) this);
tv = (TextView) v.findViewById(R.id.welcome_text);
return view;
}
#Override
public void onClick(View v) {
//Log.d("JobaApp", "Logtext"); // see LogCat
tv.setText("NeuerText");
}
}

Categories

Resources