Reputation: 497
I just started to learn about Android Developing a day ago, and I was watching the youtuber Bucky. He was using onAttach(Activity activity), but well since his video is from two years ago this method deprecated, so I need need to use the onAttach(Context context). Thing is, when using Activity, the app runs just fine although when I use Context, it just crashes whenever I press the Button. How can I fix this?
Errors:
01-29 22:28:59.156 5854-5854/? E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.jruivo.mymemegenerator.TopSectionFragment.buttonClicked(TopSectionFragment.java:53)
at com.jruivo.mymemegenerator.TopSectionFragment.access$000(TopSectionFragment.java:15)
at com.jruivo.mymemegenerator.TopSectionFragment$1.onClick(TopSectionFragment.java:45)
TopSectionFragment Class:
public class TopSectionFragment extends Fragment {
private static EditText topTextInput;
private static EditText bottomTextInput;
TopSectionListener activityCommander;
public interface TopSectionListener {
public void createMeme(String top, String bottom);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
activityCommander = (TopSectionListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString());
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.top_section_fragment, container, false);
topTextInput = (EditText) view.findViewById(R.id.topTextInput);
bottomTextInput = (EditText) view.findViewById(R.id.bottomTextInput);
final Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
buttonClicked(v);
}
});
return view;
}
private void buttonClicked(View v) {
activityCommander.createMeme(topTextInput.getText().toString(), bottomTextInput.getText().toString());
}
}
Edit: changed "knap" to "button", was a mistake while copying my code to here.
Upvotes: 0
Views: 484
Reputation: 497
For some reason I swapped the imports and it's now working properly.. I don't know if this is the correct way.
From: import android.app.Fragment;
To: import android.support.v4.app.Fragment;
Upvotes: 0
Reputation: 11923
This is most likely because your Android device is not API 23+ and the version of the method you are using was added in API 23.
You either need to override the onAttach
that takes an Activity
or switch to using the support library that includes the support fragment manager and fragments. That should correctly call onAttach(Context context) even on pre API 23 devices.
You can try proving to yourself you are not reaching onAttach(Context context)
by writing something to logcat using Log.d(...)
in that method.
Upvotes: 2
Reputation: 2493
I hope your activity implements TopSectionListener
. If so, try this.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.top_section_fragment, container, false);
try {
activityCommander = (TopSectionListener) getActivity();
} catch (ClassCastException e) {
throw new ClassCastException(context.toString());
}
topTextInput = (EditText) view.findViewById(R.id.topTextInput);
bottomTextInput = (EditText) view.findViewById(R.id.bottomTextInput);
final Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
buttonClicked(v);
}
});
return view;
}
And remove these lines,
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
activityCommander = (TopSectionListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString());
}
}
Upvotes: 1
Reputation: 2098
The NullPointer is due to the knapp
variable that does not seem to be initialised - hence the exception.
Change knapp.setOnClickListener
to button.setOnClickListener
Upvotes: 1
Reputation: 1277
The mistake is knapp.set.... change it to button and functionality should be fine.
final Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
buttonClicked(v);
}
}
);
You're using the variable knapp
which is not initialized, thus a NullPointerException appears.
Upvotes: 1