簡體   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