交易后定向后“片段未创建视图”错误

时间:2015-10-30 20:07:11

标签: android android-fragments

我的活动包含一个片段:

<!-- activity_main.xml -->
<fragment
    android:id="@+id/feed_fragment"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="match_parent"
    android:name="com.example.feedreader.FeedFragment"
    tools:layout="@layout/layout_feed_fragment"/>

在java中,如果用户转到首选项并清除历史记录,我会创建该片段的新实例:

// MainActivity.java
@Override
protected void onResume() {
    super.onResume();

    if (PreferencesFragment.historyWasCleared) {
        PreferencesFragment.historyWasCleared = false;

        Log.d(TAG, "history Was Cleared");

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        feedFragment = new FeedFragment();
        fragmentTransaction.replace(R.id.feed_fragment, feedFragment);
        fragmentTransaction.commit();
    } else {
        feedFragment.refreshView();
    }
}

如果我之后进行了方向更改,我会得到一些例外情况:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.feedreader/com.example.feedreader.MainActivity}: android.view.InflateException: Binary XML file line #10: Error inflating class fragment

Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class fragment

Caused by: java.lang.IllegalStateException: Fragment com.example.feedreader.FeedFragment did not create a view.

这是FeedFragment的onCreateView:

// FeedFragment.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.layout_feed_fragment, container, false);

    db = new DbFeed(getActivity());
    entries = db.getAllEntries();
    FeedListAdapter adapter = new FeedListAdapter(getActivity(), entries);

    setListAdapter(adapter);

    return v;
}

根据要求,onCreate:

// MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    feedFragment = (FeedFragment)getSupportFragmentManager()
            .findFragmentById(R.id.feed_fragment);

    feedListAdapter = (FeedListAdapter)feedFragment.getListAdapter();

    receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d(TAG, "mottar broadcast");
            feedFragment.refreshView();
        }
    };

    historydb = new DbHistory(this);

    connManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
}

更多错误日志。第35行是setContentView(R.layout.activity_main)

10-30 21:56:53.135 10466-10466/com.example.feedreader E/AndroidRuntime:  Caused by: java.lang.IllegalStateException: Fragment com.example.feedreader.FeedFragment did not create a view.
10-30 21:56:53.135 10466-10466/com.example.feedreader E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2325)
10-30 21:56:53.135 10466-10466/com.example.feedreader E/AndroidRuntime:     at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:111)
10-30 21:56:53.135 10466-10466/com.example.feedreader E/AndroidRuntime:     at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:314)
10-30 21:56:53.135 10466-10466/com.example.feedreader E/AndroidRuntime:     at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
10-30 21:56:53.135 10466-10466/com.example.feedreader E/AndroidRuntime:     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
10-30 21:56:53.135 10466-10466/com.example.feedreader E/AndroidRuntime:     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
10-30 21:56:53.135 10466-10466/com.example.feedreader E/AndroidRuntime:     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 
10-30 21:56:53.135 10466-10466/com.example.feedreader E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
10-30 21:56:53.135 10466-10466/com.example.feedreader E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
10-30 21:56:53.135 10466-10466/com.example.feedreader E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
10-30 21:56:53.135 10466-10466/com.example.feedreader E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:257) 
10-30 21:56:53.135 10466-10466/com.example.feedreader E/AndroidRuntime:     at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109) 
10-30 21:56:53.135 10466-10466/com.example.feedreader E/AndroidRuntime:     at com.example.feedreader.MainActivity.onCreate(MainActivity.java:35) 

0 个答案:

没有答案