我正在尝试使DialogFragment包含一个警告对话框。它包含一个接口,该接口持有一个方法,该方法应该在按下对话框的取消/确定按钮时调用。
现在的问题是我想在用户点击OK / Cancel时显示Toast.makeText,但我不知道我应该向Toast传递什么Context,以免崩溃。当用户单击对话框按钮时我的应用程序崩溃,我猜上下文导致异常:
片段类:
public class AlertDFragment extends DialogFragment {
dialogListener ds;
Context con;
public interface dialogListener {
public void onOK(Context c);
public void onCancel(Context c);
}
@Override
public Dialog onCreateDialog(Bundle savedInstances) {
Builder b = new AlertDialog.Builder(getActivity());
b.setIcon(R.drawable.abc_ic_search);
b.setTitle("DialogFragment instance");
b.setMessage("Choose an option");
b.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ds.onOK(con);
}
});
b.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
ds.onCancel(con);
}
});
return b.create();
}
public AlertDFragment() {
}
protected void OnAttach(Activity activity) {
super.onAttach(activity);
con = activity;
try {
ds = (dialogListener) activity;
} catch(ClassCastException e) {
throw new ClassCastException(activity.toString()+" must implement the dialogListener interface");
}
}
}
主要活动(片段的父级):
public class MainActivity extends FragmentActivity implements AlertDFragment.dialogListener {
Button dialog2;
FragmentManager fm = getSupportFragmentManager();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dialog2 = (Button) findViewById(R.id.btn2);
dialog2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
AlertDFragment adf = new AlertDFragment();
adf.show(fm, "MY TAG");
}
});
}
//interface methods
public void onOK(Context c) {
Toast.makeText(MainActivity.this, "OK pressed", Toast.LENGTH_SHORT).show();
}
public void onCancel(Context c) {
Toast.makeText(MainActivity.this, "Cancel pressed", Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
LogCat:
11-04 10:23:30.959: E/AndroidRuntime(2014): FATAL EXCEPTION: main
11-04 10:23:30.959: E/AndroidRuntime(2014): Process: com.apex.dialogtest, PID: 2014
11-04 10:23:30.959: E/AndroidRuntime(2014): java.lang.NullPointerException
11-04 10:23:30.959: E/AndroidRuntime(2014): at com.apex.dialogtest.AlertDFragment$1.onClick(AlertDFragment.java:33)
11-04 10:23:30.959: E/AndroidRuntime(2014): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
11-04 10:23:30.959: E/AndroidRuntime(2014): at android.os.Handler.dispatchMessage(Handler.java:102)
11-04 10:23:30.959: E/AndroidRuntime(2014): at android.os.Looper.loop(Looper.java:136)
11-04 10:23:30.959: E/AndroidRuntime(2014): at android.app.ActivityThread.main(ActivityThread.java:5017)
11-04 10:23:30.959: E/AndroidRuntime(2014): at java.lang.reflect.Method.invokeNative(Native Method)
11-04 10:23:30.959: E/AndroidRuntime(2014): at java.lang.reflect.Method.invoke(Method.java:515)
11-04 10:23:30.959: E/AndroidRuntime(2014): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-04 10:23:30.959: E/AndroidRuntime(2014): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-04 10:23:30.959: E/AndroidRuntime(2014): at dalvik.system.NativeStart.main(Native Method)
提前致谢!
答案 0 :(得分:1)
ds
未初始化,因为protected void OnAttach(Activity activity)
需要小写字母o。请务必始终使用@Override注释,否则会出错。
答案 1 :(得分:0)
您在代码中有以下内容
dialogListener ds;
但你永远不会实例化它。然后,您可以在引用ds.onOK(con);
上调用ds
方法。这会导致空指针异常。