将工具栏添加到片段后应用程序崩溃

时间:2015-10-16 04:56:39

标签: android android-layout android-fragments android-toolbar

我尝试从没有工具栏(R.id.main_screen)的片段切换到具有工具栏(R.id.source_items_list)的片段。

但是,只要我在R.id.source_items_list中有工具栏,它就会崩溃。我得到的错误是

  

android.view.InflateException:二进制XML文件行#10:错误   膨胀类android.support.v7.widget.Toolbar

来自

行 以下代码中的

View view = inflater.inflate(R.layout.fragment_sourceitem_list, viewGroup, false);

我想知道我做错了什么。这是我的代码。

public class MainActivity extends FragmentActivity implements MainFragment.OnFragmentInteractionListener {

 ...

@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);

    // set uncaught exception handler for thread
    // Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));

    setContentView(R.layout.activity_main);

    FragmentManager fragmentManager = getSupportFragmentManager();

    mMainFrame = MainFragment.newInstance();
    fragmentManager.beginTransaction().add(R.id.container, mMainFrame).commit();
}

    @Override
    public void onFragmentInteraction(int id) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        switch (id) {
            case R.id.button_sources:
                Fragment fragment = new SourceListFragment();
                FragmentTransaction fragmentTrans = fragmentManager.beginTransaction();
                fragmentTrans.replace(R.id.container, fragment);
                fragmentTrans.addToBackStack(null);
                fragmentTrans.commit();
                break;
            default:
                break;
        }
    }
}



public class SourceListFragment extends Fragment implements AbsListView.OnClickListener {
    ...

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {

        Log.d("SOURCELIST", "SourceListFragment onCreateView\n");

        View view = inflater.inflate(R.layout.fragment_sourceitem_list, viewGroup, false);

        return view;
    }


    public static SourceListFragment newInstance(BluetoothAdapter adapter) {
        SourceListFragment fragment = new SourceListFragment();
        mBTAdapter = adapter;
        return fragment;
    }

    // Container Activity must implement this interface
    public interface OnFragmentInteractionListener {
        public void onFragmentInteraction(int id);
    }
}

这是布局:

activity_main.xml中:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:materialdesign="http://schemas.android.com/apk/res-auto"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="#FFF"
              android:orientation="vertical"
              android:id="@+id/container">

</FrameLayout>

fragment_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/main_screen"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="148dp">

        <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="128dp"
                android:background="#01579b"
                android:paddingBottom="20dp"
                android:paddingLeft="104dp" android:id="@+id/banner">
        </RelativeLayout>

    </RelativeLayout>

    <android.support.percent.PercentRelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/button_sources"
                app:layout_widthPercent="30%"
                app:layout_heightPercent="30%"
                app:layout_marginTopPercent="10%"
                app:layout_marginLeftPercent="15%"
                app:layout_marginRightPercent="5%"/>
...

    </android.support.percent.PercentRelativeLayout>
</LinearLayout>

fragment_sourceitem_list.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:id="@+id/source_items_list"
              android:layout_height="match_parent"
              android:orientation="vertical">

<android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/toolbar"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:background="?attr/colorPrimary"/>

    <ListView android:id="@android:id/list" android:layout_width="match_parent"
              android:layout_height="match_parent"
            />

    <com.gc.materialdesign.views.ProgressBarCircularIndeterminate
            android:id="@+id/progressBarCircularIndeterminate"
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:background="#1E88E5" />

</FrameLayout>

任何帮助将不胜感激。

由于

0 个答案:

没有答案