我正在创建一个 2 玩家反应应用程序,试图学习一些关于 Java/Android 的新东西(如线程),并意识到每当我尝试打开应用程序时,它都会立即黑屏说“你的应用程序很臭哈哈哈它没有响应”(或类似的东西),即使我几乎没有编写任何代码。
我的 MainActivity 类:
public class MainActivity extends Activity implements View.OnClickListener {
Button player1, player2;
Random random;
Double color;
Thread timer;
long x, elapsed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeVar();
game();
}
private void game() {
color = random.nextDouble();
x = System.currentTimeMillis();
}
private void initializeVar() {
Button player1 = (Button) findViewById(R.id.bPlayer1);
Button player2 = (Button) findViewById(R.id.bPlayer2);
player1.setOnClickListener(this);
player2.setOnClickListener(this);
player1.getBackground().setColorFilter(0xFFFF0000,
PorterDuff.Mode.MULTIPLY);
player2.getBackground().setColorFilter(0xFFFF0000,
PorterDuff.Mode.MULTIPLY);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bPlayer1:
elapsed = System.currentTimeMillis();
elapsed = elapsed - x;
if (elapsed < 10) {
} else {
}
break;
case R.id.bPlayer2:
elapsed = System.currentTimeMillis();
elapsed = elapsed - x;
if (elapsed < 10) {
} else {
}
break;
}
}
}
活动 XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/bPlayer1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:src="@drawable/ic_launcher" />
<Button
android:id="@+id/bPlayer2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="100dp"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.meteorfiber.reaction"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name="com.meteorfiber.reaction.MainActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我的日志:
02-20 20:27:54.803: E/AndroidRuntime(1747): FATAL EXCEPTION: main
02-20 20:27:54.803: E/AndroidRuntime(1747): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.meteorfiber.reaction/com.meteorfiber.reaction.MainActivity}: java.lang.NullPointerException
02-20 20:27:54.803: E/AndroidRuntime(1747): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
02-20 20:27:54.803: E/AndroidRuntime(1747): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
02-20 20:27:54.803: E/AndroidRuntime(1747): at android.app.ActivityThread.access$600(ActivityThread.java:123)
02-20 20:27:54.803: E/AndroidRuntime(1747): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
02-20 20:27:54.803: E/AndroidRuntime(1747): at android.os.Handler.dispatchMessage(Handler.java:99)
02-20 20:27:54.803: E/AndroidRuntime(1747): at android.os.Looper.loop(Looper.java:137)
02-20 20:27:54.803: E/AndroidRuntime(1747): at android.app.ActivityThread.main(ActivityThread.java:4424)
02-20 20:27:54.803: E/AndroidRuntime(1747): at java.lang.reflect.Method.invokeNative(Native Method)
02-20 20:27:54.803: E/AndroidRuntime(1747): at java.lang.reflect.Method.invoke(Method.java:511)
02-20 20:27:54.803: E/AndroidRuntime(1747): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-20 20:27:54.803: E/AndroidRuntime(1747): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-20 20:27:54.803: E/AndroidRuntime(1747): at dalvik.system.NativeStart.main(Native Method)
02-20 20:27:54.803: E/AndroidRuntime(1747): Caused by: java.lang.NullPointerException
02-20 20:27:54.803: E/AndroidRuntime(1747): at com.meteorfiber.reaction.MainActivity.game(MainActivity.java:28)
02-20 20:27:54.803: E/AndroidRuntime(1747): at com.meteorfiber.reaction.MainActivity.onCreate(MainActivity.java:24)
02-20 20:27:54.803: E/AndroidRuntime(1747): at android.app.Activity.performCreate(Activity.java:4465)
02-20 20:27:54.803: E/AndroidRuntime(1747): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-20 20:27:54.803: E/AndroidRuntime(1747): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)