我有一个主要活动,在用户打开应用时加载三个标签。所有选项卡都有从外部数据库填充的ListView,因此下载和显示所有内容需要几秒钟。当然,您无需等待下载所有数据即可使用该应用程序。
有时,我在这一行得到一个NullPointerException:
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
这似乎只有在我下载所有内容之前退出应用程序时才会发生,但并非总是如此,例如3个中的1个。但是,这可能不对,因为如下所示,我检查是否有任何数据在调用适配器之前下载,结果总是相同的大小(7),这意味着下载成功,但由于某种原因,LayoutInflater变为null。
DownloadAsynctask:
public class DownloadNewsfeed extends AsyncTask<String, Void, ArrayList<String>> {
@Override
protected void onPostExecute(ArrayList<String> result) {
Log.i("SIZE", result.size() + "");
if (result.size() == 0) {
(...)
} else {
myadapter = new MyAdapter(getActivity(), arr_userid, arr_event, arr_subject, arr_date, arr_result, arr_username, arr_photo, arr_timediff);
lv.setAdapter(myadapter);
}
}
@Override
protected void onPreExecute() {
(...)
return arr_userid;
}
@Override
protected ArrayList<String> doInBackground(String... params) {
(...)
}
}
适配器
public class MyAdapter extends BaseAdapter
{
public Context context;
ArrayList<String> arr_userid = new ArrayList<String>();
ArrayList<String> arr_event = new ArrayList<String>();
ArrayList<String> arr_subject = new ArrayList<String>();
ArrayList<String> arr_date = new ArrayList<String>();
ArrayList<String> arr_result = new ArrayList<String>();
ArrayList<String> arr_username = new ArrayList<String>();
ArrayList<String> arr_photo = new ArrayList<String>();
ArrayList<String> arr_timediff = new ArrayList<String>();
public LayoutInflater inflater;
public MyAdapter(Context context, ArrayList<String> arr_userid, ArrayList<String> arr_event, ArrayList<String> arr_subject,
ArrayList<String> arr_date, ArrayList<String> arr_result, ArrayList<String> arr_username, ArrayList<String> arr_photo,
ArrayList<String> arr_timediff) {
super();
this.context = context;
this.arr_userid = arr_userid;
this.arr_event = arr_event;
this.arr_subject = arr_subject;
this.arr_date = arr_date;
this.arr_result = arr_result;
this.arr_username = arr_username;
this.arr_photo = arr_photo;
this.arr_timediff = arr_timediff;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); //**NULLPOINTER**
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return arr_userid.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public class ViewHolder
{
ImageView image;
TextView tv_name, tv_date, tv_text, tv_text2;
LinearLayout row;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if(convertView==null)
{
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.newsfeed_listitem_new, null);
holder.image = (ImageView) convertView.findViewById(R.id.iv_user);
holder.tv_text = (TextView) convertView.findViewById(R.id.tv_text);
holder.tv_text2 = (TextView) convertView.findViewById(R.id.tv_text2);
holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
holder.tv_date = (TextView) convertView.findViewById(R.id.tv_date);
holder.row = (LinearLayout) convertView.findViewById(R.id.lineItem);
convertView.setTag(holder);
}
else
holder=(ViewHolder)convertView.getTag();
holder.tv_text.setText(arr_event.get(position) + ":");
holder.tv_text2.setText(arr_result.get(position));
return convertView;
}
}
答案 0 :(得分:0)
退出应用后,再做任何工作都没有意义。活动销毁后getActivity()
将返回null
。在继续之前执行空检查:
@Override
protected void onPostExecute(ArrayList<String> result) {
if (getActivity() == null) {
Log.e(getClass().getSimpleName(), "Activity ended prematurely.");
return;
}
Log.i("SIZE", result.size() + "");
if (result.size() == 0) {
(...)
} else {
myadapter = new MyAdapter(getActivity(), arr_userid, arr_event, arr_subject, arr_date, arr_result, arr_username, arr_photo, arr_timediff);
lv.setAdapter(myadapter);
}
}
无关注意:请考虑为新闻源项目创建一个持有者,例如:
public class NewsfeedItem {
String userid;
String event;
String subject;
String date;
String result;
String username;
String photo;
String timediff;
public NewsfeedItem() {}
}
这将使您免于获得大量ArrayList
s而只支持一个。