Hi I'm running the Asynctask in my activity and getting the data from the server. In onPostExecute
metod I'm inserting the data in Database now I want to pass those ids in two different fragments I'm passing the the ids by following code:
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);
But above lines of code get executed before the AsyncTask fininshed. but i have written this line new CategoryFrag().execute();
above this code. what should i do.
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
call a method defined in your activity from your onPostExecute
method. And write the above code inside that method.
If the async task is in another class, the pass context to the async task, and in the onPostExecute
method after fetching data from the db, do this:
((YourCallingActivity)mContext).methodName();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.