我正在制作一款安卓游戏,我正在制作一家商店。我到了全局变量部分,但似乎没有任何效果。我的问题是,当我进入第二级时,应用程序说它已停止工作并进入菜单。我是编程的菜鸟,所以也许我做的不对,但我需要帮助。这是我的所有商店整数将在哪里的代码:
public class Globals extends Application{
private int player_health=10;
public int getData(){
return this.player_health;
}
public void setData(int d){
this.player_health=d;
}
}
部分android清单:
<application
android:name=".Globals"
android:allowBackup="false"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.hunter.pixeldeath.MainActivity"
android:label="@string/title_activity_menu" >
</activity>
<activity
android:name="com.hunter.pixeldeath.Menu"
android:label="@string/title_activity_menu" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.hunter.pixeldeath.WorldSelect"
android:label="@string/title_activity_world_select" >
</activity>
<activity
android:name="com.hunter.pixeldeath.LevelSelect"
android:label="@string/title_activity_level_select" >
</activity>
<activity
android:name="com.hunter.pixeldeath.Store"
android:label="@string/title_activity_store" >
</activity>
<activity
android:name="com.hunter.pixeldeath.LevelTwo"
android:label="@string/title_activity_level_two" >
</activity>
<activity
android:name="com.hunter.pixeldeath.CharacterSelect"
android:label="@string/title_activity_character_select" >
</activity>
<activity
android:name="com.hunter.pixeldeath.Integers"
android:label="@string/title_activity_integers" >
</activity>
</application>
我是否使用了导致问题的代码(我认为):
Globals g = (Globals)getApplication();
int player_health=g.getData();
atk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (enemy_health > 0){
enemy_health = enemy_health - player_damage;
enmh.setText("Enemy Health:" + enemy_health);
player_health = player_health = enemy_damage;
plrh.setText("Player Health " + player_health);
} else {
}
}
});
日志猫:
08-29 23:59:19.071: E/AndroidRuntime(1525): FATAL EXCEPTION: main
08-29 23:59:19.071: E/AndroidRuntime(1525): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.hunter.pixeldeath/com.hunter.pixeldeath.LevelTwo}: java.lang.NullPointerException
08-29 23:59:19.071: E/AndroidRuntime(1525): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
08-29 23:59:19.071: E/AndroidRuntime(1525): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-29 23:59:19.071: E/AndroidRuntime(1525): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-29 23:59:19.071: E/AndroidRuntime(1525): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-29 23:59:19.071: E/AndroidRuntime(1525): at android.os.Handler.dispatchMessage(Handler.java:99)
08-29 23:59:19.071: E/AndroidRuntime(1525): at android.os.Looper.loop(Looper.java:137)
08-29 23:59:19.071: E/AndroidRuntime(1525): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-29 23:59:19.071: E/AndroidRuntime(1525): at java.lang.reflect.Method.invokeNative(Native Method)
08-29 23:59:19.071: E/AndroidRuntime(1525): at java.lang.reflect.Method.invoke(Method.java:525)
08-29 23:59:19.071: E/AndroidRuntime(1525): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-29 23:59:19.071: E/AndroidRuntime(1525): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-29 23:59:19.071: E/AndroidRuntime(1525): at dalvik.system.NativeStart.main(Native Method)
08-29 23:59:19.071: E/AndroidRuntime(1525): Caused by: java.lang.NullPointerException
08-29 23:59:19.071: E/AndroidRuntime(1525): at com.hunter.pixeldeath.LevelTwo.<init>(LevelTwo.java:21)
08-29 23:59:19.071: E/AndroidRuntime(1525): at java.lang.Class.newInstanceImpl(Native Method)
08-29 23:59:19.071: E/AndroidRuntime(1525): at java.lang.Class.newInstance(Class.java:1130)
08-29 23:59:19.071: E/AndroidRuntime(1525): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
08-29 23:59:19.071: E/AndroidRuntime(1525): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
08-29 23:59:19.071: E/AndroidRuntime(1525): ... 11 more