Search code examples
androidandroid-imageview

ImageView onClickListener() not working


ImageView onclicklistener does not working for the following code. It shows nullPointerException in setOnClickListener.

    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); 
        } 

    });

Error log.

                    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)

Please help me to solve the issue.


Solution

  • this should solve the problem

    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); 
      } 
    });
    

    change R.drawable with R.id