gpt4 book ai didi

java - Android 应用程序在一项 Activity 中崩溃

转载 作者:行者123 更新时间:2023-12-02 05:41:00 27 4
gpt4 key购买 nike

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

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);

您无法像这样初始化引用 View 的字段。如果 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);
}

关于java - Android 应用程序在一项 Activity 中崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24520636/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com