您好我在我的活动中运行Asynctask并从服务器获取数据。在onPostExecute
metod我现在在数据库中插入数据我想在两个不同的片段中传递这些ID我通过以下代码传递id:
Bundle bundle = new Bundle();
bundle.putStringArray(BConstant.TAXONOMY_TID, dbtids);
bundle.putStringArray(BConstant.PRODUCT_CATEGORY_NAMES,
dblistProCategory);
ProductCategoryFragment.newInstance(bundle);
// ProductDetailsFragment
Bundle bundleForProductList = new Bundle();
bundleForProductList
.putStringArray(BConstant.TAXONOMY_TID, dbtids);
bundleForProductList.putString(BConstant.WEB_SERVICES_COOKIES,
cookie);
bundleForProductList.putStringArray(
BConstant.PRODUCT_CATEGORY_NAMES, dblistProCategory);
ProductListFragment.newInstance(bundleForProductList);
但是上面的代码行在AsyncTask完成之前执行。但是我已在此代码上面写了这一行new CategoryFrag().execute();
。我该怎么办。
protected void onPostExecute(JSONArray result) {
listItems = new ArrayList<String>();
for (int i = 0; i < result.length(); i++) {
try {
listItems
.add(result.getJSONObject(i)
.getString(BConstant.NAME_CONSTANT)
.toString());
} catch (Exception ex) {
ex.printStackTrace();
}
}
listProCategory = listItems.toArray(new String[0]);
returnsTID = saDatabaseHandler.getProductCategoryTID();
for (int i = 0; i < tids.length; i++) {
if (!returnsTID.contains(tids[i])) {
saDatabaseHandler.insertProductCategoryData(
tids[i], listProCategory[i]);
}
}
getOfflineData();
}
logcat的:
02-02 11:41:00.451: E/AndroidRuntime(20901): FATAL EXCEPTION: main
02-02 11:41:00.451: E/AndroidRuntime(20901): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abc.xyz/com.abc.xyz.ProductListActivity}: java.lang.NullPointerException
02-02 11:41:00.451: E/AndroidRuntime(20901): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2114)
02-02 11:41:00.451: E/AndroidRuntime(20901): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
02-02 11:41:00.451: E/AndroidRuntime(20901): at android.app.ActivityThread.access$700(ActivityThread.java:143)
02-02 11:41:00.451: E/AndroidRuntime(20901): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
02-02 11:41:00.451: E/AndroidRuntime(20901): at android.os.Handler.dispatchMessage(Handler.java:99)
02-02 11 :41:00.451: E/AndroidRuntime(20901): at android.os.Looper.loop(Looper.java:137)
02-02 11:41:00.451: E/AndroidRuntime(20901): at android.app.ActivityThread.main(ActivityThread.java:4960)
02-02 11:41:00.451: E/AndroidRuntime(20901): at java.lang.reflect.Method.invokeNative(Native Method)
02-02 11:41:00.451: E/AndroidRuntime(20901): at java.lang.reflect.Method.invoke(Method.java:511)
02-02 11:41:00.451: E/AndroidRuntime(20901): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
02-02 11:41:00.451: E/AndroidRuntime(20901): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
02-02 11:41:00.451: E/AndroidRuntime(20901): at dalvik.system.NativeStart.main(Native Method)
02-02 11:41:00.451: E/AndroidRuntime(20901): Caused by: java.lang.NullPointerException
02-02 11:41:00.451: E/AndroidRuntime(20901): at java.util.Arrays$ArrayList.<init>(Arrays.java:38)
02-02 11:41:00.451: E/AndroidRuntime(20901): at java.util.Arrays.asList(Arrays.java:154)
02-02 11:41:00.451: E/AndroidRuntime(20901): at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:141)
02-02 11:41:00.451: E/AndroidRuntime(20901): at com.abc.xyz.ProductCategoryFragment$DisplayProductCategoryListArrayAdapter.<init>(ProductCategoryFragment.java:175)
02-02 11:41:00.451: E/AndroidRuntime(20901): at com.abc.xyz.ProductCategoryFragment.onActivityCreated(ProductCategoryFragment.java:113)
02-02 11:41:00.451: E/AndroidRuntime(20901): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:848)
02-02 11:41:00.451: E/AndroidRuntime(20901): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
02-02 11:41:00.451: E/AndroidRuntime(20901): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1017)
02-02 11:41:00.451: E/AndroidRuntime(20901): at android.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1804)
02-02 11:41:00.451: E/AndroidRuntime(20901): at android.app.Activity.performCreate(Activity.java:5206)
02-02 11:41:00.451: E/AndroidRuntime(20901): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
02-02 11:41:00.451: E/AndroidRuntime(20901): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2078)
02-02 11:41:00.451: E/AndroidRuntime(20901): ... 11 more
答案 0 :(得分:0)
使用onPostExecute
方法调用活动中定义的方法。并在该方法中编写上述代码。
如果异步任务在另一个类中,则从异步任务传递上下文,并在从数据库获取数据后的onPostExecute
方法中执行此操作:
((YourCallingActivity)mContext).methodName();