我有一个错误,我无法排除故障。该程序有效,但当设备空闲并且我恢复到应用程序时它会崩溃。基本上,我的代码所做的是获得用户的邀请。它显示从多个用户收到的邀请,我可以选择接受或拒绝邀请。 0表示拒绝邀请,1表示接受邀请。我正在使用解析BaaS。我收到此错误并且无法解决问题,请需要一些帮助:
错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:153)
at com.example.davchen.skibuddies.Fragments.InviteTab$2.<init>(InviteTab.java:93)
at com.example.davchen.skibuddies.Fragments.InviteTab.updateList(InviteTab.java:92)
at com.example.davchen.skibuddies.Fragments.InviteTab.access$200(InviteTab.java:35)
at com.example.davchen.skibuddies.Fragments.InviteTab$1.done(InviteTab.java:81)
at com.example.davchen.skibuddies.Fragments.InviteTab$1.done(InviteTab.java:69)
at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:115)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
以下是我的代码:
public class InviteTab extends Fragment {
private static final String TAG = InviteTab.class.getSimpleName();
private List<Invitation> invites;
private ListView inviteList;
private Invitation invitation;
public InviteTab() {}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
invites = new ArrayList<Invitation>();
invitation = new Invitation();
}
// @Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//return super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(R.layout.invite_tab_fragment, container, false);
inviteList = (ListView)rootView.findViewById(R.id.invitationList);
queryEventListFromParse();
return rootView;
}
private void queryEventListFromParse() {
ParseQuery<ParseObject> query = ParseQuery.getQuery("Invitation");
query.whereEqualTo("Participants", ParseUser.getCurrentUser());
query.whereEqualTo("Status", "0");
query.include("EventId");
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> Objects, ParseException e) {
if (e == null) {
Log.d(TAG, "Check if program enters loop");
invites.clear();
for (ParseObject EventInvite : Objects) {
invites.add((Invitation) EventInvite);
}
Log.d(TAG, String.valueOf(invites.size()));
updateList(); //update the list
} else {
Log.d(TAG, "Unable to retreive Invites: " + e.getMessage());
}
}
});
}
private void updateList() {
ArrayAdapter<Invitation> adapter = new ArrayAdapter<Invitation>(getActivity(),
R.layout.invitation_list, invites) {
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if(convertView==null) {
convertView = getActivity().getLayoutInflater().inflate(R.layout.invitation_list, parent, false);
}
TextView Title = (TextView)convertView.findViewById(R.id.eventName);
Button button1 =(Button)convertView.findViewById(R.id.acceptButton);
Button button2 = (Button)convertView.findViewById(R.id.rejectButton);
//TextView tvStart = (TextView)convertView.findViewById(R.id.textView_start);
// TextView tvEnd = (TextView)convertView.findViewById(R.id.textView_end);
invitation = invites.get(position);
Title.setText(invitation.getEventId().get("EventTitle").toString());
// tvStart.setText(event.getStartTime());
// tvEnd.setText(event.getEndTime());
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateStatus(position);
}
});
return convertView;
}
@Override
public int getCount() {
return super.getCount();
}
};
Log.d(TAG, "Was in adapter method");
inviteList.setAdapter(adapter);
}
private void updateStatus(int position) {
Invitation invite = invites.get(position);
invite.setUserId(ParseUser.getCurrentUser());
invite.setFlag("1");
invite.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if(e==null) {
Toast.makeText(getActivity().getApplicationContext(), "Accepted Request", Toast.LENGTH_LONG).show();
}
else{
Log.e(TAG, e.getMessage());
}
}
});
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
@Override
public void onDetach() {
super.onDetach();
}
@Override
public void onResume() {
super.onResume();
queryEventListFromParse();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == getActivity().RESULT_OK) {
// If a new event has been added, update the list of events
Log.d(TAG, "Hello");
queryEventListFromParse();
}
}
}
答案 0 :(得分:0)
不要使用getActivity()
方法be'coz片段de附加
当应用程序进入后台并在之后打开时。
在将片段附加到actvity
时初始化context
@Override
public void onAttach(Activity context) {
super.onAttach(context);
this.context = context;
}
将代码放入onActivityCreated
并使用context
而不是getActivity()
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
queryEventListFromParse();
}