Android:在两个活动之间切换错误 - 不幸的是“App Name”已停止

时间:2014-09-09 02:13:24

标签: java android eclipse android-activity

我是Android应用程序开发人员的自学者,然后出现了这个问题,我不确定之前是否有人遇到过这个问题,但它说我的应用程序已停止在模拟器中。

所以这些是我的代码:

MainActivity.java

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);  

        Button imageButton13 = (Button) findViewById(R.id.imageButton13);
        imageButton13.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
                        Intent Intent = new Intent(v.getContext(), EXOActivity.class);
                        v.getContext().startActivity(Intent);           
                    }
        });
    }   
}

EXOActivity.java:

package com.mhyuktown.exowallpaper;

import android.app.Activity; import android.os.Bundle;

public class EXOActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);         
        setContentView(R.layout.activity_exo);
    }
}

最后这是我的清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mhyuktown.exowallpaper"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".EXOActivity" >
          </activity>

    </application>

</manifest>

logcat的:

09-09 00:40:02.006: D/dalvikvm(1564): GC_FOR_ALLOC freed 49K, 4% free 3304K/3436K, paused 79ms, total 86ms 
09-09 00:40:02.256: D/dalvikvm(1564): GC_FOR_ALLOC freed 7K, 3% free 3770K/3876K, paused 35ms, total 37ms 
09-09 00:40:02.326: D/AndroidRuntime(1564): Shutting down VM 
09-09 00:40:02.326: W/dalvikvm(1564): threadid=1: thread exiting with uncaught exception (group=0xb1aa7ba8) 
09-09 00:40:02.376: E/AndroidRuntime(1564): FATAL EXCEPTION: main 
09-09 00:40:02.376: E/AndroidRuntime(1564): Process: com.mhyuktown.exowallpaper, PID: 1564 
09-09 00:40:02.376: E/AndroidRuntime(1564): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mhyuktown.exowallpaper/com.mhyuktown.exowallpaper.MainActivity}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button 
09-09 00:40:02.376: E/AndroidRuntime(1564):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 
09-09 00:40:02.376: E/AndroidRuntime(1564):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
09-09 00:40:02.376: E/AndroidRuntime(1564):     at android.app.ActivityThread.access$800(ActivityThread.java:135) 
09-09 00:40:02.376: E/AndroidRuntime(1564):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
09-09 00:40:02.376: E/AndroidRuntime(1564):     at android.os.Handler.dispatchMessage(Handler.java:102) 
09-09 00:40:02.376: E/AndroidRuntime(1564):     at android.os.Looper.loop(Looper.java:136) 
09-09 00:40:02.376: E/AndroidRuntime(1564):     at android.app.ActivityThread.main(ActivityThread.java:5017) 
09-09 00:40:02.376: E/AndroidRuntime(1564):     at java.lang.reflect.Method.invokeNative(Native Method) 
09-09 00:40:02.376: E/AndroidRuntime(1564):     at java.lang.reflect.Method.invoke(Method.java:515) 
09-09 00:40:02.376: E/AndroidRuntime(1564):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
09-09 00:40:02.376: E/AndroidRuntime(1564):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
09-09 00:40:02.376: E/AndroidRuntime(1564):     at dalvik.system.NativeStart.main(Native Method) 
09-09 00:40:02.376: E/AndroidRuntime(1564): Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button 
09-09 00:40:02.376: E/AndroidRuntime(1564):     at com.mhyuktown.exowallpaper.MainActivity.onCreate(MainActivity.java:18) 
09-09 00:40:02.376: E/AndroidRuntime(1564):     at android.app.Activity.performCreate(Activity.java:5231) 
09-09 00:40:02.376: E/AndroidRuntime(1564):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
09-09 00:40:02.376: E/AndroidRuntime(1564):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 
09-09 00:40:02.376: E/AndroidRuntime(1564):     ... 11 more

4 个答案:

答案 0 :(得分:0)

更改

Intent Intent = new Intent(v.getContext(), EXOActivity.class);
v.getContext().startActivity(Intent); 

Intent intent = new Intent(MainActivity.this, EXOActivity.class);
startActivity(intent); 

答案 1 :(得分:0)

将其更改为:

 Intent intent = new Intent(MainActivity.this, EXOActivity.class);
 startActivity(intent);

Button imageButton13 = (Button) findViewById(R.id.imageButton13);

ImageButton imageButton13 = (ImageButton) findViewById(R.id.imageButton13);

答案 2 :(得分:0)

我编码了&amp;它工作正常。导入所需的所有内容。请参阅以下代码。尝试按Ctrl + Shift + O,如果缺少,Eclipse将自动添加import语句。

使用ImageButton代替Button

ImageButton button = (ImageButton)findViewById(R.id.imageButton13);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(MainActivity.this, EXOActivity.class);
            startActivity(intent);
        }
    });

答案 3 :(得分:0)

您创建的每个活动都必须包含在AndroidManifest.xml中。

找到这个:

</activity> 

然后加上这个:

<activity android:name=".YourActivity"></activity>