java - 不幸的是,AndroidTest 已经停止

标签 java android android-layout android-listview

我正在研究这个网站上的教程 http://www.youtube.com/watch?v=jSyy5TrUD5Q

我似乎在为讲师工作,但一旦我在模拟器上运行它,它就会说

很遗憾,AndroidTest 已停止。

主要 Activity 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>

主要 Activity .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

最佳答案

更改您的 Activity 代码,因为您需要在 Activity 中实现 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

关于java - 不幸的是,AndroidTest 已经停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14446619/

相关文章:

android - 多个屏幕的图像按钮大小

java - 当发布者线程数远高于消费者数时,Spring Reactor 会受益

java - 在 Windows 10 上使用 OpenJDK 11 启动 Eclipse 时出现问题

java - Spring MVC - 动态更改安全设置

android - 从 fragment 按钮单击( Intent )开始 Activity 会抛出空白屏幕

android - 如何将可绘制的形状与图像一起使用?

java - 在 android 中使用字符串的数字选择器显示完整的文本值

android - 弹跳按钮 flutter

android - 服务在循环时崩溃 - Android Studio

android - 如何调整 ScrollView 的大小以适合底部 60% 的屏幕?