public class AddItemDialog extends DialogFragment implements View.OnClickListener {
Button addButton, cancelButton;
EditText showEditText, seasonEditText, episodeEditText;
Communicator communicator;
@Override
public void onAttach(Context context) {
super.onAttach(context);
communicator= (Communicator) context;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.add_item_layout, null);
addButton = (Button) view.findViewById(R.id.add_button);
cancelButton = (Button) view.findViewById(R.id.cancel_add_button);
showEditText = (EditText) view.findViewById(R.id.show_edit);
seasonEditText = (EditText) view.findViewById(R.id.season_edit);
episodeEditText = (EditText) view.findViewById(R.id.episode_edit);
addButton.setOnClickListener(this);
cancelButton.setOnClickListener(this);
setCancelable(false);
return view;
}
@Override
public void onClick(View view) {
long mseconds = System.currentTimeMillis();
if(view.getId()==R.id.add_button){
communicator.yesOnAddClick(showEditText.getText().toString(), Integer.parseInt(seasonEditText.getText().toString()),Integer.parseInt(episodeEditText.getText().toString()), mseconds);
dismiss();
} else if(view.getId()==R.id.cancel_add_button){
dismiss();
}
}
interface Communicator {
public void yesOnAddClick(String show, int season, int episode, long dateId);
}
代码适用于设备模拟器,但不适用于实际设备。发生了什么?
我认为我已正确初始化所有内容,因此它应该有效。此外,它在棉花糖仿真器上运行,但在实际设备上运行。我一直在我的平板电脑中测试它,这是Kitkat。这个版本可以在这里播放吗?
错误日志
致命例外:主要流程:com.leviebora.showandepisodetracker, PID:27412 java.lang.NullPointerException at com.leviebora.showandepisodetracker.AddItemDialog.onClick(AddItemDialog.java:49) 在android.view.View.performClick(View.java:4445)at android.view.View $ PerformClick.run(View.java:18429)at android.os.Handler.handleCallback(Handler.java:733)at android.os.Handler.dispatchMessage(Handler.java:95)at android.os.Looper.loop(Looper.java:136)at android.app.ActivityThread.main(ActivityThread.java:5013)at java.lang.reflect.Method.invokeNative(Native Method)at java.lang.reflect.Method.invoke(Method.java:515)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)at dalvik.system.NativeStart.main(原生方法)
这是错误日志。它在onclick对话框中显示nullpointerexception
答案 0 :(得分:5)
onAttach(Context)
仅在Marshmallow中添加。因此,它从未在版本中被调用过,并且communicator
从未设置过。您需要在那里使用onAttach(Activity)
。目前仅仅覆盖后者就足够了,因为前者无论如何都会调用它。
答案 1 :(得分:1)
@ tynn答案的延伸答案。
如果片段需要父活动来实现Communicator
接口,您也可以更清楚地说明这一点:
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof Communicator) {
communicator= (Communicator) activity;
} else {
throw new RuntimeException("Parent activity must implement Communicator interface");
}
}
答案 2 :(得分:1)
请使用此
@Override
public void onAttach(Context context) {
super.onAttach(context);
Activity a;
if (context instanceof Activity) {
a = (Activity) context;
communicator = (Communicator) a;
}
}