繁体   English   中英

Android应用程式在一次活动中当机

[英]Android app crashes on one activity

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

JAVA:

package com.dabaam.battlereaction;

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

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View.OnTouchListener;
import android.view.View;
import android.widget.LinearLayout;
import android.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:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gameLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android: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 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2514)
07-01 19:16:05.669: E/AndroidRuntime(19731):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
07-01 19:16:05.669: E/AndroidRuntime(19731):    at android.app.ActivityThread.access$800(ActivityThread.java:156)
07-01 19:16:05.669: E/AndroidRuntime(19731):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
07-01 19:16:05.669: E/AndroidRuntime(19731):    at android.os.Handler.dispatchMessage(Handler.java:102)
07-01 19:16:05.669: E/AndroidRuntime(19731):    at android.os.Looper.loop(Looper.java:157)
07-01 19:16:05.669: E/AndroidRuntime(19731):    at android.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.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1069)
07-01 19:16:05.669: E/AndroidRuntime(19731):    at com.android.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 android.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 android.app.Instrumentation.newActivity(Instrumentation.java:1079)
07-01 19:16:05.669: E/AndroidRuntime(19731):    at  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2505)
07-01 19:16:05.669: E/AndroidRuntime(19731):    ... 11 more
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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM