InboxFragment is a fragment of MainActivity.
In action bar menu of the MainActivity (those three dots on the ActionBar), I added an option Refresh Inbox, which calls a function retrieveMessages() by creating an object of InboxFragment.
retrieveMessages is a 'member' function in InboxFragment (which was earlier in onCreateView function of the fragment to retrieve messages, then I refactored it to extract code to reuse it in MainActivity).
I have tried using intent to re-create MainActivity by using this, MainActivity.class as parameters of the Intent but that is kind of an ugly refresh. How to handle the above error?
Here are the required pieces of code:
In MainActivity:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
switch(itemId) {
//pieces of code we're not concerned with
case R.id.action_refresh_inbox:
InboxFragment inboxFragment = new InboxFragment();
inboxFragment.retrieveMessages();
break;
}
}
In InboxFragment.java -
public class InboxFragment extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_inbox, container, false);
return rootView;
}
@Override
public void onResume() {
super.onResume();
retrieveMessages();
}
public void retrieveMessages() {
//code that was earlier running in onResume(){};
}
Here is the logcat:
01-11 01:35:44.515 21968-21968/com.ghostriley.sgt.ghostchat E/ViewRootImpl﹕ sendUserActionEvent() mView == null
01-11 01:35:44.535 21968-22032/com.ghostriley.sgt.ghostchat D/mali_winsys﹕ new_window_surface returns 0x3000, [227x88]-format:1
01-11 01:35:44.945 21968-21968/com.ghostriley.sgt.ghostchat D/AndroidRuntime﹕ Shutting down VM
01-11 01:35:44.945 21968-21968/com.ghostriley.sgt.ghostchat E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.ghostriley.sgt.ghostchat, PID: 21968
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 com.ghostriley.sgt.ghostchat.UI.InboxFragment$1.done(InboxFragment.java:67)
at com.ghostriley.sgt.ghostchat.UI.InboxFragment$1.done(InboxFragment.java:55)
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:135)
at android.app.ActivityThread.main(ActivityThread.java:5930)
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:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
Is there any way to cal InboxFragment.onResume() ?
解决方案
You haven't attached your Fragment to your Activity. You'll need something along the lines of:
getFragmentManager()
.beginTransaction()
.add(android.R.id.content, inboxFragment)
.commit();