【问题标题】:Android Studio: java.lang.RuntimeException: Unable to start activityAndroid Studio:java.lang.RuntimeException:无法启动活动
【发布时间】:2019-03-07 20:49:23
【问题描述】:

我是一个初学者,我正在做一个 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)

更新:
我解决了我的问题,我只需要在清单中声明应用程序,感谢您的帮助!

【问题讨论】:

标签: java android xml android-layout canvas


【解决方案1】:

stacktrace 中的最后 3 行声明您不能将 application 强制转换为 game 类的实例。

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

setContentView 之后的 主要活动 中,您可以在没有 this 的情况下使用 getActivity,即

gam = (Game) getActivity();

我认为现在的问题可能是您正在尝试将 application 的实例转换为 activity 的实例。

只要我的两分钱。

【讨论】:

  • 我无法执行 getActivity()。
【解决方案2】:

堆栈跟踪显示 -

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

这个answer 提供了更好的图片。

希望对你有帮助。

【讨论】:

  • 您好,感谢您的帮助,那我该怎么办?
【解决方案3】:

这个堆栈跟踪显示了这个

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

您不能将超类的实例强制转换为子类。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-24
    • 1970-01-01
    • 2012-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多