java视图没了怎么办,如何处理错误:java.lang.IllegalStateException:内容视图尚未创建...

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();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`java.lang.IllegalStateException: Surface has already been released` 是一个常见错误,它通常发生在试图操作已经释放(closed)的Surface时。在Android开发中,SurfaceView是一个用于显示图形内容视图,如果在SurfaceView不再需要时没有正确地释放其Surface,就可能导致这种异常。 引用提到的错误可能是因为在SurfaceView的生命周期管理中出现了问题,比如在SurfaceView关闭(如在onDetachedFromWindow()或onPause()方法中)之后还尝试访问它的Surface。这可能是由于忘记调用`setSurfaceCallback(null)`来清除SurfaceView的回调,或者在SurfaceView被销毁后再尝试绘制。 引用提供的日志信息进一步确认了SurfaceView更新窗口(updateWindow)引发了ANR(应用无响应),因为Surface已经释放。解决这个问题的步骤通常是: 1. **正确管理SurfaceView的生命周期**:确保在SurfaceView不再需要时,正确地调用`surfaceDestroyed()`方法并清理Surface。在`onDetachedFromWindow()`或`onPause()`中,移除SurfaceView的SurfaceHolder的回调。 ```java // 在SurfaceView的类中 @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); if (holder != null) { holder.removeCallback(surfaceCallback); holder.surface.release(); } } ``` 2. **处理SurfaceView更新**:如果在主线程中更新SurfaceView,可能会导致ANR。考虑将耗时的操作移到子线程中执行,或者使用`post Runnable`异步处理。 3. **监控并修复ANR**:确保应用没有陷入无限循环或其他阻塞操作,使用Android Studio的Logcat工具实时查看应用日志,以便快速定位问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值