App keeps Force Closing?
Hey guys I am still sort of a noob but today I finished up the layout on my main menu XML. I had three image buttons and wanted to try to make one go to another XML layout when pushed so I watched a video on youtube about it and now my app keeps forceclosing. No errors show up in eclipse so I think it might be a wrong activity name or something.
DragonFruitActivity.java
package com.Dragon_Fruit;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
public class DragonFruitActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button playbutton = (Button) findViewById(R.id.playbutton);
playbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent("com.Dragon_Fruit.playbutton"));
}
});
}
}
playbutton.java
package com.Dragon_Fruit;
import android.app.Activity;
import android.os.Bundle;
public class playbutton extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.playscreen);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:background="@drawable/mainbackground"
android:weightSum="1"
android:baselineAl开发者_JS百科igned="false" android:orientation="vertical">
<ImageButton
android:layout_width="wrap_content"
android:background="@drawable/playbutton"
android:layout_height="wrap_content"
android:src="@drawable/playbutton"
android:layout_gravity="center_horizontal"
android:id="@+id/playbutton">
</ImageButton>
<ImageButton
android:layout_width="wrap_content"
android:id="@+id/imageButton1"
android:background="@drawable/playbutton"
android:layout_height="wrap_content"
android:src="@drawable/settingsbutton"
android:layout_gravity="center_horizontal">
</ImageButton>
<ImageButton
android:layout_width="wrap_content"
android:id="@+id/imageButton1"
android:background="@drawable/playbutton"
android:layout_height="wrap_content"
android:src="@drawable/creditsbutton" android:layout_gravity="center_horizontal">
</ImageButton>
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Dragon_Fruit"
android:versionCode="1"
android:versionName="1.0"
android:installLocation="preferExternal">
<application android:icon="@drawable/icon"
android:label="@string/app_name"
android:debuggable="true">
<activity android:name=".DragonFruitActivity"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:configChanges="keyboard|keyboardHidden|orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".playbutton"
android:label="@string/app_name"
>
<intent-filter>
<action android:name="com.Dragon_Fruit.PLAYBUTTON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="9"/>
</manifest>
Here is what LogCat says:
08-03 16:24:26.974: ERROR/AndroidRuntime(3910): FATAL EXCEPTION: main
08-03 16:24:26.974: ERROR/AndroidRuntime(3910): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Dragon_Fruit/com.Dragon_Fruit.DragonFruitActivity}: java.lang.ClassCastException: android.widget.ImageButton
08-03 16:24:26.974: ERROR/AndroidRuntime(3910): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2737)
08-03 16:24:26.974: ERROR/AndroidRuntime(3910): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2753)
08-03 16:24:26.974: ERROR/AndroidRuntime(3910): at android.app.ActivityThread.access$2500(ActivityThread.java:129)
08-03 16:24:26.974: ERROR/AndroidRuntime(3910): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2107)
08-03 16:24:26.974: ERROR/AndroidRuntime(3910): at android.os.Handler.dispatchMessage(Handler.java:99)
08-03 16:24:26.974: ERROR/AndroidRuntime(3910): at android.os.Looper.loop(Looper.java:143)
08-03 16:24:26.974: ERROR/AndroidRuntime(3910): at android.app.ActivityThread.main(ActivityThread.java:4701)
08-03 16:24:26.974: ERROR/AndroidRuntime(3910): at java.lang.reflect.Method.invokeNative(Native Method)
08-03 16:24:26.974: ERROR/AndroidRuntime(3910): at java.lang.reflect.Method.invoke(Method.java:521)
08-03 16:24:26.974: ERROR/AndroidRuntime(3910): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-03 16:24:26.974: ERROR/AndroidRuntime(3910): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-03 16:24:26.974: ERROR/AndroidRuntime(3910): at dalvik.system.NativeStart.main(Native Method)
08-03 16:24:26.974: ERROR/AndroidRuntime(3910): Caused by: java.lang.ClassCastException: android.widget.ImageButton
08-03 16:24:26.974: ERROR/AndroidRuntime(3910): at com.Dragon_Fruit.DragonFruitActivity.onCreate(DragonFruitActivity.java:21)
08-03 16:24:26.974: ERROR/AndroidRuntime(3910): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-03 16:24:26.974: ERROR/AndroidRuntime(3910): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2701)
You probably have a ClassCastException
. Your ImageButton
does not extend Button
, but ImageView
yet you try to cast it to Button
:
<ImageButton
android:layout_width="wrap_content"
android:background="@drawable/playbutton"
android:layout_height="wrap_content"
android:src="@drawable/playbutton"
android:layout_gravity="center_horizontal"
android:id="@+id/playbutton">
</ImageButton>
And
Button playbutton = (Button) findViewById(R.id.playbutton);
You should change it to:
ImageButton playbutton = (ImageButton) findViewById(R.id.playbutton);
playbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent("com.Dragon_Fruit.playbutton"));
}
}
精彩评论