我正在做一个简单的游戏,首先要求主要活动有困难。它调用了一个要求游戏模式的新活动。这称为实际游戏的新活动,它只是告诉用户在颜色变化时点击屏幕,并测量他们的反应时间。一旦我运行它,它告诉我“不幸的是,应用程序已经停止”并且存在很多错误。我在其他问题中看到几乎完全相同的错误集,但没有一个答案适用于我。有什么建议?我很抱歉,如果这个问题看起来很模糊,我不知道如何处理这个问题。以下是Android Manifest,三个活动的代码和错误日志:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dabaam.battlereaction"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.dabaam.battlereaction.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.dabaam.battlereaction.GameType">
</activity>
<activity
android:name="com.dabaam.battlereaction.Game">
</activity>
</application>
活动1:
package com.dabaam.battlereaction;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
public class MainActivity extends Activity {
Button ezbtn = (Button) findViewById(R.id.ezbtn);
Button medbtn = (Button) findViewById(R.id.medbtn);
Button hardbtn = (Button) findViewById(R.id.hardbtn);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ezbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
askDifficulty("EASY");
}
});
medbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
askDifficulty("MEDIUM");
}
});
hardbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
askDifficulty("HARD");
}
});
}
private void askDifficulty(String whichDiff){
Intent intent = new Intent(MainActivity.this, GameType.class);
intent.putExtra(GameType.difficulty, whichDiff);
startActivity(intent);
}
}
活动2:
package com.dabaam.battlereaction;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
public class GameType extends Activity{
public static String difficulty = "difficulty";
public static final String EASY = "EASY";
public static final String MEDIUM = "MEDIUM";
public static final String HARD = "HARD";
Button visbtn = (Button) findViewById(R.id.vis_btn);
Button tactbtn = (Button) findViewById(R.id.tact_btn);
Button audbtn = (Button) findViewById(R.id.aud_btn);
public void onCreate(Bundle b){
super.onCreate(b);
setContentView(R.layout.type_layout);
Bundle extras = getIntent().getExtras();
//if( extras.getString(difficulty).equals(EASY) ){
// Start EASY game
difficulty = extras.getString(difficulty);
visbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
launchGame(difficulty,"VISUAL");
}
});
tactbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
launchGame(difficulty,"TACTILE");
}
});
audbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
launchGame(difficulty,"AUDITORY");
}
});
}
private void launchGame(String whichDiff, String whichMode){
Intent intent = new Intent(GameType.this, Game.class);
intent.putExtra(Game.difficulty, whichDiff);
intent.putExtra(Game.mode, whichMode);
startActivity(intent);
}
}
活动3:
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{
LinearLayout glayout = (LinearLayout) findViewById(R.id.gameLayout);
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 = 1212121; //1212121 by default for debugging
public String ms_score = "1212121"; //1212121 by default
public void onCreate(Bundle b){
super.onCreate(b);
setContentView(R.layout.game_layout);
new Timer().schedule(change(), after);
}
public boolean onTouch(View v, MotionEvent event) {
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() {
glayout.setBackgroundColor(R.color.Blue);
time1= System.nanoTime();
return null;
}
}
错误日志:
07-01 17:01:43.490: E/AndroidRuntime(20563): FATAL EXCEPTION: main
07-01 17:01:43.490: E/AndroidRuntime(20563): Process: com.dabaam.battlereaction, PID: 20563
07-01 17:01:43.490: E/AndroidRuntime(20563): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.dabaam.battlereaction/com.dabaam.battlereaction.MainActivity}: java.lang.NullPointerException
07-01 17:01:43.490: E/AndroidRuntime(20563): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2514)
07-01 17:01:43.490: E/AndroidRuntime(20563): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
07-01 17:01:43.490: E/AndroidRuntime(20563): at android.app.ActivityThread.access$800(ActivityThread.java:156)
07-01 17:01:43.490: E/AndroidRuntime(20563): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
07-01 17:01:43.490: E/AndroidRuntime(20563): at android.os.Handler.dispatchMessage(Handler.java:102)
07-01 17:01:43.490: E/AndroidRuntime(20563): at android.os.Looper.loop(Looper.java:157)
07-01 17:01:43.490: E/AndroidRuntime(20563): at android.app.ActivityThread.main(ActivityThread.java:5872)
07-01 17:01:43.490: E/AndroidRuntime(20563): at java.lang.reflect.Method.invokeNative(Native Method)
07-01 17:01:43.490: E/AndroidRuntime(20563): at java.lang.reflect.Method.invoke(Method.java:515)
07-01 17:01:43.490: E/AndroidRuntime(20563): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1069)
07-01 17:01:43.490: E/AndroidRuntime(20563): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:885)
07-01 17:01:43.490: E/AndroidRuntime(20563): at dalvik.system.NativeStart.main(Native Method)
07-01 17:01:43.490: E/AndroidRuntime(20563): Caused by: java.lang.NullPointerException
07-01 17:01:43.490: E/AndroidRuntime(20563): at android.app.Activity.findViewById(Activity.java:1952)
07-01 17:01:43.490: E/AndroidRuntime(20563): at com.dabaam.battlereaction.MainActivity.<init>(MainActivity.java:11)
07-01 17:01:43.490: E/AndroidRuntime(20563): at java.lang.Class.newInstanceImpl(Native Method)
07-01 17:01:43.490: E/AndroidRuntime(20563): at java.lang.Class.newInstance(Class.java:1208)
07-01 17:01:43.490: E/AndroidRuntime(20563): at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
07-01 17:01:43.490: E/AndroidRuntime(20563): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2505)
07-01 17:01:43.490: E/AndroidRuntime(20563): ... 11 more
07-01 17:01:47.684: D/Process(20563): killProcess, pid=20563
07-01 17:01:47.684: D/Process(20563): com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException:131 java.lang.ThreadGroup.uncaughtException:693 java.lang.ThreadGroup.uncaughtException:690
答案 0 :(得分:3)
移动
Button ezbtn = (Button) findViewById(R.id.ezbtn);
Button medbtn = (Button) findViewById(R.id.medbtn);
Button hardbtn = (Button) findViewById(R.id.hardbtn);
下
setContentView(R.layout.type_layout);
当您尝试使用findViewById()
查找按钮时,不会创建按钮,因此会出现NullPointerException。
声明布局后,会创建它们。
答案 1 :(得分:1)
您的应用程序因此而崩溃:
LinearLayout glayout = (LinearLayout) findViewById(R.id.gameLayout);
发生崩溃的原因是您在设置了contentView之前找不到 View
,这发生在您的onCreate(...)
方法中{1}}。
移动&#34; glayout&#34;的初始化低于Activity
电话。
除此之外,我还提出yygyt's建议。