Android全局整数不起作用

时间:2013-08-30 03:44:15

标签: android variables integer global

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

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 {

            }
        }
    });

logcat的:

    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

2 个答案:

答案 0 :(得分:0)

第21行中的LevelTwo.java中有NullPointerException,这是在构造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()方法。

答案 1 :(得分:-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();

访问您的变量