创建抽屉菜单时,我的应用程序崩溃

时间:2015-06-08 23:20:09

标签: android

我正在尝试用抽屉菜单制作一个Android应用程序。 我不想使用带抽屉的向导活动。我想从头开始构建它。 好吧,我看过很多例子。 所以,我从零开始做了一些事情。 我的应用程序一直崩溃。我是否必须修改某些设置。

所以我做了以下事情: 1-创建了一个新的空白活动 我的布局

<RelativeLayout android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="20dp"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    android:orientation="vertical"
    android:background="@android:color/transparent"
    >

</RelativeLayout>
<fragment
    android:layout_width="280dp"
    android:layout_height="match_parent"
    app:layout ="@layout/fragment_navigation_drawer"
    android:layout_gravity = "start"
    tools:layout="@layout/fragment_navigation_drawer" />

然后我创建了一个名为fragment_navigation_drawer

的片段

<!-- TODO: Update blank fragment layout -->
<TextView android:layout_width="match_parent" android:layout_height="match_parent"
    android:text="@string/hello_blank_fragment" />

然后我尝试运行该程序只是为了检查一切是否正常。我收到了这个错误

 06-09 01:34:05.752    1498-1518/android.process.acore E/DictionaryBackupAgent﹕ Couldn't read from the cursor
06-09 01:44:55.736    1498-1510/android.process.acore W/art﹕ Suspending all threads took: 10.443ms

06-09 01:53:09.020    1498-1514/android.process.acore E/StrictMode﹕ A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
    java.lang.Throwable: Explicit termination method 'close' not called
            at dalvik.system.CloseGuard.open(CloseGuard.java:184)
            at android.os.ParcelFileDescriptor.<init>(ParcelFileDescriptor.java:180)
            at android.os.ParcelFileDescriptor$1.createFromParcel(ParcelFileDescriptor.java:916)
            at android.os.ParcelFileDescriptor$1.createFromParcel(ParcelFileDescriptor.java:906)
            at android.app.IBackupAgent$Stub.onTransact(IBackupAgent.java:57)
            at android.os.Binder.execTransact(Binder.java:446)
06-09 01:53:09.021    1498-1514/android.process.acore E/StrictMode﹕ A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
    java.lang.Throwable: Explicit termination method 'close' not called
            at dalvik.system.CloseGuard.open(CloseGuard.java:184)
            at android.os.ParcelFileDescriptor.<init>(ParcelFileDescriptor.java:180)
            at android.os.ParcelFileDescriptor$1.createFromParcel(ParcelFileDescriptor.java:916)
            at android.os.ParcelFileDescriptor$1.createFromParcel(ParcelFileDescriptor.java:906)
            at android.app.IBackupAgent$Stub.onTransact(IBackupAgent.java:64)
            at android.os.Binder.execTransact(Binder.java:446)
06-09 01:53:09.021    1498-1514/android.process.acore E/StrictMode﹕ A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
    java.lang.Throwable: Explicit termination method 'close' not called
            at dalvik.system.CloseGuard.open(CloseGuard.java:184)
            at android.os.ParcelFileDescriptor.<init>(ParcelFileDescriptor.java:180)
            at android.os.ParcelFileDescriptor$1.createFromParcel(ParcelFileDescriptor.java:916)
            at android.os.ParcelFileDescriptor$1.createFromParcel(ParcelFileDescriptor.java:906)
            at android.app.IBackupAgent$Stub.onTransact(IBackupAgent.java:71)
            at android.os.Binder.execTransact(Binder.java:446)

1 个答案:

答案 0 :(得分:2)

使用Drawerlayout包装布局添加框架布局以保存片段和列表以显示列表项:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_container"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000">
    <!--
        this frame layout will hold the main blank fragment (used to swap fragments in and out)
    -->
    <FrameLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>

    <!--
        this list-view will hold the drawer options i.e; settings, preferences, etc....
    -->
    <ListView
        android:id="@+id/drawer_list"
        android:layout_width="320dp"
        android:layout_height="wrap_content"
        android:layout_gravity="start"
        android:background="#fff"/>

</android.support.v4.widget.DrawerLayout>

接下来在代码中设置抽屉项目:

 private void setDrawerItems() {
        String[] drawerItems = getResources().getStringArray(R.array.drawer_list_items);

        mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, drawerItems);
        mDrawerList.setAdapter(mAdapter);
    }

这里的R.array.drawer_list_items在res / values / strings.xml中定义

像这样:

<!--
        array list of items in the nav drawer
    -->
    <string-array name="drawer_list_items">
        <item>item 1</item>
        <item>item 2</item>
    </string-array>

交换片段你需要使用这样的东西:

public void replaceContentWithFragment(final Fragment frag) {
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.setCustomAnimations(R.anim.fastfade_in, R.anim.fastfade_out, R.anim.fastfade_in, R.anim.fastfade_out);
        ft.replace(R.id.main_content, frag);
        ft.commit();
    }