可能是一個相當簡單的問題,但我似乎無法理解爲什麼會添加任何onClickListeners。 (我似乎也得到類似的崩潰時,將文本更改偵聽器添加到EditText框,這讓我覺得我一直在設置錯誤?)添加onClickListener時崩潰
我遵循基本的Android開發人員的東西,創建一個onClickListener爲以下...
mPickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
但是,我想創建監聽器,然後有一個onCreate方法來執行代碼。
這是我這是當我運行它
package com.chris.formStuff;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class FormStuffActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
// Create the view
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Load all views.
//EditText edtText1;
Button btnChange1;
// And find all views in the relevant layout file
//edtText1=(EditText) findViewById(R.layout.main);
btnChange1 = (Button) this.findViewById(R.layout.main);
btnChange1.setOnClickListener((OnClickListener) this);
}
@Override
public void onClick(View v)
{
Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
而且logcat的輸出電流崩潰設置...
07-25 13:19:10.593: ERROR/AndroidRuntime(22861): FATAL EXCEPTION: main 07-25 13:19:10.593: ERROR/AndroidRuntime(22861): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.chris.formStuff/com.chris.formStuff.FormStuffActivity}: java.lang.ClassCastException: com.chris.formStuff.FormStuffActivity 07-25 13:19:10.593: ERROR/AndroidRuntime(22861): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787) 07-25 13:19:10.593: ERROR/AndroidRuntime(22861): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803) 07-25 13:19:10.593: ERROR/AndroidRuntime(22861): at android.app.ActivityThread.access$2300(ActivityThread.java:135) 07-25 13:19:10.593: ERROR/AndroidRuntime(22861): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136) 07-25 13:19:10.593: ERROR/AndroidRuntime(22861): at android.os.Handler.dispatchMessage(Handler.java:99) 07-25 13:19:10.593: ERROR/AndroidRuntime(22861): at android.os.Looper.loop(Looper.java:144) 07-25 13:19:10.593: ERROR/AndroidRuntime(22861): at android.app.ActivityThread.main(ActivityThread.java:4937) 07-25 13:19:10.593: ERROR/AndroidRuntime(22861): at java.lang.reflect.Method.invokeNative(Native Method) 07-25 13:19:10.593: ERROR/AndroidRuntime(22861): at java.lang.reflect.Method.invoke(Method.java:521) 07-25 13:19:10.593: ERROR/AndroidRuntime(22861): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 07-25 13:19:10.593: ERROR/AndroidRuntime(22861): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 07-25 13:19:10.593: ERROR/AndroidRuntime(22861): at dalvik.system.NativeStart.main(Native Method) 07-25 13:19:10.593: ERROR/AndroidRuntime(22861): Caused by: java.lang.ClassCastException: com.chris.formStuff.FormStuffActivity 07-25 13:19:10.593: ERROR/AndroidRuntime(22861): at com.chris.formStuff.FormStuffActivity.onCreate(FormStuffActivity.java:25) 07-25 13:19:10.593: ERROR/AndroidRuntime(22861): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069) 07-25 13:19:10.593: ERROR/AndroidRuntime(22861): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751) 07-25 13:19:10.593: ERROR/AndroidRuntime(22861): ... 11 more
btnChange1.setOnClickListener((OnClickListener)this);嘗試刪除轉換爲OnClickListener –