有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android应用程序在一个活动中崩溃

我编写了一个应用程序,首先要求用户为反应时游戏选择一些参数。当它到达游戏本身的活动时,应用程序崩溃。该活动应在2到10秒之间的随机时间后将屏幕颜色从红色更改为绿色,然后启动计时器。当用户点击时计时器停止,从而测量他们对颜色变化的反应时间。以下是活动的代码和我的错误日志。如果您看到收到消息的原因,请告诉我“很遗憾,应用程序已停止”

JAVA:

package com.dabaam.battlereaction;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import 安卓.annotation.SuppressLint;
import 安卓.app.Activity;
import 安卓.os.Bundle;
import 安卓.view.MotionEvent;
import 安卓.view.View.OnTouchListener;
import 安卓.view.View;
import 安卓.widget.LinearLayout;
import 安卓.widget.TextView;

public class Game extends Activity implements OnTouchListener{
    public static final String difficulty = "difficulty";
    public static final String mode = "mode";
    public Random r = new Random();
    int after = r.nextInt(10000 - 2000) + 2000;
    public long time1 = 0;
    public long time2 = 0;
    public long elapsed = 80085; //boobs by default
    public String ms_score = "boobs"; //boobs by default

    public void onCreate(Bundle b){
        super.onCreate(b);
        setContentView(R.layout.game_layout);
        new Timer().schedule(change(), after);
    }

    public LinearLayout glayout = (LinearLayout) findViewById(R.id.gameLayout);

    public boolean onTouch(View v, MotionEvent event) {
        // LinearLayout glayout = (LinearLayout) findViewById(R.id.gameLayout);
        time2= System.nanoTime();
        elapsed = (time2-time1)/1000000;

        TextView score = new TextView(getApplicationContext());
        ms_score = getString(R.string.score, elapsed);
        score.setText(ms_score);
        glayout.addView(score);

        return false;
    }

    @SuppressLint("ResourceAsColor") public TimerTask change() {
        // LinearLayout glayout = (LinearLayout) findViewById(R.id.gameLayout);
        glayout.setBackgroundColor(R.color.Blue);
        time1= System.nanoTime();
        return null;
    }

}

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:id="@+id/gameLayout"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:orientation="vertical" 
    安卓:background="@color/red" >

</LinearLayout>

错误日志:

07-01 19:16:03.777: W/asset(19731): Copying FileAsset 0x761e3178 (zip:/data/app/com.dabaam.battlereaction-2.apk:/resources.arsc) to buffer size 108276 to make it aligned.
07-01 19:16:03.897: I/Adreno-EGL(19731): <qeglDrvAPI_eglInitialize:316>: EGL 1.4 QUALCOMM build:  (CL4169980)
07-01 19:16:03.897: I/Adreno-EGL(19731): OpenGL ES Shader Compiler Version:  17.01.10.SPL
07-01 19:16:03.897: I/Adreno-EGL(19731): Build Date: 02/04/14 Tue
07-01 19:16:03.897: I/Adreno-EGL(19731): Local Branch: 
07-01 19:16:03.897: I/Adreno-EGL(19731): Remote Branch: 
07-01 19:16:03.897: I/Adreno-EGL(19731): Local Patches: 
07-01 19:16:03.897: I/Adreno-EGL(19731): Reconstruct Branch: 
07-01 19:16:05.659: W/dalvikvm(19731): threadid=1: thread exiting with uncaught exception (group=0x41642e18)
07-01 19:16:05.669: E/AndroidRuntime(19731): FATAL EXCEPTION: main
07-01 19:16:05.669: E/AndroidRuntime(19731): Process: com.dabaam.battlereaction, PID: 19731
07-01 19:16:05.669: E/AndroidRuntime(19731): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.dabaam.battlereaction/com.dabaam.battlereaction.Game}: java.lang.NullPointerException
07-01 19:16:05.669: E/AndroidRuntime(19731):    at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2514)
07-01 19:16:05.669: E/AndroidRuntime(19731):    at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
07-01 19:16:05.669: E/AndroidRuntime(19731):    at 安卓.app.ActivityThread.access$800(ActivityThread.java:156)
07-01 19:16:05.669: E/AndroidRuntime(19731):    at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
07-01 19:16:05.669: E/AndroidRuntime(19731):    at 安卓.os.Handler.dispatchMessage(Handler.java:102)
07-01 19:16:05.669: E/AndroidRuntime(19731):    at 安卓.os.Looper.loop(Looper.java:157)
07-01 19:16:05.669: E/AndroidRuntime(19731):    at 安卓.app.ActivityThread.main(ActivityThread.java:5872)
07-01 19:16:05.669: E/AndroidRuntime(19731):    at java.lang.reflect.Method.invokeNative(Native Method)
07-01 19:16:05.669: E/AndroidRuntime(19731):    at java.lang.reflect.Method.invoke(Method.java:515)
07-01 19:16:05.669: E/AndroidRuntime(19731):    at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1069)
07-01 19:16:05.669: E/AndroidRuntime(19731):    at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:885)
07-01 19:16:05.669: E/AndroidRuntime(19731):    at dalvik.system.NativeStart.main(Native Method)
07-01 19:16:05.669: E/AndroidRuntime(19731): Caused by: java.lang.NullPointerException
07-01 19:16:05.669: E/AndroidRuntime(19731):    at 安卓.app.Activity.findViewById(Activity.java:1952)
07-01 19:16:05.669: E/AndroidRuntime(19731):    at com.dabaam.battlereaction.Game.<init>(Game.java:32)
07-01 19:16:05.669: E/AndroidRuntime(19731):    at java.lang.Class.newInstanceImpl(Native Method)
07-01 19:16:05.669: E/AndroidRuntime(19731):    at java.lang.Class.newInstance(Class.java:1208)
07-01 19:16:05.669: E/AndroidRuntime(19731):    at 安卓.app.Instrumentation.newActivity(Instrumentation.java:1079)
07-01 19:16:05.669: E/AndroidRuntime(19731):    at  安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2505)
07-01 19:16:05.669: E/AndroidRuntime(19731):    ... 11 more

共 (1) 个答案

  1. # 1 楼答案

    public LinearLayout glayout = (LinearLayout) findViewById(R.id.gameLayout);
    

    不能初始化引用类似这样的视图的字段findViewById()如果在super.onCreate()之前运行,将崩溃。此外,它应该在setContentView()之后运行,否则将返回null

    您应该将此代码更改为以下内容:

    public LinearLayout glayout;
    
    public void onCreate(Bundle b) {
        super.onCreate(b);
        setContentView(R.layout.game_layout);
        glayout = (LinearLayout) findViewById(R.id.gameLayout);
        new Timer().schedule(change(), after);
    }