簡體   English   中英

Android Studio:java.lang.RuntimeException:無法啟動活動

[英]Android Studio: java.lang.RuntimeException: Unable to start activity

我是一個初學者,正在做一個Android游戲,我用Java(在Eclipse上)制作了模型。

我實際上是在嘗試繪制網格,但是當我運行時,我有一個java.lang.RuntimeException: Unable to start activity

我真的不明白問題出在哪里,它說問題出在setContentView (R.layout.activity_main)

這是我的AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="e.khoig.test2">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:ignore="GoogleAppIndexingWarning">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

這是其中包含游戲及其創建的位置:

public class Game extends Application {
    TenGame tenGame;

    public void onCreate(){
        super.onCreate();
        tenGame=new TenGame();

    }

    TenGame getTenGame(){
        return tenGame;
    }
}

這是我繪制並執行動作的GameView:

public class GameView extends SurfaceView implements SurfaceHolder.Callback{
    Game gam;
    Paint paint=new Paint();
    int canvasWidth;
    int cellSize;

    public GameView(Context context, AttributeSet attributeSet){
        super(context,attributeSet);
        getHolder().addCallback(this);
        getGam(context);
    }

    public GameView(Context context){
        super(context);
        getHolder().addCallback(this);
        this.getGam(context);
    }

    public final void getGam(Context context){
        gam=(Game)(context.getApplicationContext());
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {

    }


    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        canvasWidth=width;
        cellSize=width/5;
        reDraw();
    }


    public void reDraw(){
        Canvas c=getHolder().lockCanvas();
        if(c!=null){
            this.onDraw(c);
            getHolder().unlockCanvasAndPost(c);

        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x=(int)event.getX();
        int y=(int)event.getY();

        int action=event.getAction();
        TenGame theGame=gam.getTenGame();
        switch(action){
            case MotionEvent.ACTION_DOWN:{
                theGame.getSelectedGroup();
            }
            default:
                return false;
        }

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }
    @Override
    public void onDraw(Canvas canvas){
        paint.reset();
        TenGame theGame=gam.getTenGame();
        canvas.drawColor(Color.GRAY);

        paint.setColor(Color.BLACK);
        for(int x=0;x<canvasWidth;x+=cellSize){
            canvas.drawLine(x,0,x,canvasWidth,paint);
        }
        for(int y=0;y<canvasWidth;y+=cellSize){
            canvas.drawLine(0,y,canvasWidth,y,paint);
        }

        paint.setTextSize(50);
        paint.setFlags(Paint.ANTI_ALIAS_FLAG);

        for(int x=0;x<5;x++){
            for(int y=0;y<5;y++){
                canvas.drawText("1",(x*cellSize)+11,(cellSize+y*cellSize)-6,paint);
            }
        }
    }
}

最后是我的MainActivity:

public class MainActivity extends Activity {
    Game gam;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gam=(Game)this.getApplication();

    }
}

而我的activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <e.khoig.test2.GameView
        android:id="@+id/gameView"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_gravity="center"
         />

</LinearLayout>

這是我的錯誤消息:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: e.khoig.test2, PID: 15920
java.lang.RuntimeException: Unable to start activity ComponentInfo{e.khoig.test2/e.khoig.test2.MainActivity}: android.view.InflateException: Binary XML file line #18: Binary XML file line #18: Error inflating class e.khoig.test2.GameView
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2957)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6944)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
 Caused by: android.view.InflateException: Binary XML file line #18: Binary XML file line #18: Error inflating class e.khoig.test2.GameView
 Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class e.khoig.test2.GameView
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.newInstance0(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
    at android.view.LayoutInflater.createView(LayoutInflater.java:647)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
    at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:461)
    at android.app.Activity.setContentView(Activity.java:2737)
    at e.khoig.test2.MainActivity.onCreate(MainActivity.java:12)
    at android.app.Activity.performCreate(Activity.java:7183)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6944)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
 Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to e.khoig.test2.Game
    at e.khoig.test2.GameView.getGam(GameView.java:34)
    at e.khoig.test2.GameView.<init>(GameView.java:24)

更新:
我解決了我的問題,我只需要在清單中聲明應用程序,謝謝您的幫助!

堆棧跟蹤中的最后三行指出您無法將應用程序 強制轉換為游戲類的實例。

Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to e.khoig.test2.Game

setContentView之后的主要活動中 ,可以使用不帶this的 getActivity ,即

gam = (Game) getActivity();

我認為現在的問題可能是您正在嘗試將應用程序實例轉換為活動實例。

只是我的兩分錢。

堆棧跟蹤顯示-

Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to e.khoig.test2.Game at e.khoig.test2.GameView.getGam(GameView.java:34)

您正在這樣做-

public final void getGam(Context context){ gam=(Game)(context.getApplicationContext()); }

您不應該將超類的實例轉換為子類。 這導致您的ClassCastException

這個答案給出了更好的畫面。

希望能幫助到你。

該堆棧跟蹤顯示了這一點

Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to e.khoig.test2.Game

因為這

A extends B
A a = new B()  // this is wrong
(B)a//this is wrong
B b = new A()  // this is right
(A)b // this is right

您不能將超類的實例轉換為子類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM