我正在使用带有两个片段的ViewPager
。打开应用程序时,第一个片段被设置为默认值。如果第一个片段没有被刷到下一个片段然后应用程序关闭它就会崩溃。我尝试在StackOverflow上的其他问题中使用onViewCreated
作为回答,但它似乎无法正常工作。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);
View rootView = inflater.inflate(R.layout.fragment_inbox,
container, false);
return rootView;
}
@Override
public void onViewCreated (View view, Bundle savedInstanceState){
//getActivity().setProgressBarIndeterminateVisibility(true);
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(ParseConstants.CLASS_MESSAGES);
query.whereEqualTo(ParseConstants.KEY_RECIPIENT_IDS, ParseUser.getCurrentUser().getObjectId());
query.orderByDescending(ParseConstants.KEY_CREATED_AT);
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> messages, ParseException e) {
// getActivity().setProgressBarIndeterminateVisibility(false);
if (e == null) {
// We found messages!
mMessages = messages;
String[] usernames = new String[mMessages.size()];
int i = 0;
for (ParseObject message : mMessages) {
usernames[i] = message.getString(ParseConstants.KEY_SENDER_NAME);
i++;
}
if (getListView().getAdapter() == null) {
MessageAdapter adapter = new MessageAdapter(
getListView().getContext(),
mMessages);
setListAdapter(adapter);
} else {
// refill the adapter!
((MessageAdapter)getListView().getAdapter()).refill(mMessages);
}
}
}
});
}
以下是例外:
java.lang.IllegalStateException: Content view not yet created
at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
at android.support.v4.app.ListFragment.getListView(ListFragment.java:222)
at io.wyntr.peepster.Fragments.InboxFragment$1.done(InboxFragment.java:73)
at io.wyntr.peepster.Fragments.InboxFragment$1.done(InboxFragment.java:58)
at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:116)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5845)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)