-1

选择一个菜单项后,我试图显示一个自定义对话框,但是当我设置 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
4

2 回答 2

5

如果按钮yes和在对话框中,您需要设置的对话框布局中cancel搜索它们:

Button yes = (Button) alertDialog.findViewById(R.id.signout);
Button cancel = (Button) alertDialog.findViewById(R.id.cancelsignout);
于 2012-04-19T15:39:58.530 回答
0

没有 CallStack,很难说。不过,一个想法可能是您使用 findViewById 获得的小部件为空。

最好的办法是检查调用堆栈,以了解是“是”按钮还是“取消”按钮导致错误,或者最好使用断点并逐步通过代码查找缺少的内容。

于 2012-04-19T15:36:36.813 回答