可能是一个相当简单的问题,但我似乎无法理解为什么会添加任何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 –