我有一個主要活動,它有選項卡主機。我將tabhost類充氣到我的主要活動中。從另一個類和notifyDataSetChanced調用內部類NullPointerException
我的問題是,其中一個標籤有一個gridview。當我點擊該選項卡時,其課程獲得NullPointerException
。
我的主要活動的卷軸到達底部,我稱爲提供asyc任務數據的內部類,但notifyDataSetChanged
給出了例外。
這裏是logcat。
04-07 14:36:51.520: E/AndroidRuntime(8503): FATAL EXCEPTION: main
04-07 14:36:51.520: E/AndroidRuntime(8503): java.lang.NullPointerException
04-07 14:36:51.520: E/AndroidRuntime(8503): at com.example.profile.Tab2$GetDataTask.onPostExecute(Tab2.java:176)
04-07 14:36:51.520: E/AndroidRuntime(8503): at com.example.profile.Tab2$GetDataTask.onPostExecute(Tab2.java:1)
04-07 14:36:51.520: E/AndroidRuntime(8503): at android.os.AsyncTask.finish(AsyncTask.java:631)
04-07 14:36:51.520: E/AndroidRuntime(8503): at android.os.AsyncTask.access$600(AsyncTask.java:177)
04-07 14:36:51.520: E/AndroidRuntime(8503): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
04-07 14:36:51.520: E/AndroidRuntime(8503): at android.os.Handler.dispatchMessage(Handler.java:99)
04-07 14:36:51.520: E/AndroidRuntime(8503): at android.os.Looper.loop(Looper.java:176)
04-07 14:36:51.520: E/AndroidRuntime(8503): at android.app.ActivityThread.main(ActivityThread.java:5419)
04-07 14:36:51.520: E/AndroidRuntime(8503): at java.lang.reflect.Method.invokeNative(Native Method)
04-07 14:36:51.520: E/AndroidRuntime(8503): at java.lang.reflect.Method.invoke(Method.java:525)
04-07 14:36:51.520: E/AndroidRuntime(8503): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
04-07 14:36:51.520: E/AndroidRuntime(8503): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
04-07 14:36:51.520: E/AndroidRuntime(8503): at dalvik.system.NativeStart.main(Native Method)
這是我調用類主要活動代碼:
Tab2 newObj = new Tab2(); newObj.LoadMore();
//
這裏是我的標籤類代碼:
public class GetDataTask extends AsyncTask<Void, Void, List<String>> {
@Override
protected List<String> doInBackground(Void... params) {
// Simulates a background job.
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
return products_x220;
}
@Override
protected void onPostExecute(List<String> result) {
products_x220.addAll(GetDataService.List_x220);
Products_imgAdapter.notifyDataSetChanged(); // this line gives error.
super.onPostExecute(result);
}
}
TAB2類的整個代碼:
公共類Tab2擴展片段{
List<String> products_x220 = new ArrayList<String>();
ImageAdapter Products_imgAdapter;
DisplayImageOptions options;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_stub)
.showImageForEmptyUri(R.drawable.ic_empty)
.showImageOnFail(R.drawable.ic_error)
.cacheInMemory(false)
.cacheOnDisk(true)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.ARGB_8888)
.build();
new GetDataService();
new GetDataTask().execute();
products_x220.addAll(GetDataService.List_x220);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.collectionproducts_layout, container, false);
ExpandableHeightGridView mGridViewProducts = (ExpandableHeightGridView)v.findViewById(R.id.gridview_products);
mGridViewProducts.setExpanded(true);
Products_imgAdapter = new ImageAdapter();
mGridViewProducts.setAdapter(Products_imgAdapter);
loadMoreBtn = (Button)v.findViewById(R.id.loadMoreBtn);
loadMoreBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
GetDataService.List_x220.clear();
new GetDataService();
new GetDataTask().execute();
}
});
return v;
}
@Override
public void setUserVisibleHint(boolean visible)
{
super.setUserVisibleHint(visible);
if (visible && isResumed())
{
onResume();
}
}
@Override
public void onResume()
{
super.onResume();
if (!getUserVisibleHint())
{
isVisible = false;
return;
}
Tab1.isVisible = false;
isVisible = true;
}
public void LoadMore(){
new GetDataService();
new GetDataTask().execute();
}
public class GetDataTask extends AsyncTask<Void, Void, List<String>> {
@Override
protected List<String> doInBackground(Void... params) {
// Simulates a background job.
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
return products_x220;
}
@Override
protected void onPostExecute(List<String> result) {
products_x220.addAll(GetDataService.List_x220);
Products_imgAdapter.notifyDataSetChanged();
super.onPostExecute(result);
}
}
public class ImageAdapter extends BaseAdapter {
private LayoutInflater inflater;
ImageLoader imageLoader = ImageLoader.getInstance();
ImageAdapter() {
inflater = LayoutInflater.from(getActivity());
}
@Override
public int getCount() {
return products_x220.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder2 holder2;
View view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.item_grid_products, parent, false);
holder2 = new ViewHolder2();
assert view != null;
holder2.imageView = (ImageView) view.findViewById(R.id.image);
holder2.progressBar = (ProgressBar) view.findViewById(R.id.progress);
view.setTag(holder2);
} else {
holder2 = (ViewHolder2) view.getTag();
}
ImageLoader.getInstance()
.displayImage(products_x220.get(position), holder2.imageView, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
holder2.progressBar.setProgress(0);
holder2.progressBar.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
holder2.progressBar.setVisibility(View.GONE);
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
holder2.progressBar.setVisibility(View.GONE);
}
}, new ImageLoadingProgressListener() {
@Override
public void onProgressUpdate(String imageUri, View view, int current, int total) {
holder2.progressBar.setProgress(Math.round(100.0f * current/total));
}
});
return view;
}
}
static class ViewHolder2 {
ImageView imageView;
ProgressBar progressBar;
}
}
請爲什麼我收到NullPointerException異常。 謝謝大家。
帖子中LoadMore()方法來實現 – Pankaj
檢查編輯之類的代碼,將其請。我加了班。 – newComer
爲什麼你要調用LoadMore()方法。不要直接調用它。在setAdapter()方法後,從onCreateView()方法調用它。 – Pankaj