我正在从这个网站上学习教程 http://www.youtube.com/watch?v=jSyy5TrUD5Q
我似乎为教练工作,但是一旦我在模拟器上运行它就会说
不幸的是,AndroidTest已停止。
主要活动XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
<Button
android:id="@+id/buttonTEST1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button" />
<EditText
android:id="@+id/editTextTEST1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/textView1"
android:layout_alignParentLeft="true"
android:layout_marginBottom="72dp"
android:ems="10" />
</RelativeLayout>
主要活动.java
package com.example.androidtest;
import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener {
Button button1;
EditText editText1;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
button1= (Button) findViewById(R.id.buttonTEST1);
editText1= (EditText) findViewById(R.id.editTextTEST1);
button1.setOnClickListener((android.view.View.OnClickListener)this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void onClick(View v) {
if(v.getId() == R.id.buttonTEST1)
{
// Switching from screen to screen
startActivity(new Intent(this, AndroidActivity2.class));
editText1.setText("Button Tester Works");
}
}
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}
}
logcat的
01-21 20:06:51.753: W/Trace(764): Unexpected value from nativeGetEnabledTags: 0
01-21 20:06:51.763: W/Trace(764): Unexpected value from nativeGetEnabledTags: 0
01-21 20:06:51.763: W/Trace(764): Unexpected value from nativeGetEnabledTags: 0
01-21 20:06:52.015: W/Trace(764): Unexpected value from nativeGetEnabledTags: 0
01-21 20:06:52.015: W/Trace(764): Unexpected value from nativeGetEnabledTags: 0
01-21 20:06:52.482: D/AndroidRuntime(764): Shutting down VM
01-21 20:06:52.482: W/dalvikvm(764): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
01-21 20:06:52.503: E/AndroidRuntime(764): FATAL EXCEPTION: main
01-21 20:06:52.503: E/AndroidRuntime(764): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidtest/com.example.androidtest.MainActivity}: java.lang.ClassCastException: com.example.androidtest.MainActivity cannot be cast to android.view.View$OnClickListener
01-21 20:06:52.503: E/AndroidRuntime(764): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-21 20:06:52.503: E/AndroidRuntime(764): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-21 20:06:52.503: E/AndroidRuntime(764): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-21 20:06:52.503: E/AndroidRuntime(764): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-21 20:06:52.503: E/AndroidRuntime(764): at android.os.Handler.dispatchMessage(Handler.java:99)
01-21 20:06:52.503: E/AndroidRuntime(764): at android.os.Looper.loop(Looper.java:137)
01-21 20:06:52.503: E/AndroidRuntime(764): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-21 20:06:52.503: E/AndroidRuntime(764): at java.lang.reflect.Method.invokeNative(Native Method)
01-21 20:06:52.503: E/AndroidRuntime(764): at java.lang.reflect.Method.invoke(Method.java:511)
01-21 20:06:52.503: E/AndroidRuntime(764): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-21 20:06:52.503: E/AndroidRuntime(764): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-21 20:06:52.503: E/AndroidRuntime(764): at dalvik.system.NativeStart.main(Native Method)
01-21 20:06:52.503: E/AndroidRuntime(764): Caused by: java.lang.ClassCastException: com.example.androidtest.MainActivity cannot be cast to android.view.View$OnClickListener
01-21 20:06:52.503: E/AndroidRuntime(764): at com.example.androidtest.MainActivity.onCreate(MainActivity.java:24)
01-21 20:06:52.503: E/AndroidRuntime(764): at android.app.Activity.performCreate(Activity.java:5104)
01-21 20:06:52.503: E/AndroidRuntime(764): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-21 20:06:52.503: E/AndroidRuntime(764): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-21 20:06:52.503: E/AndroidRuntime(764): ... 11 more
答案 0 :(得分:1)
更改您的活动代码,因为您需要在活动中实施View.OnClickListener
以便为Button添加setOnClickListener
public class MainActivity extends Activity implements View.OnClickListener {
Button button1;
EditText editText1;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
button1= (Button) findViewById(R.id.buttonTEST1);
editText1= (EditText) findViewById(R.id.editTextTEST1);
button1.setOnClickListener(this);
}
//your code here
答案 1 :(得分:0)
您没有实施OnClickListener
(至少已被注释掉),因此您的onClick()
方法无效。
答案 2 :(得分:0)
您正试图将您的活动解析为OnClickListener
。但是,您尚未实现它,因为代码已注释掉(使用/* ... */
)。
变化
/*implements OnClickListener*/
到
implements OnClickListener