我创建了这样扩展应用程序的类
package com.appnetics;
import java.util.ArrayList;
import android.app.Application;
public class GlobalVariables extends Application {
public ArrayList<Encounter> encounters;
}
并将其设置在清单中,如下所示
<application
android:name="GlobalVariables"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
在一个活动中,我使用此代码填充该数组
GlobalVariables appState = new GlobalVariables();
appState.encounters =new ArrayList<Encounter>();
................
then add
当尝试使用它时,它会因 null 异常而崩溃,我在另一个类似的活动或类中使用它
GlobalVariables appState = new GlobalVariables();
Encounter EncounterObject = appState.encounters.get(position);
请问有什么东西丢失了吗,如何修复
It's an instance的变量GlobalVariables
类,所以每次你创建一个新的instance of GlobalVariables
,你会得到一个单独的变量。
If you really想要一个全局变量,它应该是static变量 - 但即使这样也只有在同一流程中的活动之间共享数据时才有效。您应该阅读Android 应用程序框架常见问题解答了解更多信息。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)