当我从Eclipse安装我的应用程序到模拟器时,我有时在日志“线程退出未捕获的异常”中收到错误。其他SO帖子指的是这个,但“原因”通常是由开发者构建的活动。我的原因似乎是一些Android类。我已经尝试了Project> Clean,但这并没有改变任何东西。此外,即使出现此错误,该应用程序运行正常。它的外观似乎没有任何押韵或理由。想法?
登录:
04-09 21:22:49.487: W/dalvikvm(1462): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
04-09 21:22:49.497: E/AndroidRuntime(1462): FATAL EXCEPTION: main
04-09 21:22:49.497: E/AndroidRuntime(1462): java.lang.RuntimeException: Unable to instantiate application com.example.blobtag2.GlobalAppContext: java.lang.NullPointerException
04-09 21:22:49.497: E/AndroidRuntime(1462): at android.app.LoadedApk.makeApplication(LoadedApk.java:501)
04-09 21:22:49.497: E/AndroidRuntime(1462): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4124)
04-09 21:22:49.497: E/AndroidRuntime(1462): at android.app.ActivityThread.access$1300(ActivityThread.java:130)
04-09 21:22:49.497: E/AndroidRuntime(1462): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1255)
04-09 21:22:49.497: E/AndroidRuntime(1462): at android.os.Handler.dispatchMessage(Handler.java:99)
04-09 21:22:49.497: E/AndroidRuntime(1462): at android.os.Looper.loop(Looper.java:137)
04-09 21:22:49.497: E/AndroidRuntime(1462): at android.app.ActivityThread.main(ActivityThread.java:4745)
04-09 21:22:49.497: E/AndroidRuntime(1462): at java.lang.reflect.Method.invokeNative(Native Method)
04-09 21:22:49.497: E/AndroidRuntime(1462): at java.lang.reflect.Method.invoke(Method.java:511)
04-09 21:22:49.497: E/AndroidRuntime(1462): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-09 21:22:49.497: E/AndroidRuntime(1462): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-09 21:22:49.497: E/AndroidRuntime(1462): at dalvik.system.NativeStart.main(Native Method)
04-09 21:22:49.497: E/AndroidRuntime(1462): Caused by: java.lang.NullPointerException
04-09 21:22:49.497: E/AndroidRuntime(1462): at android.app.LoadedApk.initializeJavaContextClassLoader(LoadedApk.java:377)
04-09 21:22:49.497: E/AndroidRuntime(1462): at android.app.LoadedApk.getClassLoader(LoadedApk.java:320)
04-09 21:22:49.497: E/AndroidRuntime(1462): at android.app.LoadedApk.makeApplication(LoadedApk.java:493)
04-09 21:22:49.497: E/AndroidRuntime(1462): ... 11 more
编辑 - 清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.blobtag2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".GlobalAppContext"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<service android:name=".service.RESTService" />
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ResultsActivity"
android:label="@string/title_activity_results_activity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.blobtag2.MainActivity" />
</activity>
<activity
android:name=".PlaceActivity"
android:label="@string/title_activity_place_activity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.blobtag2.ResultsActivity" />
</activity>
</application>
</manifest>
globalappcontext代码:
public class GlobalAppContext extends Application {
private static VenueList venueList;
@Override
public void onCreate() {
venueList = new VenueList();
}
public static VenueList getVenueList() {
return venueList;
}
/*do I still need this, since I have it in the venueList class?
public static void setPlaceName(String s) {
venueList = s;
}
**/
}
答案 0 :(得分:0)
将GlobalAppContext.java更改为:
public class GlobalAppContext {
private static VenueList mVenueList = null;
//----------------------------
// Uses the Singleton method of Bill Pugh.
// https://en.wikipedia.org/wiki/Singleton_pattern
//----------------------------
// --- Singleton code
private GlobalAppContext() { }
private static class GlobalAppContextHolder {
public static final GlobalAppContext INSTANCE = new GlobalAppContext();
}
public static GlobalAppContext getInstance() {
if (mVenueList == null) {
mVenueList = new VenueList();
}
return GlobalAppContext.INSTANCE;
}
// --- End Singleton code
public static VenueList getVenueList() {
return mVenueList;
}
}
并使用以下内容调用此全局类:
... snip ....
private static GlobalAppContext mGlobal = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (mGlobal == null) {
mGlobal = GlobalAppContext.getInstance();
}
setContentView(R.layout.your_layout);
}
现在您可以像这样访问“VenueList”:
VenueList venueList = mGlobal.getVenueList();