AndroidRuntime致命异常:主要

时间:2015-01-15 21:07:45

标签: java android eclipse android-layout

每当我运行程序时,我都会收到错误Fatal Exception main。我有一个活动续 游戏关卡的按钮。每当点击一个按钮时,玩家应该被定向到其相应的级别。但是每当我点击一个按钮时,我的程序就会停止工作。(我只是开始在门廊级别工作)

这是我的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"
    android:background="@drawable/scr_levelboard"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Survival" >

<ImageButton
    android:id="@+id/btn_porch"
    android:layout_width="100dip"
    android:layout_height="40dip"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="36dp"
    android:layout_marginTop="93dp"
    android:background="@null"
    android:scaleType="fitXY"
    android:src="@drawable/btn_porch" 
    android:onClick="LvlSelect"/>

<ImageButton
    android:id="@+id/btn_livingroom"
    android:layout_width="100dip"
    android:layout_height="40dip"
    android:layout_alignLeft="@+id/btn_porch"
    android:layout_below="@+id/btn_porch"
    android:background="@null"
    android:scaleType="fitXY"
    android:src="@drawable/btn_livingroom" 
    android:onClick="LvlSelect"/>

<ImageButton
    android:id="@+id/btnkitchen"
    android:layout_width="100dip"
    android:layout_height="40dip"
    android:layout_alignLeft="@+id/btnbedroom"
    android:layout_below="@+id/btnbedroom"
    android:layout_marginTop="14dp"
    android:background="@null"
    android:scaleType="fitXY"
    android:src="@drawable/btn_kitchen" 
    android:onClick="LvlSelect"/>

<ImageButton
    android:id="@+id/btnbedroom"
    android:layout_width="100dip"
    android:layout_height="40dip"
    android:layout_alignLeft="@+id/btn_livingroom"
    android:layout_below="@+id/btn_livingroom"
    android:background="@null"
    android:scaleType="fitXY"
    android:src="@drawable/btn_bedroom" 
    android:onClick="LvlSelect"/>

<ImageButton
    android:id="@+id/btnwashroom"
    android:layout_width="100dip"
    android:layout_height="40dip"
    android:layout_alignLeft="@+id/btnkitchen"
    android:layout_below="@+id/btnkitchen"
    android:layout_marginTop="18dp"
    android:background="@null"
    android:scaleType="fitXY"
    android:src="@drawable/btn_washroom" 
    android:onClick="LvlSelect"/>

<ImageButton
    android:id="@+id/btnbasement"
    android:layout_width="100dip"
    android:layout_height="40dip"
    android:layout_alignLeft="@+id/btnwashroom"
    android:layout_below="@+id/btnwashroom"
    android:background="@null"
    android:scaleType="fitXY"
    android:src="@drawable/btn_basement" 
    android:onClick="LvlSelect"/>

</RelativeLayout>

现在这是我对应的课程:

package com.example.thesis;

import android.os.Bundle;

import android.view.View;
import android.view.Window;
import android.app.Activity;

import android.content.Intent;


public class Survival extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_survival);

}
    public void LvlSelect (View view){
        if(view.getId()==R.id.btn_porch){
        Intent d=new Intent(this, PorchStartactivity.class);
        startActivity(d);}

        else if(view.getId()==R.id.btn_livingroom){
            Intent b=new Intent(this, LvroomStartactivity.class);
            startActivity(b);
            }
            else if(view.getId()==R.id.btn_bdroom){
            Intent c=new Intent(this, BdroomStartactivity.class);
            startActivity(c);

        }
    }
}

最后,这是我的PorchStartActivity.class:

package com.example.thesis;



import android.os.Bundle;
import android.view.Window;
import android.app.Activity;
import android.media.MediaPlayer;


public class PorchStartactivity extends Activity {

    MediaPlayer bgmusic;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(new PorchGameView(this));

        bgmusic = MediaPlayer.create(PorchStartactivity.this, R.raw.bgm_housefly);
        bgmusic.setLooping(true);
        bgmusic.start();
    }
    @Override
    protected void onPause(){
        super.onPause();
        bgmusic.release();
        finish();
    }
}

这里是logcat(抱歉不发布)

01-15 22:24:11.428: E/AndroidRuntime(4170): FATAL EXCEPTION: main
01-15 22:24:11.428: E/AndroidRuntime(4170): java.lang.IllegalStateException: Could not execute method of the activity
01-15 22:24:11.428: E/AndroidRuntime(4170):     at android.view.View$1.onClick(View.java:3599)
01-15 22:24:11.428: E/AndroidRuntime(4170):     at android.view.View.performClick(View.java:4204)
01-15 22:24:11.428: E/AndroidRuntime(4170):     at android.view.View$PerformClick.run(View.java:17355)
01-15 22:24:11.428: E/AndroidRuntime(4170):     at android.os.Handler.handleCallback(Handler.java:725)
01-15 22:24:11.428: E/AndroidRuntime(4170):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-15 22:24:11.428: E/AndroidRuntime(4170):     at android.os.Looper.loop(Looper.java:137)
01-15 22:24:11.428: E/AndroidRuntime(4170):     at android.app.ActivityThread.main(ActivityThread.java:5041)
01-15 22:24:11.428: E/AndroidRuntime(4170):     at java.lang.reflect.Method.invokeNative(Native Method)
01-15 22:24:11.428: E/AndroidRuntime(4170):     at java.lang.reflect.Method.invoke(Method.java:511)
01-15 22:24:11.428: E/AndroidRuntime(4170):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-15 22:24:11.428: E/AndroidRuntime(4170):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-15 22:24:11.428: E/AndroidRuntime(4170):     at dalvik.system.NativeStart.main(Native Method)
01-15 22:24:11.428: E/AndroidRuntime(4170): Caused by: java.lang.reflect.InvocationTargetException
01-15 22:24:11.428: E/AndroidRuntime(4170):     at java.lang.reflect.Method.invokeNative(Native Method)
01-15 22:24:11.428: E/AndroidRuntime(4170):     at java.lang.reflect.Method.invoke(Method.java:511)
01-15 22:24:11.428: E/AndroidRuntime(4170):     at android.view.View$1.onClick(View.java:3594)
01-15 22:24:11.428: E/AndroidRuntime(4170):     ... 11 more
01-15 22:24:11.428: E/AndroidRuntime(4170): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.thesis/com.example.thesis.PorchStartactivity}; have you declared this activity in your AndroidManifest.xml?
01-15 22:24:11.428: E/AndroidRuntime(4170):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1618)
01-15 22:24:11.428: E/AndroidRuntime(4170):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
01-15 22:24:11.428: E/AndroidRuntime(4170):     at android.app.Activity.startActivityForResult(Activity.java:3370)
01-15 22:24:11.428: E/AndroidRuntime(4170):     at android.app.Activity.startActivityForResult(Activity.java:3331)
01-15 22:24:11.428: E/AndroidRuntime(4170):     at android.app.Activity.startActivity(Activity.java:3566)
01-15 22:24:11.428: E/AndroidRuntime(4170):     at android.app.Activity.startActivity(Activity.java:3534)
01-15 22:24:11.428: E/AndroidRuntime(4170):     at com.example.thesis.Survival.LvlSelect(Survival.java:24)
01-15 22:24:11.428: E/AndroidRuntime(4170):     ... 14 more
01-15 22:24:15.369: E/Trace(5617): error opening trace file: No such file or directory (2)

1 个答案:

答案 0 :(得分:0)

你的意图找不到“PorchStartactivity”类。

在你的androidmanifest中声明“PorchStartactivity”活动

此外,需要在应用程序清单文件中将其他活动(您的案例中的级别)声明为活动。有关其重要性的更多信息,请read this.