0

我正在制作一款安卓游戏,我正在制作一家商店。我到了全局变量部分,但似乎没有任何效果。我的问题是,当我进入第二级时,应用程序说它已停止工作并进入菜单。我是编程的菜鸟,所以也许我做的不对,但我需要帮助。这是我的所有商店整数将在哪里的代码:

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
4

2 回答 2

0

NullPointerException您在第 21 行的 LevelTwo.java 中有一个,它在LevelTwo构造实例时发生。查看这一行以找出哪个变量可以是null或使用调试器。

堆栈跟踪中的这些行告诉您:

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)

编辑(从 OP 获得更多信息后):

错误发生在第二行(在类构造函数中):

Globals g = (Globals)getApplication();
int player_health=g.getData();

在这里获得 NPE 只有一种可能性,gnull. 并且gnull因为在执行构造函数时尚未设置应用程序。代码应该移到onCreate()活动的方法中。

于 2013-08-30T04:43:25.723 回答
-1

使全局变量player_health成为静态变量。那就是你的代码变成

public class Globals extends Application{
   private static int player_health;

   public static int getData(){
     return this.player_health;
   }

   public static void setData(int d){
     this.player_health=d;
   }

    public static Globals getInstance() {
        return singleton;
   } 

    @Override
    public void onCreate()
    {
    super.onCreate();
    player_health=10;
    }
}

现在访问您的变量使用Globals.getInstance().getData();

于 2013-08-30T04:10:37.453 回答