Android导航抽屉错误

时间:2014-07-26 04:36:18

标签: java android

我正在尝试做一个android导航抽屉。

GoogleMap.java

public class GoogleMap extends FragmentActivity {

    // Google Map
    private com.google.android.gms.maps.GoogleMap map;

    private String[] drawerListViewItems;
    private DrawerLayout drawerLayout;
    private ListView drawerListView;
    private ActionBarDrawerToggle actionBarDrawerToggle;

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

        try {
            // Loading map
            initilizeMap();

        } catch (Exception e) {
            e.printStackTrace();
        }

        // get list items from strings.xml
        drawerListViewItems = getResources().getStringArray(R.array.items);
        // get ListView defined in activity_main.xml
        drawerListView = (ListView) findViewById(R.id.left_drawer);

        // Set the adapter for the list view
        drawerListView.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_listview_item, drawerListViewItems));

        // App Icon 
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        actionBarDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                drawerLayout,         /* DrawerLayout object */
                R.drawable.ic_drawer,  /* nav drawer icon to replace 'Up' caret */
                R.string.drawer_open,  /* "open drawer" description */
                R.string.drawer_close  /* "close drawer" description */
                );

        // Set actionBarDrawerToggle as the DrawerListener
        drawerLayout.setDrawerListener(actionBarDrawerToggle);

        getActionBar().setDisplayHomeAsUpEnabled(true); 

        // just styling option add shadow the right edge of the drawer
    drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
    drawerListView.setOnItemClickListener(new DrawerItemClickListener());
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        actionBarDrawerToggle.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 (actionBarDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
         actionBarDrawerToggle.syncState();
    }

    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView parent, View view, int position, long id) {
            Toast.makeText(GoogleMap.this, ((TextView)view).getText(), Toast.LENGTH_LONG).show();
            drawerLayout.closeDrawer(drawerListView);

        }
    }



    /**
     * function to load map. If map is not created it will create it for you
     * */
    private void initilizeMap() {
        if (map == null) {
            map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
                    R.id.map)).getMap();

            map.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(1.32814,103.80679) , 11.0f) );

            // check if map is created successfully or not
            if (map == null) {
                Toast.makeText(getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initilizeMap();
    }
}

main.xml中

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".GoogleMap" >

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

    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- The navigation drawer -->
    <ListView android:id="@+id/left_drawer"
        android:layout_width="240dp"
        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"
        />

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

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

</RelativeLayout>

drawer_listview_item.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
    android:id="@android:id/text1"
    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"
/>

</LinearLayout>

但是,我收到的错误是logCat:

    07-26 12:29:39.417: D/dalvikvm(5779): GC_CONCURRENT freed 221K, 11% free 9389K/10503K, paused 8ms+6ms
07-26 12:29:40.042: I/Google Maps Android API(5779): Google Play services client version: 3225100
07-26 12:29:40.050: I/dalvikvm(5779): Could not find method guj.a, referenced from method gqi.a
07-26 12:29:40.050: W/dalvikvm(5779): VFY: unable to resolve static method 24936: Lguj;.a (Landroid/content/Context;)Lgri;
07-26 12:29:40.050: D/dalvikvm(5779): VFY: replacing opcode 0x71 at 0x0003
07-26 12:29:40.097: E/dalvikvm(5779): Could not find class 'gpr', referenced from method gps.a
07-26 12:29:40.097: W/dalvikvm(5779): VFY: unable to resolve new-instance 4090 (Lgpr;) in Lgps;
07-26 12:29:40.097: D/dalvikvm(5779): VFY: replacing opcode 0x22 at 0x0000
07-26 12:29:40.097: E/dalvikvm(5779): Could not find class 'gpr', referenced from method gps.a
07-26 12:29:40.097: W/dalvikvm(5779): VFY: unable to resolve new-instance 4090 (Lgpr;) in Lgps;
07-26 12:29:40.097: D/dalvikvm(5779): VFY: replacing opcode 0x22 at 0x0000
07-26 12:29:40.113: E/dalvikvm(5779): Could not find class 'gpr', referenced from method gps.a
07-26 12:29:40.113: W/dalvikvm(5779): VFY: unable to resolve new-instance 4090 (Lgpr;) in Lgps;
07-26 12:29:40.113: D/dalvikvm(5779): VFY: replacing opcode 0x22 at 0x0000
07-26 12:29:40.113: D/dalvikvm(5779): DexOpt: unable to opt direct call 0x5ea3 at 0x0a in Lgps;.a
07-26 12:29:40.113: D/dalvikvm(5779): DexOpt: unable to opt direct call 0x5ea3 at 0x0c in Lgps;.a
07-26 12:29:40.113: D/dalvikvm(5779): DexOpt: unable to opt direct call 0x5ea3 at 0x0a in Lgps;.a
07-26 12:29:40.113: D/dalvikvm(5779): GC_CONCURRENT freed 160K, 11% free 9666K/10759K, paused 2ms+2ms
07-26 12:29:40.128: I/Google Maps Android API(5779): Google Play services package version: 5084034
07-26 12:29:40.144: W/dalvikvm(5779): VFY: unable to resolve static field 20873 (t) in Lyp;
07-26 12:29:40.144: D/dalvikvm(5779): VFY: replacing opcode 0x62 at 0x000e
07-26 12:29:40.152: W/dalvikvm(5779): VFY: unable to resolve static field 20873 (t) in Lyp;
07-26 12:29:40.152: D/dalvikvm(5779): VFY: replacing opcode 0x62 at 0x000d
07-26 12:29:40.425: D/dalvikvm(5779): GC_CONCURRENT freed 337K, 12% free 9844K/11079K, paused 2ms+3ms
07-26 12:29:40.933: D/dalvikvm(5779): GC_CONCURRENT freed 276K, 11% free 10029K/11207K, paused 14ms+3ms
07-26 12:29:41.363: I/dalvikvm(5779): Failed resolving Lcom/google/android/gms/location/internal/ParcelableGeofence; interface 4023 'Lgln;'
07-26 12:29:41.363: W/dalvikvm(5779): Link of class 'Lcom/google/android/gms/location/internal/ParcelableGeofence;' failed
07-26 12:29:41.363: E/dalvikvm(5779): Could not find class 'com.google.android.gms.location.internal.ParcelableGeofence', referenced from method glt.a
07-26 12:29:41.363: W/dalvikvm(5779): VFY: unable to resolve check-cast 2086 (Lcom/google/android/gms/location/internal/ParcelableGeofence;) in Lglt;
07-26 12:29:41.363: D/dalvikvm(5779): VFY: replacing opcode 0x1f at 0x0019
07-26 12:29:42.253: D/dalvikvm(5779): GC_CONCURRENT freed 280K, 11% free 10258K/11463K, paused 27ms+4ms
07-26 12:29:43.706: D/TextLayoutCache(5779): Using debug level: 0 - Debug Enabled: 0
07-26 12:29:43.831: E/ArrayAdapter(5779): You must supply a resource ID for a TextView
07-26 12:29:43.831: D/AndroidRuntime(5779): Shutting down VM
07-26 12:29:43.831: W/dalvikvm(5779): threadid=1: thread exiting with uncaught exception (group=0x40c331f8)
07-26 12:29:43.855: E/AndroidRuntime(5779): FATAL EXCEPTION: main
07-26 12:29:43.855: E/AndroidRuntime(5779): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
07-26 12:29:43.855: E/AndroidRuntime(5779):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
07-26 12:29:43.855: E/AndroidRuntime(5779):     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
07-26 12:29:43.855: E/AndroidRuntime(5779):     at android.widget.AbsListView.obtainView(AbsListView.java:2222)
07-26 12:29:43.855: E/AndroidRuntime(5779):     at android.widget.ListView.makeAndAddView(ListView.java:1774)
07-26 12:29:43.855: E/AndroidRuntime(5779):     at android.widget.ListView.fillDown(ListView.java:672)
07-26 12:29:43.855: E/AndroidRuntime(5779):     at android.widget.ListView.fillFromTop(ListView.java:732)
07-26 12:29:43.855: E/AndroidRuntime(5779):     at android.widget.ListView.layoutChildren(ListView.java:1625)
07-26 12:29:43.855: E/AndroidRuntime(5779):     at android.widget.AbsListView.onLayout(AbsListView.java:2052)
07-26 12:29:43.855: E/AndroidRuntime(5779):     at android.view.View.layout(View.java:11447)
07-26 12:29:43.855: E/AndroidRuntime(5779):     at android.view.ViewGroup.layout(ViewGroup.java:4224)
07-26 12:29:43.855: E/AndroidRuntime(5779):     at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:702)
07-26 12:29:43.855: E/AndroidRuntime(5779):     at android.view.View.layout(View.java:11447)
07-26 12:29:43.855: E/AndroidRuntime(5779):     at android.view.ViewGroup.layout(ViewGroup.java:4224)
07-26 12:29:43.855: E/AndroidRuntime(5779):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:925)
07-26 12:29:43.855: E/AndroidRuntime(5779):     at android.view.View.layout(View.java:11447)
07-26 12:29:43.855: E/AndroidRuntime(5779):     at android.view.ViewGroup.layout(ViewGroup.java:4224)
07-26 12:29:43.855: E/AndroidRuntime(5779):     at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
07-26 12:29:43.855: E/AndroidRuntime(5779):     at android.view.View.layout(View.java:11447)
07-26 12:29:43.855: E/AndroidRuntime(5779):     at android.view.ViewGroup.layout(ViewGroup.java:4224)
07-26 12:29:43.855: E/AndroidRuntime(5779):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1634)
07-26 12:29:43.855: E/AndroidRuntime(5779):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1492)
07-26 12:29:43.855: E/AndroidRuntime(5779):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1405)
07-26 12:29:43.855: E/AndroidRuntime(5779):     at android.view.View.layout(View.java:11447)
07-26 12:29:43.855: E/AndroidRuntime(5779):     at android.view.ViewGroup.layout(ViewGroup.java:4224)
07-26 12:29:43.855: E/AndroidRuntime(5779):     at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
07-26 12:29:43.855: E/AndroidRuntime(5779):     at android.view.View.layout(View.java:11447)
07-26 12:29:43.855: E/AndroidRuntime(5779):     at android.view.ViewGroup.layout(ViewGroup.java:4224)
07-26 12:29:43.855: E/AndroidRuntime(5779):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1671)
07-26 12:29:43.855: E/AndroidRuntime(5779):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2628)
07-26 12:29:43.855: E/AndroidRuntime(5779):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-26 12:29:43.855: E/AndroidRuntime(5779):     at android.os.Looper.loop(Looper.java:137)
07-26 12:29:43.855: E/AndroidRuntime(5779):     at android.app.ActivityThread.main(ActivityThread.java:4512)
07-26 12:29:43.855: E/AndroidRuntime(5779):     at java.lang.reflect.Method.invokeNative(Native Method)
07-26 12:29:43.855: E/AndroidRuntime(5779):     at java.lang.reflect.Method.invoke(Method.java:511)
07-26 12:29:43.855: E/AndroidRuntime(5779):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
07-26 12:29:43.855: E/AndroidRuntime(5779):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
07-26 12:29:43.855: E/AndroidRuntime(5779):     at dalvik.system.NativeStart.main(Native Method)
07-26 12:29:43.855: E/AndroidRuntime(5779): Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
07-26 12:29:43.855: E/AndroidRuntime(5779):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
07-26 12:29:43.855: E/AndroidRuntime(5779):     ... 36 more
07-26 12:29:43.917: D/dalvikvm(5779): GC_CONCURRENT freed 273K, 11% free 10465K/11655K, paused 2ms+3ms
07-26 12:29:52.769: I/Process(5779): Sending signal. PID: 5779 SIG: 9

我不确定哪个部分出了问题。我按照教程:Navigation Drawer

我测试了设置谷歌地图部分,它工作正常。只有在我尝试添加导航抽屉时才会出现错误。

任何指南?提前谢谢。

1 个答案:

答案 0 :(得分:0)

您的drawer_listview_item应该只包含TextView。也就是说,删除LinearLayout

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    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"
/>

logcat的:

Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView

另一个指标:

java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView