考虑到我的这个Previous question问题,我仍然无法找到,为什么wifi状态更改导致应用程序崩溃,在我的应用程序处于onreume状态时从启动器返回,它在简历上更改为oncreate,并且app get坠毁了,我试图在我的第二个活动的onstop上保存一个id在sharedPref,但仍然崩溃..
这是logcat:
12-05 17:14:39.966: E/AndroidRuntime(29449): FATAL EXCEPTION: main
12-05 17:14:39.966: E/AndroidRuntime(29449): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Pakagename/com.Pakagename.subCategories.SubCategoriesActivity}: java.lang.NullPointerException
12-05 17:14:39.966: E/AndroidRuntime(29449): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
12-05 17:14:39.966: E/AndroidRuntime(29449): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
12-05 17:14:39.966: E/AndroidRuntime(29449): at android.app.ActivityThread.access$600(ActivityThread.java:139)
12-05 17:14:39.966: E/AndroidRuntime(29449): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
12-05 17:14:39.966: E/AndroidRuntime(29449): at android.os.Handler.dispatchMessage(Handler.java:99)
12-05 17:14:39.966: E/AndroidRuntime(29449): at android.os.Looper.loop(Looper.java:156)
12-05 17:14:39.966: E/AndroidRuntime(29449): at android.app.ActivityThread.main(ActivityThread.java:4977)
12-05 17:14:39.966: E/AndroidRuntime(29449): at java.lang.reflect.Method.invokeNative(Native Method)
12-05 17:14:39.966: E/AndroidRuntime(29449): at java.lang.reflect.Method.invoke(Method.java:511)
12-05 17:14:39.966: E/AndroidRuntime(29449): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-05 17:14:39.966: E/AndroidRuntime(29449): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-05 17:14:39.966: E/AndroidRuntime(29449): at dalvik.system.NativeStart.main(Native Method)
12-05 17:14:39.966: E/AndroidRuntime(29449): Caused by: java.lang.NullPointerException
12-05 17:14:39.966: E/AndroidRuntime(29449): at com.Pakagename.subCategories.SubCategoriesActivity.getSubCategories(SubCategoriesActivity.java:119)
12-05 17:14:39.966: E/AndroidRuntime(29449): at com.Pakagename.subCategories.SubCategoriesActivity.onCreate(SubCategoriesActivity.java:90)
12-05 17:14:39.966: E/AndroidRuntime(29449): at android.app.Activity.performCreate(Activity.java:4538)
12-05 17:14:39.966: E/AndroidRuntime(29449): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
12-05 17:14:39.966: E/AndroidRuntime(29449): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
12-05 17:14:39.966: E/AndroidRuntime(29449): ... 11 more
12-05 17:14:39.976: E/EmbeddedLogger(1627): App crashed! Process: com.Pakagename
12-05 17:14:39.976: E/EmbeddedLogger(1627): App crashed! Package: com.Pakagename v2 (1.0)
这是我在创建子类别活动
中所做的事情if(getIntent().hasExtra("categoryid")) {
if(getIntent().getStringExtra("categoryid") != null) {
categoryId = Integer.parseInt(getIntent().getStringExtra("categoryid"));
getSubCategories(categoryId);
}
} else {
getSubCategories(categoryIdFromPref); // getting an id from pref, that iam saving in onstop
}
和getSubCategories方法,
private void getSubCategories(int categoryId) {
if(categoryId!=0) {
query = "SELECT * FROM sub_categories WHERE category_id = "+categoryId+" AND is_archived = 0";
Log.d("subCategory query", query);
c = MainActivity.db.rawQuery(query, null);
c.moveToFirst();
}
}
检查后,logcat带我到这条线,
c = MainActivity.db.rawQuery(query, null);
我的清单看起来像,
<uses-permission android:name="com.android.vending.BILLING" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
<activity
android:name="com.Pakagename.MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="com.Pakagename.SplashScreenActivity"
android:parentActivityName="com.Pakagename.MainActivity"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:screenOrientation="portrait"
android:value="com.Pakagename.MainActivity" />
</activity>
<activity
android:name="com.Pakagename.subCategories.SubCategoriesActivity"
android:screenOrientation="portrait"
</activity>
答案 0 :(得分:1)
db
内的静态变量MainActivity
为空。这会导致nullpointer。如果你解决了这个问题,错误也会得到解决。最好不要在MainActivity中使用静态db
变量,而是在SubCategoriesActivity
内重新打开数据库。
答案 1 :(得分:0)
您的错误是NullPointerException,您应该检查该变量是不是inicializaste还是null。
start activity ComponentInfo{com.Pakagename/com.Pakagename.subCategories.SubCategoriesActivity}: java.lang.NullPointerException
我希望能帮到你;抱歉我的英语。