Reputation: 2530
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.
Upvotes: 0
Views: 2195
Reputation: 124
it's showing null pointer exception because Imageview isn't getting his id which you have define like
ImageView cus_image = (ImageView) findViewById(R.drawable.cust_list);
it should be:
ImageView cus_image = (ImageView) findViewById(R.id.cust_list);
Upvotes: -1
Reputation: 676
You getting error at :
ImageView cus_image = (ImageView) findViewById(**R.drawable.cust_list**);
replace it with below content:
ImageView cus_image = (ImageView) findViewById(**R.id.imageid**);
Upvotes: 1
Reputation: 4339
For sure, please change the way you get your image doing this:
ImageView cus_image = (ImageView) findViewById(R.id.cust_list_img_id);
You have to reference it using the id you have set in your layout and not the drawable to be displayed.
Upvotes: 1
Reputation: 1996
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
Upvotes: 1
Reputation: 8645
Use Like This
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);
}
});
Upvotes: 1
Reputation: 23638
You are trying to get the reference of your imageview from the drawable
besides you should get the imageview id as R.id.yourimageviewId
.
Try out as below:
ImageView cus_image = (ImageView) findViewById(R.id.<youriamgeid>);
Upvotes: 1
Reputation: 22064
What fails is:
ImageView cus_image = (ImageView) findViewById(R.drawable.cust_list);
Just by looking at the name, "cust_list" and also a "drawable" instead of "id"?
Upvotes: 1
Reputation: 28823
Replace
ImageView cus_image = (ImageView) findViewById(R.drawable.cust_list);
with
ImageView cus_image = (ImageView) findViewById(R.id.cust_list_img_id); // cust_list_img_id will be id of that imageview in xml file.
Upvotes: 1