我遇到以下错误的问题:
E / AndroidRuntime(658):致命异常:主要 E / AndroidRuntime(658):java.lang.NullPointerException E / AndroidRuntime(658):在android.view.LayoutInflater.from(LayoutInflater.java:210) E / AndroidRuntime(658):at com.gauss.sportsapp.messages.RecievedRequestsAdapter。(RecievedRequestsAdapter.java:26) E / AndroidRuntime(658):at com.gauss.sportsapp.messages.MessagesList.setRecievedRequests(MessagesList.java:157) E / AndroidRuntime(658):at com.gauss.sportsapp.messages.MessagesList.onEventsAvailable(MessagesList.java:181) E / AndroidRuntime(658):at com.gauss.sportsapp.eventrequests.GetEventRequestsTask.onPostExecute(GetEventRequestsTask.java:74) E / AndroidRuntime(658):at com.gauss.sportsapp.eventrequests.GetEventRequestsTask.onPostExecute(GetEventRequestsTask.java:1) E / AndroidRuntime(658):在android.os.AsyncTask.finish(AsyncTask.java:602) E / AndroidRuntime(658):在android.os.AsyncTask.access $ 600(AsyncTask.java:156) E / AndroidRuntime(658):在android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:615) E / AndroidRuntime(658):在android.os.Handler.dispatchMessage(Handler.java:99) E / AndroidRuntime(658):在android.os.Looper.loop(Looper.java:154) E / AndroidRuntime(658):在android.app.ActivityThread.main(ActivityThread.java:4945) E / AndroidRuntime(658):at java.lang.reflect.Method.invokeNative(Native Method) E / AndroidRuntime(658):at java.lang.reflect.Method.invoke(Method.java:511) E / AndroidRuntime(658):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784) E / AndroidRuntime(658):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) E / AndroidRuntime(658):at dalvik.system.NativeStart.main(Native Method)
只有在我通过主页按钮退出应用程序并让它在后台暂停一段时间后才会出现错误。如果我只是退出应用程序并立即再次启动它,那就没有错误。有时甚至在我让它静置一段时间之后它仍然有效,但是当我在适配器的构造函数中设置布局inflater时,它通常返回一个空指针。
片段的重要代码:
public class MessagesList extends Fragment implements EventRequestListener {
private ListView lv;
private GetEventRequestsTask recievedRequestsTask;
private List<GetEventRequest> data;
private RecievedRequestsAdapter adapter;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
recievedRequestsTask = new GetEventRequestsTask(getActivity(), user_id);
recievedRequestsTask.setEventRequestListener(MessagesList.this);
recievedRequestsTask.execute();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment_messages, container, false);
}
//Async Task listener, gets called if(result != null)
@Override
public void onEventsAvailable(List<GetEventRequest> result) {
// TODO Auto-generated method stub
adapter = new RecievedRequestsAdapter(getActivity(), data);
lv.setAdapter(adapter);
}
@Override
public void onStop() {
// TODO Auto-generated method stub
super.onStop();
if(recievedRequestsTask != null){
recievedRequestsTask.cancel(true);
}
}
}
适配器:
public class RecievedRequestsAdapter extends BaseAdapter {
private LayoutInflater inflater;
private List<GetEventRequest> list;
public RecievedRequestsAdapter(Context ctx, List<GetEventRequest> list){
this.list = list;
inflater = LayoutInflater.from(ctx); //This is where null pointer happens
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.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;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.result_list_adapter, parent, false);
holder.txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
holder.txtDesc = (TextView) convertView.findViewById(R.id.txtDescription);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//Do stuff, set views, etc
return convertView;
}
private static final class ViewHolder{
public TextView txtTitle, txtDesc;
}
}
任何人都有任何想法?测试或调试它非常困难,因为它是随机发生的,只有在延长的应用期后才会在后台闲置。
答案 0 :(得分:1)
如果片段未附加到活动,则片段getActivity()
会返回null
。
您的异步任务侦听器onEventsAvailable()
未绑定到片段生命周期,并且在分离片段时也可以触发。
考虑重新设计代码,以便不显示的片段不需要自行更新。