0

ImageView onclicklistener 不适用于以下代码。它显示nullPointerExceptionsetOnClickListener.

    ImageView cus_image = (ImageView) findViewById(R.drawable.cust_list);
    cus_image.setOnClickListener(new View.OnClickListener() 
        {
        public void onClick(final View v) 
          {
             Intent myIntent = new Intent(v.getContext(), Customer.class);
              startActivity(myIntent); 
        } 

    });

错误日志。

                    10-16 13:15:40.167: E/AndroidRuntime(568): FATAL EXCEPTION: main
                    10-16 13:15:40.167: E/AndroidRuntime(568): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.First.namespace/com.tiara.tallyApp.New_grid_activity}: java.lang.NullPointerException
                    10-16 13:15:40.167: E/AndroidRuntime(568):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
                    10-16 13:15:40.167: E/AndroidRuntime(568):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
                    10-16 13:15:40.167: E/AndroidRuntime(568):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
                    10-16 13:15:40.167: E/AndroidRuntime(568):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
                    10-16 13:15:40.167: E/AndroidRuntime(568):  at android.os.Handler.dispatchMessage(Handler.java:99)
                    10-16 13:15:40.167: E/AndroidRuntime(568):  at android.os.Looper.loop(Looper.java:123)
                    10-16 13:15:40.167: E/AndroidRuntime(568):  at android.app.ActivityThread.main(ActivityThread.java:4627)
                    10-16 13:15:40.167: E/AndroidRuntime(568):  at java.lang.reflect.Method.invokeNative(Native Method)
                    10-16 13:15:40.167: E/AndroidRuntime(568):  at java.lang.reflect.Method.invoke(Method.java:521)
                    10-16 13:15:40.167: E/AndroidRuntime(568):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
                    10-16 13:15:40.167: E/AndroidRuntime(568):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
                    10-16 13:15:40.167: E/AndroidRuntime(568):  at dalvik.system.NativeStart.main(Native Method)
                    10-16 13:15:40.167: E/AndroidRuntime(568): Caused by: java.lang.NullPointerException
                    10-16 13:15:40.167: E/AndroidRuntime(568):  at com.tiara.tallyApp.New_grid_activity.onCreate(New_grid_activity.java:24)
                    10-16 13:15:40.167: E/AndroidRuntime(568):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
                    10-16 13:15:40.167: E/AndroidRuntime(568):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

请帮我解决问题。

4

8 回答 8

1

代替

ImageView cus_image = (ImageView) findViewById(R.drawable.cust_list);

ImageView cus_image = (ImageView) findViewById(R.id.cust_list_img_id); // cust_list_img_id will be id of that imageview in xml file.
于 2013-10-16T08:55:55.510 回答
1

失败的是:

ImageView cus_image = (ImageView) findViewById(R.drawable.cust_list);

只需查看名称“cust_list”以及“drawable”而不是“id”?

于 2013-10-16T08:56:10.497 回答
1

您正在尝试从您的 imageview 中获取参考,drawable此外您应该将 imageview id 获取为R.id.yourimageviewId.

尝试如下:

  ImageView cus_image = (ImageView) findViewById(R.id.<youriamgeid>);
于 2013-10-16T08:57:54.850 回答
1

像这样使用

ImageView cus_image = (ImageView) findViewById(R.id.cust_list);
cus_image.setOnClickListener(new View.OnClickListener() 
    {
    public void onClick(final View v) 
      {
         Intent myIntent = new Intent(getContext(), Customer.class);
          startActivity(myIntent); 
    } 

});
于 2013-10-16T08:58:45.833 回答
1

这应该可以解决问题

ImageView cus_image = (ImageView) findViewById(R.id.cust_list);
cus_image.setOnClickListener(new View.OnClickListener() 
{
public void onClick(final View v) 
  {
     Intent myIntent = new Intent(v.getContext(), Customer.class);
      startActivity(myIntent); 
  } 
});

用 R.id 改变 R.drawable

于 2013-10-16T09:00:01.253 回答
1

您在以下位置收到错误:

ImageView cus_image = (ImageView) findViewById(**R.drawable.cust_list**);

将其替换为以下内容:

ImageView cus_image = (ImageView) findViewById(**R.id.imageid**);
于 2013-10-16T09:01:16.490 回答
1

当然,请更改获取图像的方式:

ImageView cus_image = (ImageView) findViewById(R.id.cust_list_img_id);

您必须使用您在布局中设置的 id 来引用它,而不是要显示的可绘制对象。

于 2013-10-16T09:01:34.953 回答
-1

它显示空指针异常,因为 Imageview 没有得到你定义的他的 id

ImageView cus_image = (ImageView) findViewById(R.drawable.cust_list);

它应该是:

ImageView cus_image = (ImageView) findViewById(R.id.cust_list);
于 2019-09-18T06:20:15.920 回答