2013-10-16 69 views
0

ImageView onclicklistener不適用於以下代碼。它顯示nullPointerExceptionsetOnClickListenerImageView onClickListener()不起作用

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) 

請幫我解決這個問題。

+0

你的imageview初始化失敗,因此你有NPE。 'cus_image'爲空 – Raghunandan

+1

@GrIsHu v.getContext()有什麼問題' – Raghunandan

+0

他不能傳遞「this」,因爲它會引用OnClickListener類。 – Carnal

回答

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.drawable與R.id

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. 
1

什麼失敗是:

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

只要看一眼名字, 「cust_list」,也是一個 「可繪」,而不是「 ID」?

1

您正試圖從drawable中獲取imageview的參考,除此之外,您應該將imageview ID設爲R.id.yourimageviewId

試試如下:

ImageView cus_image = (ImageView) findViewById(R.id.<youriamgeid>); 
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); 
    } 

}); 
1

你得到錯誤:

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

與下面的內容替換爲:

ImageView cus_image = (ImageView) findViewById(**R.id.imageid**); 
+0

請發佈您的佈局xml其中包含此圖像查看 –

+0

嘿,請接受答案,如果你覺得它有用謝謝 –

1

可以肯定的,請改變你讓你的形象這樣的方式:

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

你必須使用id引用它您已在佈局中設置,而不是要顯示的繪圖。