对话框中的imageView的setOnClickListener不起作用

时间:2013-12-13 05:41:52

标签: android nullpointerexception imageview onclicklistener

我在按钮单击时使用了以下方法来显示带有图像列表的对话框。如果我从图像中删除setOnclickListener,该对话框正常工作。但是,如果我将 setOnclickListener 用于图片,应用程序将会停止运行。

 private void showFrame(){
            frameDialog = new Dialog(CameraActivity.this);
            frameDialog.setContentView(R.layout.frame_selection);
            frameDialog.setTitle("Select a frame");

            ImageView thumb1 = (ImageView) findViewById(R.id.thumbView1);
            ImageView thumb2 = (ImageView) findViewById(R.id.thumbView2);
            ImageView thumb3 = (ImageView) findViewById(R.id.thumbView3);

            frameDialog.show();
            thumb1.setOnClickListener(
                     new View.OnClickListener() {
                         @Override
                         public void onClick(View v) {
                                Toast.makeText(getApplicationContext(), "to this is okay!", Toast.LENGTH_LONG).show();
                                frameDialog.dismiss();

                         }
                     }
                 );
thumb2.setOnClickListener(
                     new View.OnClickListener() {
                         @Override
                         public void onClick(View v) {
                                Toast.makeText(getApplicationContext(), "to this is okay!", Toast.LENGTH_LONG).show();
                                frameDialog.dismiss();

                         }
                     }
                 );
                thumb3.setOnClickListener(
                     new View.OnClickListener() {
                         @Override
                         public void onClick(View v) {
                                Toast.makeText(getApplicationContext(), "to this is okay!", Toast.LENGTH_LONG).show();
                                frameDialog.dismiss();

                         }
                     }
                 );


                }

logcat的

12-13 11:22:07.110: E/AndroidRuntime(17913): FATAL EXCEPTION: main
12-13 11:22:07.110: E/AndroidRuntime(17913): java.lang.NullPointerException
12-13 11:22:07.110: E/AndroidRuntime(17913):    at com.***.**.**.showFrame(CameraActivity.java:98)
12-13 11:22:07.110: E/AndroidRuntime(17913):    at com.**.**.***.access$0(CameraActivity.java:87)
12-13 11:22:07.110: E/AndroidRuntime(17913):    at com.**.**.**$3.onClick(CameraActivity.java:79)
12-13 11:22:07.110: E/AndroidRuntime(17913):    at android.view.View.performClick(View.java:4261)
12-13 11:22:07.110: E/AndroidRuntime(17913):    at android.view.View$PerformClick.run(View.java:17420)
12-13 11:22:07.110: E/AndroidRuntime(17913):    at android.os.Handler.handleCallback(Handler.java:615)
12-13 11:22:07.110: E/AndroidRuntime(17913):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-13 11:22:07.110: E/AndroidRuntime(17913):    at android.os.Looper.loop(Looper.java:137)
12-13 11:22:07.110: E/AndroidRuntime(17913):    at android.app.ActivityThread.main(ActivityThread.java:4944)
12-13 11:22:07.110: E/AndroidRuntime(17913):    at java.lang.reflect.Method.invokeNative(Native Method)
12-13 11:22:07.110: E/AndroidRuntime(17913):    at java.lang.reflect.Method.invoke(Method.java:511)
12-13 11:22:07.110: E/AndroidRuntime(17913):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
12-13 11:22:07.110: E/AndroidRuntime(17913):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
12-13 11:22:07.110: E/AndroidRuntime(17913):    at dalvik.system.NativeStart.main(Native Method)

**注意98没有线是: thumb1.setOnClickListener(........

2 个答案:

答案 0 :(得分:6)

替换

 ImageView thumb1 = (ImageView) findViewById(R.id.thumbView1);
 ImageView thumb2 = (ImageView) findViewById(R.id.thumbView2);
 ImageView thumb3 = (ImageView) findViewById(R.id.thumbView3);

 ImageView thumb1 = (ImageView) frameDialog.findViewById(R.id.thumbView1);
 ImageView thumb2 = (ImageView) frameDialog.findViewById(R.id.thumbView2);
 ImageView thumb3 = (ImageView) frameDialog.findViewById(R.id.thumbView3);

答案 1 :(得分:1)

更改此

ImageView thumb1 = (ImageView) findViewById(R.id.thumbView1);
ImageView thumb2 = (ImageView) findViewById(R.id.thumbView2);
ImageView thumb3 = (ImageView) findViewById(R.id.thumbView3);

ImageView thumb1 = (ImageView)frameDialog.findViewById(R.id.thumbView1);
ImageView thumb2 = (ImageView)frameDialog.findViewById(R.id.thumbView2);
ImageView thumb3 = (ImageView)frameDialog.findViewById(R.id.thumbView3);

findViewById查找当前膨胀布局中提到的id的视图。因此,您需要使用对话框对象来初始化视图。