选择一个菜单项后,我试图显示一个自定义对话框,但是当我设置 onClickListener(没有 onClickListner 可以正常工作)时,会显示一个空指针错误。我检查按钮 id 拼写错误,一切都是正确的。
关于为什么的任何想法?
public void unlinkDeviceClick(MenuItem item){
final Dialog alertDiag = new Dialog(Home.this);
alertDiag.setContentView(R.layout.unlinkdialog);
alertDiag.setTitle("Unlink Device");
Button yes = (Button)findViewById(R.id.signout);
Button cancel = (Button)findViewById(R.id.cancelsignout);
final EditText usernameField = (EditText)findViewById(R.id.usernameField);
final EditText passwordField = (EditText)findViewById(R.id.passwordField);
final TextView descr = (TextView)findViewById(R.id.unlinkresults);
yes.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
String userName = usernameField.getText().toString();
String password = passwordField.getText().toString();
if(checkLoginFields(userName,password)){
if(checkCredentials(userName,password)){
//loadUnlink();
descr.setText("Sucessfully unlinked");
}
else{
showWrongInfo(new View(Home.this));
}
}
else{
}
}
});
cancel.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
alertDiag.dismiss();
}
});
alertDiag.show();
}
//////////////////////堆栈跟踪////////////////////////// ////////////
04-19 10:30:32.198: E/AndroidRuntime(20386): FATAL EXCEPTION: main
04-19 10:30:32.198: E/AndroidRuntime(20386): java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
04-19 10:30:32.198: E/AndroidRuntime(20386): at android.view.MenuInflater$InflatedOnMenuItemClickListener.onMenuItemClick(MenuInflater.java:203)
04-19 10:30:32.198: E/AndroidRuntime(20386): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:154)
04-19 10:30:32.198: E/AndroidRuntime(20386): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:964)
04-19 10:30:32.198: E/AndroidRuntime(20386): at com.android.internal.view.menu.MenuBuilder$ActionMenuAdapter$1.onClick(MenuBuilder.java:1565)
04-19 10:30:32.198: E/AndroidRuntime(20386): at android.view.View.performClick(View.java:3135)
04-19 10:30:32.198: E/AndroidRuntime(20386): at android.view.View.onKeyUp(View.java:5055)
04-19 10:30:32.198: E/AndroidRuntime(20386): at android.view.KeyEvent.dispatch(KeyEvent.java:2623)
04-19 10:30:32.198: E/AndroidRuntime(20386): at android.view.View.dispatchKeyEvent(View.java:4603)
04-19 10:30:32.198: E/AndroidRuntime(20386): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1157)
04-19 10:30:32.198: E/AndroidRuntime(20386): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1159)
04-19 10:30:32.198: E/AndroidRuntime(20386): at android.widget.ListView.dispatchKeyEvent(ListView.java:2061)
04-19 10:30:32.198: E/AndroidRuntime(20386): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1159)
04-19 10:30:32.198: E/AndroidRuntime(20386): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1159)
04-19 10:30:32.198: E/AndroidRuntime(20386): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1159)
04-19 10:30:32.198: E/AndroidRuntime(20386): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1159)
04-19 10:30:32.198: E/AndroidRuntime(20386): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1159)
04-19 10:30:32.198: E/AndroidRuntime(20386): at com.android.internal.policy.impl.tv.TvWindow$DecorView.superDispatchKeyEvent(TvWindow.java:1161)
04-19 10:30:32.198: E/AndroidRuntime(20386): at com.android.internal.policy.impl.tv.TvWindow.superDispatchKeyEvent(TvWindow.java:803)
04-19 10:30:32.198: E/AndroidRuntime(20386): at android.app.Dialog.dispatchKeyEvent(Dialog.java:706)
04-19 10:30:32.198: E/AndroidRuntime(20386): at com.android.internal.policy.impl.tv.TvWindow$DecorView.dispatchKeyEvent(TvWindow.java:1117)
04-19 10:30:32.198: E/AndroidRuntime(20386): at android.view.ViewRoot.deliverKeyEventPostIme(ViewRoot.java:2902)
04-19 10:30:32.198: E/AndroidRuntime(20386): at android.view.ViewRoot.deliverKeyEvent(ViewRoot.java:2860)
04-19 10:30:32.198: E/AndroidRuntime(20386): at android.view.ViewRoot.handleMessage(ViewRoot.java:2059)
04-19 10:30:32.198: E/AndroidRuntime(20386): at android.os.Handler.dispatchMessage(Handler.java:99)
04-19 10:30:32.198: E/AndroidRuntime(20386): at android.os.Looper.loop(Looper.java:132)
04-19 10:30:32.198: E/AndroidRuntime(20386): at android.app.ActivityThread.main(ActivityThread.java:4083)
04-19 10:30:32.198: E/AndroidRuntime(20386): at java.lang.reflect.Method.invokeNative(Native Method)
04-19 10:30:32.198: E/AndroidRuntime(20386): at java.lang.reflect.Method.invoke(Method.java:491)
04-19 10:30:32.198: E/AndroidRuntime(20386): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
04-19 10:30:32.198: E/AndroidRuntime(20386): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
04-19 10:30:32.198: E/AndroidRuntime(20386): at dalvik.system.NativeStart.main(Native Method)
04-19 10:30:32.198: E/AndroidRuntime(20386): Caused by: java.lang.reflect.InvocationTargetException
04-19 10:30:32.198: E/AndroidRuntime(20386): at java.lang.reflect.Method.invokeNative(Native Method)
04-19 10:30:32.198: E/AndroidRuntime(20386): at java.lang.reflect.Method.invoke(Method.java:491)
04-19 10:30:32.198: E/AndroidRuntime(20386): at android.view.MenuInflater$InflatedOnMenuItemClickListener.onMenuItemClick(MenuInflater.java:199)
04-19 10:30:32.198: E/AndroidRuntime(20386): ... 30 more
04-19 10:30:32.198: E/AndroidRuntime(20386): Caused by: java.lang.NullPointerException
04-19 10:30:32.198: E/AndroidRuntime(20386): at com.ameba.api.activityClasses.Home.unlinkDeviceClick(Home.java:604)
04-19 10:30:32.198: E/AndroidRuntime(20386): ... 33 more