我有一个主要活动,它有标签主机。我将tabhost类膨胀到我的主要活动中。
我的问题是其中一个标签有一个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扩展了Fragment {
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。 谢谢大家。
答案 0 :(得分:0)
在调用GetDataTask
异步任务时,适配器不存在,因此,在PostExecute
上,适配器尚不存在 - 因此您的选项卡未获取适配器的对象....
答案 1 :(得分:0)
你应该在setAdapter之后调用asyncTask。只需删除
new GetDataTask().execute();
来自oncreate并在
之后设置mGridViewProducts.setAdapter(Products_imgAdapter);