我正在使用ActionBarSherlock来使用viewpager呈现标签。每个选项卡都有自己的片段。 还使用回调接口创建了一个外部异步任务,因此我可以多次使用它。
成功输出响应但是当调用asynctask到fragment时,它会抛出nullpointerexception。我试图从View中引用LinearLayout并动态添加更多视图。 这是我关于StackOverFlow的第一个问题,请告诉我任何其他细节。
LogCat错误:
08-01 11:02:00.364: W/System.err(23359): java.lang.NullPointerException
08-01 11:02:00.364: W/System.err(23359): at in.clairvoyant.comicswala.JustAddedFragment.onTaskComplete(JustAddedFragment.java:78)
08-01 11:02:00.364: W/System.err(23359): at in.clairvoyant.utils.AsyncTask.onPostExecute(AsyncTask.java:54)
08-01 11:02:00.364: W/System.err(23359): at android.os.AsyncTask.finish(AsyncTask.java:631)
08-01 11:02:00.364: W/System.err(23359): at android.os.AsyncTask.access$600(AsyncTask.java:177)
08-01 11:02:00.364: W/System.err(23359): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
08-01 11:02:00.364: W/System.err(23359): at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 11:02:00.364: W/System.err(23359): at android.os.Looper.loop(Looper.java:137)
08-01 11:02:00.364: W/System.err(23359): at android.app.ActivityThread.main(ActivityThread.java:4744)
08-01 11:02:00.364: W/System.err(23359): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 11:02:00.364: W/System.err(23359): at java.lang.reflect.Method.invoke(Method.java:511)
08-01 11:02:00.364: W/System.err(23359): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-01 11:02:00.364: W/System.err(23359): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-01 11:02:00.364: W/System.err(23359): at dalvik.system.NativeStart.main(Native Method)
08-01 11:02:00.364: W/System.err(23359): java.lang.NullPointerException
08-01 11:02:00.364: W/System.err(23359): at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
08-01 11:02:00.364: W/System.err(23359): at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:153)
08-01 11:02:00.364: W/System.err(23359): at in.clairvoyant.comicswala.FeaturedGridViewAdapter.<init>(FeaturedGridViewAdapter.java:23)
08-01 11:02:00.364: W/System.err(23359): at in.clairvoyant.comicswala.JustAddedFragment.onTaskComplete(JustAddedFragment.java:115)
08-01 11:02:00.364: W/System.err(23359): at in.clairvoyant.utils.AsyncTask.onPostExecute(AsyncTask.java:54)
08-01 11:02:00.364: W/System.err(23359): at android.os.AsyncTask.finish(AsyncTask.java:631)
08-01 11:02:00.364: W/System.err(23359): at android.os.AsyncTask.access$600(AsyncTask.java:177)
08-01 11:02:00.374: W/System.err(23359): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
08-01 11:02:00.374: W/System.err(23359): at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 11:02:00.374: W/System.err(23359): at android.os.Looper.loop(Looper.java:137)
08-01 11:02:00.374: W/System.err(23359): at android.app.ActivityThread.main(ActivityThread.java:4744)
08-01 11:02:00.374: W/System.err(23359): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 11:02:00.374: W/System.err(23359): at java.lang.reflect.Method.invoke(Method.java:511)
08-01 11:02:00.374: W/System.err(23359): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-01 11:02:00.374: W/System.err(23359): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-01 11:02:00.374: W/System.err(23359): at dalvik.system.NativeStart.main(Native Method)
AsyncTaskClass下面:
public class AsyncTask extends android.os.AsyncTask<String, Integer, Object> {
private Context context;
private AsyncTaskListener<Object> listener;
private Object mobj;
String response = "{\"success\":\"true\",\"bannerList\":[{\"bannerId\":1,\"bannerURL\":\"http://guardianlv.com/wp-content/uploads/2014/05/Katrina-Kaif-Is-Dating-Ranbir-Kapoor.jpg\",\"bannerContentType\":1,\"productContentID\":1},{\"bannerId\":2,\"bannerURL\":\"http://guardianlv.com/wp-content/uploads/2014/05/Katrina-Kaif-Is-Dating-Ranbir-Kapoor.jpg\",\"bannerContentType\":2,\"productContentID\":2},{\"bannerId\":3,\"bannerURL\":\"http://guardianlv.com/wp-content/uploads/2014/05/Katrina-Kaif-Is-Dating-Ranbir-Kapoor.jpg\",\"bannerContentType\":2,\"productContentID\":3},{\"bannerId\":4,\"bannerURL\":\"http://guardianlv.com/wp-content/uploads/2014/05/Katrina-Kaif-Is-Dating-Ranbir-Kapoor.jpg\",\"bannerContentType\":4,\"productContentID\":4}],\"categories\":[{\"catName\":\"Recomended\",\"catType\":1,\"catId\":1,\"productCount\":13,\"products\":[{\"productId\":1,\"productName\":\"ChachaChoudharyKaDanda\",\"publishesName\":\"Diamond\",\"productType\":1,\"productSeries\":\"ChachaChoudhary\",\"units\":35,\"actualPrice\":20,\"discountedPrice\":10,\"discountPercentage\":50,\"offerName\":\"Recommended\",\"offerId\":1,\"imageURL\":\"http://guardianlv.com/wp-content/uploads/2014/05/Katrina-Kaif-Is-Dating-Ranbir-Kapoor.jpg\",\"rating\":3,\"downloads\":2,\"language\":1,\"genre\":[{\"genreId\":1},{\"genreId\":2}]},{\"productId\":2,\"productName\":\"ChachaChoudharyAndRocket\",\"publishesName\":\"Diamond\",\"productType\":1,\"productSeries\":\"ChachaChoudhary\",\"units\":35,\"actualPrice\":20,\"discountedPrice\":20,\"discountPercentage\":50,\"offerName\":\"Recommended\",\"offerId\":1,\"imageURL\":\"http://guardianlv.com/wp-content/uploads/2014/05/Katrina-Kaif-Is-Dating-Ranbir-Kapoor.jpg\",\"rating\":2,\"downloads\":12,\"language\":1,\"genre\":[{\"genreId\":1},{\"genreId\":2}]},{\"productId\":3,\"productName\":\"ChachaChoudharyAndRaka\",\"publishesName\":\"Diamond\",\"productType\":1,\"productSeries\":\"ChachaChoudhary\",\"units\":25,\"actualPrice\":50,\"discountedPrice\":25,\"discountPercentage\":50,\"offerName\":\"NewLaunch\",\"offerId\":1,\"imageURL\":\"http://guardianlv.com/wp-content/uploads/2014/05/Katrina-Kaif-Is-Dating-Ranbir-Kapoor.jpg\",\"rating\":3,\"downloads\":2,\"language\":1,\"genre\":[{\"genreId\":1},{\"genreId\":2}]}]}],\"categories\":[{\"catName\":\"SpecialPackage\",\"catType\":2,\"catId\":2,\"productCount\":19,\"products\":[{\"productId\":20,\"productName\":\"ChachaChoudhary\",\"publishesName\":\"Diamond\",\"productType\":2,\"productSeries\":\"ChachaChoudhary\",\"units\":12,\"actualPrice\":30,\"discountedPrice\":25,\"discountPercentage\":25,\"offerName\":\"Recommended\",\"offerId\":1,\"imageURL\":\"http://guardianlv.com/wp-content/uploads/2014/05/Katrina-Kaif-Is-Dating-Ranbir-Kapoor.jpg\",\"rating\":3,\"downloads\":2,\"language\":1,\"genre\":[{\"genreId\":1},{\"genreId\":2}]},{\"productId\":21,\"productName\":\"PinkiSeries\",\"publishesName\":\"Diamond\",\"productType\":2,\"productSeries\":\"Pinki\",\"units\":5,\"actualPrice\":20,\"discountedPrice\":15,\"discountPercentage\":25,\"offerName\":\"Recommended\",\"offerId\":1,\"imageURL\":\"http://guardianlv.com/wp-content/uploads/2014/05/Katrina-Kaif-Is-Dating-Ranbir-Kapoor.jpg\",\"rating\":2,\"downloads\":12,\"language\":1,\"genre\":[{\"genreId\":1},{\"genreId\":2}]},{\"productId\":22,\"productName\":\"BabluandRocket\",\"publishesName\":\"Diamond\",\"productType\":2,\"productSeries\":\"Bablu\",\"units\":11,\"actualPrice\":20,\"discountedPrice\":15,\"discountPercentage\":25,\"offerName\":\"ExclusiveLaunch\",\"offerId\":2,\"imageURL\":\"http://guardianlv.com/wp-content/uploads/2014/05/Katrina-Kaif-Is-Dating-Ranbir-Kapoor.jpg\",\"rating\":3,\"downloads\":2,\"language\":1,\"genre\":[{\"genreId\":1},{\"genreId\":2}]}]}]}";
public AsyncTask(Context context2, Object object,
AsyncTaskListener<Object> listener) {
this.context = context2;
this.mobj = object;
this.listener = listener;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Object doInBackground(String... params) {
Gson gson = new Gson();
try {
mobj = gson.fromJson(response, mobj.getClass());
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Response in string>>> " + mobj.toString());
return mobj;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(Object result) {
System.out.println("INSIDE POST EXECUTE METHOD>>>>>> "+result.toString() );
try {
if(null != listener) {
listener.onTaskComplete(result);
} else {
System.out.println("Listener is Null");
}
System.out.println("API response>>" + result.toString());
} catch (Exception e) {
e.printStackTrace();
}
super.onPostExecute(result);
}
}
JustAddedFragment Class:
public class JustAddedFragment extends SherlockFragment implements AsyncTaskListener<Object> {
// response string temporary
String response = "{\"success\":\"true\",\"bannerList\":[{\"bannerId\":1,\"bannerURL\":\"http://guardianlv.com/wp-content/uploads/2014/05/Katrina-Kaif-Is-Dating-Ranbir-Kapoor.jpg\",\"bannerContentType\":1,\"productContentID\":1},{\"bannerId\":2,\"bannerURL\":\"http://guardianlv.com/wp-content/uploads/2014/05/Katrina-Kaif-Is-Dating-Ranbir-Kapoor.jpg\",\"bannerContentType\":2,\"productContentID\":2},{\"bannerId\":3,\"bannerURL\":\"http://guardianlv.com/wp-content/uploads/2014/05/Katrina-Kaif-Is-Dating-Ranbir-Kapoor.jpg\",\"bannerContentType\":2,\"productContentID\":3},{\"bannerId\":4,\"bannerURL\":\"http://guardianlv.com/wp-content/uploads/2014/05/Katrina-Kaif-Is-Dating-Ranbir-Kapoor.jpg\",\"bannerContentType\":4,\"productContentID\":4}],\"categories\":[{\"catName\":\"Recomended\",\"catType\":1,\"catId\":1,\"productCount\":13,\"products\":[{\"productId\":1,\"productName\":\"ChachaChoudharyKaDanda\",\"publishesName\":\"Diamond\",\"productType\":1,\"productSeries\":\"ChachaChoudhary\",\"units\":35,\"actualPrice\":20,\"discountedPrice\":10,\"discountPercentage\":50,\"offerName\":\"Recommended\",\"offerId\":1,\"imageURL\":\"http://guardianlv.com/wp-content/uploads/2014/05/Katrina-Kaif-Is-Dating-Ranbir-Kapoor.jpg\",\"rating\":3,\"downloads\":2,\"language\":1,\"genre\":[{\"genreId\":1},{\"genreId\":2}]},{\"productId\":2,\"productName\":\"ChachaChoudharyAndRocket\",\"publishesName\":\"Diamond\",\"productType\":1,\"productSeries\":\"ChachaChoudhary\",\"units\":35,\"actualPrice\":20,\"discountedPrice\":20,\"discountPercentage\":50,\"offerName\":\"Recommended\",\"offerId\":1,\"imageURL\":\"http://guardianlv.com/wp-content/uploads/2014/05/Katrina-Kaif-Is-Dating-Ranbir-Kapoor.jpg\",\"rating\":2,\"downloads\":12,\"language\":1,\"genre\":[{\"genreId\":1},{\"genreId\":2}]},{\"productId\":3,\"productName\":\"ChachaChoudharyAndRaka\",\"publishesName\":\"Diamond\",\"productType\":1,\"productSeries\":\"ChachaChoudhary\",\"units\":25,\"actualPrice\":50,\"discountedPrice\":25,\"discountPercentage\":50,\"offerName\":\"NewLaunch\",\"offerId\":1,\"imageURL\":\"http://guardianlv.com/wp-content/uploads/2014/05/Katrina-Kaif-Is-Dating-Ranbir-Kapoor.jpg\",\"rating\":3,\"downloads\":2,\"language\":1,\"genre\":[{\"genreId\":1},{\"genreId\":2}]}]}],\"categories\":[{\"catName\":\"SpecialPackage\",\"catType\":2,\"catId\":2,\"productCount\":19,\"products\":[{\"productId\":20,\"productName\":\"ChachaChoudhary\",\"publishesName\":\"Diamond\",\"productType\":2,\"productSeries\":\"ChachaChoudhary\",\"units\":12,\"actualPrice\":30,\"discountedPrice\":25,\"discountPercentage\":25,\"offerName\":\"Recommended\",\"offerId\":1,\"imageURL\":\"http://guardianlv.com/wp-content/uploads/2014/05/Katrina-Kaif-Is-Dating-Ranbir-Kapoor.jpg\",\"rating\":3,\"downloads\":2,\"language\":1,\"genre\":[{\"genreId\":1},{\"genreId\":2}]},{\"productId\":21,\"productName\":\"PinkiSeries\",\"publishesName\":\"Diamond\",\"productType\":2,\"productSeries\":\"Pinki\",\"units\":5,\"actualPrice\":20,\"discountedPrice\":15,\"discountPercentage\":25,\"offerName\":\"Recommended\",\"offerId\":1,\"imageURL\":\"http://guardianlv.com/wp-content/uploads/2014/05/Katrina-Kaif-Is-Dating-Ranbir-Kapoor.jpg\",\"rating\":2,\"downloads\":12,\"language\":1,\"genre\":[{\"genreId\":1},{\"genreId\":2}]},{\"productId\":22,\"productName\":\"BabluandRocket\",\"publishesName\":\"Diamond\",\"productType\":2,\"productSeries\":\"Bablu\",\"units\":11,\"actualPrice\":20,\"discountedPrice\":15,\"discountPercentage\":25,\"offerName\":\"ExclusiveLaunch\",\"offerId\":2,\"imageURL\":\"http://guardianlv.com/wp-content/uploads/2014/05/Katrina-Kaif-Is-Dating-Ranbir-Kapoor.jpg\",\"rating\":3,\"downloads\":2,\"language\":1,\"genre\":[{\"genreId\":1},{\"genreId\":2}]}]}]}";
String jsonString;
FeaturedGridViewAdapter featuredAdapter;
LinearLayout justAddedContainer;
Context context;
View view, row;
GridView featuredGridview;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_justadded_fragment, container, false);
// take the parent container where child views to be added
justAddedContainer = (LinearLayout) view.findViewById(R.id.just_added_container);
context = getActivity();
// inflate the child views (work in a look, lenght will the category count
row = inflater.inflate(R.layout.category_layout, null);
// reference to gridviews per child
featuredGridview = (GridView) row.findViewById(R.id.featured_gridview);
new AsyncTask(context, new FeaturedModel(), new JustAddedFragment()).execute();
return view;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
public void onTaskComplete(Object result) {
System.out.println("INSIDE TASK COMPLETE METHOD>>>> "
+ result.toString());
FeaturedModel featuredModel = new FeaturedModel();
featuredModel = (FeaturedModel) result;
System.out.println("featured model length>>> "
+ featuredModel.toString());
ProductModel productModel = new ProductModel();
ArrayList<ProductModel> data = new ArrayList<ProductModel>();
if (featuredModel.isSuccess()) {
CategoriesModel[] categoriesArray = new CategoriesModel[featuredModel
.getCategories().length];
categoriesArray = featuredModel.getCategories();
int length1 = categoriesArray.length;
System.out.println("Length of Category array : " + length1);
for (int i = 0; i < categoriesArray.length; i++) {
try {
// here getting the nullpointer
**justAddedContainer**.addView(row, i, new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
RobotoTextView categoryName = (RobotoTextView) row
.findViewById(R.id.featured_title);
RobotoTextView seeAll = (RobotoTextView) row
.findViewById(R.id.featured_seeall);
categoryName.setText(categoriesArray[i].getCatName());
} catch (Exception e) {
e.printStackTrace();
}
}
for (int i = 0; i < categoriesArray.length; i++) {
ProductModel[] productsArray = new ProductModel[categoriesArray[i]
.getProducts().length];
productsArray = categoriesArray[i].getProducts();
for (int j = 0; j < productsArray.length; j++) {
productModel.setProductName(productsArray[i]
.getProductName());
productModel.setProductSeries(productsArray[i]
.getProductSeries());
// productModel.setImageURL(productsArray[i].getImageURL());
productModel.setUnits(productsArray[i].getUnits());
productModel.setRating(productsArray[i].getRating());
productModel.setDownloads(productsArray[i].getDownloads());
productModel.setDiscountedPrice(productsArray[i]
.getDiscountedPrice());
productModel.setActualPrice(productsArray[i]
.getActualPrice());
productModel.setDiscountPercentage(productsArray[i]
.getDiscountPercentage());
data.add(productModel);
featuredAdapter = new FeaturedGridViewAdapter(context,
R.layout.product_view, data);
featuredGridview.setAdapter(featuredAdapter);
}
}
} else {
System.out.println("feature model response is false>>");
}
}
}
AsyncTaskListener接口:
public interface AsyncTaskListener<Object> {
public void onTaskComplete(Object result);
}
答案 0 :(得分:0)
new JustAddedFragment()将创建一个新对象,你应该用JustAddedFragment替换它。这可能会解决你的问题。
Better Approach:
您应该在您的活动上实现回调接口,然后将其上下文移动到其他类,然后在完成的操作中,您的活动将被通知,您可以从活动中将结果传递给碎片。
通过调用getActivity()在片段中,您可以获取活动上下文,并可以使用其他操作。