mhyuk mhyuk - 1 month ago 8
Android Question

Android: Switching between two activities Error - Unfortunately "App Name" has stopped

I'm a self learner for android app developer, then this problem came up and I'm not sure if people have this problem before but it says my app has stopped in the emulator.

So these are the codes I have :

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);
}
}


Lastly here is my manifest

<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

Answer Source

Change it to this:

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

And

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

to

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