2012-02-04 83 views
1

我正在做一個非常簡單的應用程序,它必須在用戶點擊一個按鈕時執行一個動作。我試圖用OnClickListener來做到這一點,但這會導致程序在執行期間停止運行 。我尋找不同的解決方案,但沒有任何可以幫助我。 我發現問題位於哪裏放置的行:Button.setOnClickListenerAndroid:Button.setOnClickListener在運行時產生錯誤

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(0x7f030000); 

    Button b = (Button) findViewById (0x7f05000a) ; 
    b.setOnClickListener((android.view.View.OnClickListener) buttonL) ; 



} 
private OnClickListener buttonL = new OnClickListener () { 

    @Override 
    public void onClick(DialogInterface arg0, int arg1) { 
//perform action} 
}; 

通過logcat的給出的誤差:

02-04 19:45:46.101: E/AndroidRuntime(27043): FATAL EXCEPTION: main 
02-04 19:45:46.101: E/AndroidRuntime(27043): java.lang.RuntimeException: Unable to start activity ComponentInfo{me.sms.smsReminder/me.sms.smsReminder.SmsReminderActivity}: java.lang.ClassCastException: me.sms.smsReminder.SmsReminderActivity$1 
02-04 19:45:46.101: E/AndroidRuntime(27043): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1830) 
02-04 19:45:46.101: E/AndroidRuntime(27043): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1851) 
02-04 19:45:46.101: E/AndroidRuntime(27043): at android.app.ActivityThread.access$1500(ActivityThread.java:132) 
02-04 19:45:46.101: E/AndroidRuntime(27043): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038) 
02-04 19:45:46.101: E/AndroidRuntime(27043): at android.os.Handler.dispatchMessage(Handler.java:99) 
02-04 19:45:46.101: E/AndroidRuntime(27043): at android.os.Looper.loop(Looper.java:150) 
02-04 19:45:46.101: E/AndroidRuntime(27043): at android.app.ActivityThread.main(ActivityThread.java:4277) 
02-04 19:45:46.101: E/AndroidRuntime(27043): at java.lang.reflect.Method.invokeNative(Native Method) 
02-04 19:45:46.101: E/AndroidRuntime(27043): at java.lang.reflect.Method.invoke(Method.java:507) 
02-04 19:45:46.101: E/AndroidRuntime(27043): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
02-04 19:45:46.101: E/AndroidRuntime(27043): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
02-04 19:45:46.101: E/AndroidRuntime(27043): at dalvik.system.NativeStart.main(Native Method) 
02-04 19:45:46.101: E/AndroidRuntime(27043): Caused by: java.lang.ClassCastException: me.sms.smsReminder.SmsReminderActivity$1 
02-04 19:45:46.101: E/AndroidRuntime(27043): at me.sms.smsReminder.SmsReminderActivity.onCreate(SmsReminderActivity.java:28) 
02-04 19:45:46.101: E/AndroidRuntime(27043): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072) 
02-04 19:45:46.101: E/AndroidRuntime(27043): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1794) 
02-04 19:45:46.101: E/AndroidRuntime(27043): ... 11 more 

謝謝,湯姆

回答

1

Yo您已導入並正在使用錯誤的偵聽器DialogInterface.OnClickListener。因此,當偵聽器被觸發時,您的活動無法投射到DialogInterface中,因此出現錯誤。使用View.OnClickListener,它應該工作。

+0

謝謝,這確實是問題所在。 – tb96 2012-02-05 11:41:47

0
setContentView(0x7f030000); 

Button b = (Button) findViewById (0x7f05000a) ; 

這些線強烈不鼓勵。使用`R.id.buttonname」語法來使你的代碼不易出錯.. 和u可以告訴您的代碼

直接實現的onclick功能線28 ...

在按鍵佈局,添加另一個參數

android:onclick="functionName" 

在java中的活動file..create這樣便的方法,

public void functionName(View v) 
{ 
//Tasks to perform at on click event 
} 
+0

你永遠不應該綁定在那樣的XML的onclick事件。這違背了創建可重用的XML佈局的目的! – Jonathan 2012-02-04 18:56:50

+0

@Jonathan:雖然我原則上同意WRT可重用代碼,但在許多情況下綁定onClick監聽器在XML佈局文件中沒有任何問題。 – Squonk 2012-02-04 19:11:35

2

不要使用資源的字面ID,您應該是使用:

R.id.your_id

R是動態產生的對象保持你的資源。

例如,如果你有在res /佈局/ main.xml中作爲如此定義一個TextView:

<TextView 
    android:id="@+id/myTextView" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:singleLine="true" 
    android:text="Test Type" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:textColor="@color/BLACK_TEXTCOLOR" 
    android:textStyle="bold" /> 

你會爲引用此:

R.id.myTextView

我也會推薦使用匿名類爲你點擊綁定:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.id.myLayoutId); 

    Button b = (Button) findViewById (R.id.myButton) ; 
    b.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      //perform action 
     } 
    }); 
} 
0

你能說雙檢buttonL確實是一個

android.view.View。 OnClickListener

,而不是一個

android.content.DialogInterface。OnClickListener

(檢查import語句)

你也可以穿上b.setOnClickListener斷點和檢查值/類型buttonL

0

您使用的是DialogInterface.OnClickListener。你需要使用一個View.OnClickListener,它需要函數onClick(View v),而不是onClick(DialogInterface arg0,int arg1)。

當您嘗試將DialogInterface.OnClickListener強制轉換爲View.OnClickListener時,該錯誤是類轉換異常,因爲一個用於對話框,後者用於按鈕。

上面也嘗試用R.它的一個原因產生的,所以你應該把它作爲提到...