单击图标时Android DrawerLayout崩溃

时间:2014-01-26 00:13:19

标签: android list layout drawerlayout

所以我想了解DrawerLayout。首先,我刚刚创建了它并且它运行良好。然后我尝试通过在左上角添加图标来操纵它,这允许您显示/隐藏列表。 问题是,当我启动应用程序时,列表已经显示,当我尝试通过单击图标隐藏它时,应用程序崩溃了。这是我的文件:  activity_main.xml中:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/DrawerLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/ContenuFrame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

       <ListView
           android:id="@+id/ListView1"
            android:layout_width="241dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:divider="#666"
            android:dividerHeight="1dp"
            android:background="#333"
            android:paddingLeft="15sp"
            android:paddingRight="15sp"/>
    </FrameLayout>


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

Item_Layout :(列表中的项目(每个)的布局):

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/Element"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#fff"
        android:textSize="20sp"
        android:gravity="center_vertical"
        android:paddingStart="14.5sp"
        android:paddingEnd="14.5sp"
        android:minHeight="35sp"/>

MainActivity.java:

package com.example.drawerlisttest;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

    String[] ListElement;
    ListView List;
    DrawerLayout DLayout;
    ActionBarDrawerToggle ABDT;

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

        ListElement = getResources().getStringArray(R.array.StringList);
        List = (ListView)findViewById(R.id.ListView1);
        List.setAdapter(new ArrayAdapter<String>(this, R.layout.item_layout, ListElement));

        //Set the Icon:
        DLayout = (DrawerLayout)findViewById(R.id.DrawerLayout1);
        ABDT = new ActionBarDrawerToggle(this, DLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close);

        //Faire de l'actionBarDrawerToggle un DrawerListener de DrawerLayout
        DLayout.setDrawerListener(ABDT);
        getActionBar().setDisplayHomeAsUpEnabled(true);

        //Add the shodow to the right after displying the list
        DLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); 
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig){
        super.onConfigurationChanged(newConfig);
        ABDT.onConfigurationChanged(newConfig);
    }

     @Override
        public boolean onOptionsItemSelected(MenuItem item) {

             // call ActionBarDrawerToggle.onOptionsItemSelected(), if it returns true
            // then it has handled the app icon touch event
            if (ABDT.onOptionsItemSelected(item)) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
}

最后是Log:

01-25 18:52:53.232: E/AndroidRuntime(1618): FATAL EXCEPTION: main
01-25 18:52:53.232: E/AndroidRuntime(1618): Process: com.example.drawerlisttest, PID: 1618
01-25 18:52:53.232: E/AndroidRuntime(1618): java.lang.IllegalArgumentException: No drawer view found with gravity LEFT
01-25 18:52:53.232: E/AndroidRuntime(1618):     at android.support.v4.widget.DrawerLayout.openDrawer(DrawerLayout.java:1046)
01-25 18:52:53.232: E/AndroidRuntime(1618):     at android.support.v4.app.ActionBarDrawerToggle.onOptionsItemSelected(ActionBarDrawerToggle.java:292)
01-25 18:52:53.232: E/AndroidRuntime(1618):     at com.example.drawerlisttest.MainActivity.onOptionsItemSelected(MainActivity.java:61)
01-25 18:52:53.232: E/AndroidRuntime(1618):     at android.app.Activity.onMenuItemSelected(Activity.java:2600)
01-25 18:52:53.232: E/AndroidRuntime(1618):     at com.android.internal.widget.ActionBarView$3.onClick(ActionBarView.java:167)
01-25 18:52:53.232: E/AndroidRuntime(1618):     at android.view.View.performClick(View.java:4438)
01-25 18:52:53.232: E/AndroidRuntime(1618):     at android.view.View$PerformClick.run(View.java:18422)
01-25 18:52:53.232: E/AndroidRuntime(1618):     at android.os.Handler.handleCallback(Handler.java:733)
01-25 18:52:53.232: E/AndroidRuntime(1618):     at android.os.Handler.dispatchMessage(Handler.java:95)
01-25 18:52:53.232: E/AndroidRuntime(1618):     at android.os.Looper.loop(Looper.java:136)
01-25 18:52:53.232: E/AndroidRuntime(1618):     at android.app.ActivityThread.main(ActivityThread.java:5017)
01-25 18:52:53.232: E/AndroidRuntime(1618):     at java.lang.reflect.Method.invokeNative(Native Method)
01-25 18:52:53.232: E/AndroidRuntime(1618):     at java.lang.reflect.Method.invoke(Method.java:515)
01-25 18:52:53.232: E/AndroidRuntime(1618):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-25 18:52:53.232: E/AndroidRuntime(1618):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-25 18:52:53.232: E/AndroidRuntime(1618):     at dalvik.system.NativeStart.main(Native Method)

正如你所看到的,Log描述了一些关于重力的东西,我在布局文件中查找它,并且没有这样的节点有gravity = left或类似的东西......

至于string.xml,好吧;我不认为这是必要的......

谢谢。

1 个答案:

答案 0 :(得分:5)

尝试从FrameLayout中删除listView。

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/DrawerLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
    android:id="@+id/ContenuFrame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

<ListView
    android:id="@+id/ListView1"
    android:layout_width="241dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="#666"
    android:dividerHeight="1dp"
    android:background="#333"
    android:paddingLeft="15sp"
    android:paddingRight="15sp"/>

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