Issue
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);
}
Solution
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.
Answered By - Shayan Pourvatan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.