我已经在不同的应用程序中多次这样做了,但这是我第一次使用蜂窝应用程序而且由于某种原因它无法运行...
我打电话
Intent myIntent = new Intent(getApplicationContext(), PvP.class);
startActivity(myIntent);
按下按钮时。这是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.aaron.decker.make15"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="11" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainMenu"
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="PvP" android:label="PvP" android:theme="@android:style/Theme.NoTitleBar"> </activity>
</application>
</manifest>
此外,如果需要这里是另一个类(只是onCreate):
package com.aaron.decker.make15;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class PvP extends Activity{
private int PLAYER_ONE_SCORE;
private int PLATER_TWO_SCORE;
private boolean turn;
//Button button = (Button) findViewById(R.id.button_id);
TextView p1 = (TextView) findViewById(R.id.p1);
TextView p2 = (TextView) findViewById(R.id.p2);
//If turn is false then it is player one's turn, else if it is true it is player two's turn. FALSE = P1 TRUE = P2
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
turn = false;
}
}
编辑:FORGOT LOGCAT!抱歉!这里:
03-06 16:08:13.382: ERROR/AndroidRuntime(652): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.aaron.decker.make15/com.aaron.decker.make15.PvP}: java.lang.NullPointerException
03-06 16:08:13.382: ERROR/AndroidRuntime(652): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1660)
03-06 16:08:13.382: ERROR/AndroidRuntime(652): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
03-06 16:08:13.382: ERROR/AndroidRuntime(652): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
03-06 16:08:13.382: ERROR/AndroidRuntime(652): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
03-06 16:08:13.382: ERROR/AndroidRuntime(652): at android.os.Handler.dispatchMessage(Handler.java:99)
03-06 16:08:13.382: ERROR/AndroidRuntime(652): at android.os.Looper.loop(Looper.java:126)
03-06 16:08:13.382: ERROR/AndroidRuntime(652): at android.app.ActivityThread.main(ActivityThread.java:3997)
03-06 16:08:13.382: ERROR/AndroidRuntime(652): at java.lang.reflect.Method.invokeNative(Native Method)
03-06 16:08:13.382: ERROR/AndroidRuntime(652): at java.lang.reflect.Method.invoke(Method.java:491)
03-06 16:08:13.382: ERROR/AndroidRuntime(652): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
03-06 16:08:13.382: ERROR/AndroidRuntime(652): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
03-06 16:08:13.382: ERROR/AndroidRuntime(652): at dalvik.system.NativeStart.main(Native Method)
03-06 16:08:13.382: ERROR/AndroidRuntime(652): Caused by: java.lang.NullPointerException
03-06 16:08:13.382: ERROR/AndroidRuntime(652): at android.app.Activity.findViewById(Activity.java:1741)
03-06 16:08:13.382: ERROR/AndroidRuntime(652): at com.aaron.decker.make15.PvP.<init>(PvP.java:13)
03-06 16:08:13.382: ERROR/AndroidRuntime(652): at java.lang.Class.newInstanceImpl(Native Method)
03-06 16:08:13.382: ERROR/AndroidRuntime(652): at java.lang.Class.newInstance(Class.java:1424)
03-06 16:08:13.382: ERROR/AndroidRuntime(652): at android.app.Instrumentation.newActivity(Instrumentation.java:1022)
03-06 16:08:13.382: ERROR/AndroidRuntime(652): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
03-06 16:08:13.382: ERROR/AndroidRuntime(652): ... 11 more
有什么想法吗?也许它是愚蠢的东西,我不知道。似乎无法弄明白。
答案 0 :(得分:3)
TextView p1 = (TextView) findViewById(R.id.p1);
TextView p2 = (TextView) findViewById(R.id.p2);
在设置布局后,必须在onCreate()中调用它们。