应用程序保持强制关闭?

时间:2011-08-03 20:17:24

标签: android eclipse

嘿伙计我仍然是一个菜鸟但是今天我在主菜单XML上完成了布局。我有三个图像按钮,想要在推送时尝试使用另一个XML布局,所以我在youtube上观看了一个关于它的视频,现在我的应用程序保持强制关闭。在eclipse中没有出现任何错误,所以我认为它可能是一个错误的活动名称或其他东西。

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:baselineAligned="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>

这是LogCat所说的:

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)

1 个答案:

答案 0 :(得分:4)

可能有一个ClassCastException。您的ImageButton未延伸Button,但ImageView但您尝试将其投放到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>

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

您应将其更改为:

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