导航抽屉

时间:2013-07-02 11:41:29

标签: android xml classnotfoundexception

通过实施导航抽屉我遇到了问题。  我的xml和.java代码:

<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 -->

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/hintergrund"
    tools:context=".MainActivity" >

    <ImageButton
        android:id="@+id/imgBtNewDocuStartPage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="15dp"
        android:background="@drawable/buttonschatten"
        android:contentDescription="@drawable/ic_notiz"
        android:onClick="dokuklick"
        android:src="@drawable/ic_notiz" />

    <ImageButton
        android:id="@+id/imgBtSearchStartPage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/imgBtNewDocuStartPage"
        android:layout_toRightOf="@+id/imgBtNewDocuStartPage"
        android:background="@drawable/buttonschatten"
        android:contentDescription="@drawable/ic_suchen"
        android:onClick="suchenklick"
        android:src="@drawable/ic_suchen" />

    <Button
        android:id="@+id/btUnreadCommentsStartPage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/imgBtNewDocuStartPage"
        android:layout_marginTop="26dp"
        android:background="@drawable/rahmen"
        android:text="@string/Start.comment"
        android:drawableRight="@drawable/ic_kom"
        android:textColor="@color/button_color" />

    <Button
        android:id="@+id/btUnreadDocusStartPage"
        style="@style/AppBaseTheme"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btUnreadCommentsStartPage"
        android:layout_alignRight="@+id/btUnreadCommentsStartPage"
        android:layout_below="@+id/btUnreadCommentsStartPage"
        android:layout_marginTop="10dp"
        android:background="@drawable/rahmen"
        android:onClick="ungeleseneDoku"
        android:text="@string/Start.unread_docu"
        android:drawableRight="@drawable/ic_doku"
        android:textColor="@color/button_color" />

    <Button
        android:id="@+id/btUnreadEvaluationsStartPage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btUnreadDocusStartPage"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/btUnreadDocusStartPage"
        android:layout_marginTop="10dp"
        android:background="@drawable/rahmen"
        android:text="@string/Start.outcomes"
        android:drawableRight="@drawable/ic_erg"
        android:textColor="@color/button_color" />

    <Button
        android:id="@+id/btFavouritesStartPage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btUnreadEvaluationsStartPage"
        android:layout_alignRight="@+id/btUnreadEvaluationsStartPage"
        android:layout_below="@+id/btUnreadEvaluationsStartPage"
        android:layout_marginTop="10dp"
        android:background="@drawable/rahmen"
        android:text="@string/Start.favorite"
        android:drawableRight="@drawable/ic_fav"
        android:textColor="@color/button_color" />

</RelativeLayout>
<!-- The navigation drawer -->
<ListView 
    android:id="@+id/drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
        android:layout_gravity="left"
        android:background="#78BF3D"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>




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

public class StartActivity extends Activity {

    private ListView mDrawerList;
    private DrawerLayout mDrawer;
    private CustomActionBarDrawerToggle mDrawerToggle;
    private String[] menuItems;

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

        // enable ActionBar app icon to behave as action to toggle nav drawer
                getActionBar().setDisplayHomeAsUpEnabled(true);


                mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);

                // set a custom shadow that overlays the main content when the drawer
                // opens
                mDrawer.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

                _initMenu();
                mDrawerToggle = new CustomActionBarDrawerToggle(this, mDrawer);
                mDrawer.setDrawerListener(mDrawerToggle);


    }


     @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.items, menu);
            return true;
        }

     private void _initMenu() {
            NsMenuAdapter mAdapter = new NsMenuAdapter(this);


            // Add first block

            menuItems = getResources().getStringArray(
                    R.array.ns_menu_items);
            String[] menuItemsIcon = getResources().getStringArray(
                    R.array.ns_menu_items_icon);

            int res = 0;
            for (String item : menuItems) {

                int id_title = getResources().getIdentifier(item, "string",
                        this.getPackageName());
                int id_icon = getResources().getIdentifier(menuItemsIcon[res],
                        "drawable", this.getPackageName());

                NsMenuItemModel mItem = new NsMenuItemModel(id_title, id_icon);

                mAdapter.addItem(mItem);
                res++;
            }



            mDrawerList = (ListView) findViewById(R.id.drawer);
            if (mDrawerList != null)
                mDrawerList.setAdapter(mAdapter);

            mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        }

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

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



        /* Called whenever we call invalidateOptionsMenu() */
        @Override
        public boolean onPrepareOptionsMenu(Menu menu) {
            // If the nav drawer is open, hide action items related to the content view
            boolean drawerOpen = mDrawer.isDrawerOpen(mDrawerList);
            menu.findItem(R.id.action_refresh).setVisible(!drawerOpen);
            return super.onPrepareOptionsMenu(menu);
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            /*
             * The action bar home/up should open or close the drawer.
             * ActionBarDrawerToggle will take care of this.
             */
            if (mDrawerToggle.onOptionsItemSelected(item)) {
                return true;
            }

            // Handle your other action bar items...
            return super.onOptionsItemSelected(item);
        }

        private class CustomActionBarDrawerToggle extends ActionBarDrawerToggle {

            public CustomActionBarDrawerToggle(Activity mActivity,DrawerLayout mDrawerLayout){
                super(
                    mActivity,
                    mDrawerLayout, 
                    R.drawable.ic_drawer,
                    R.string.ns_menu_open, 
                    R.string.ns_menu_close);
            }

            @Override
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(getString(R.string.ns_menu_close));
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(getString(R.string.ns_menu_open));
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        }

        private class DrawerItemClickListener implements ListView.OnItemClickListener {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
                // Highlight the selected item, update the title, and close the drawer
                // update selected item and title, then close the drawer
                mDrawerList.setItemChecked(position, true);

                mDrawer.closeDrawer(mDrawerList);

            }

        }
}

这不起作用,昨天它起作用了,现在它崩溃了。 我尝试了一切,但没有找到解决方案。我希望你能帮助我。

  

07-02 12:04:24.715: E/Trace(3282): error opening trace file: No such file or directory (2)
07-02 12:04:32.394: E/dalvikvm(3282): Could not find class 'android.support.v4.widget.DrawerLayout', referenced from method com.example.talkreflection.StartActivity.onCreate
07-02 12:04:32.705: E/AndroidRuntime(3282): FATAL EXCEPTION: main
07-02 12:04:32.705: E/AndroidRuntime(3282): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.talkreflection/com.example.talkreflection.StartActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class android.support.v4.widget.DrawerLayout
07-02 12:04:32.705: E/AndroidRuntime(3282):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-02 12:04:32.705: E/AndroidRuntime(3282):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-02 12:04:32.705: E/AndroidRuntime(3282):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-02 12:04:32.705: E/AndroidRuntime(3282):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-02 12:04:32.705: E/AndroidRuntime(3282):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-02 12:04:32.705: E/AndroidRuntime(3282):     at android.os.Looper.loop(Looper.java:137)
07-02 12:04:32.705: E/AndroidRuntime(3282):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-02 12:04:32.705: E/AndroidRuntime(3282):     at java.lang.reflect.Method.invokeNative(Native Method)
07-02 12:04:32.705: E/AndroidRuntime(3282):     at java.lang.reflect.Method.invoke(Method.java:511)
07-02 12:04:32.705: E/AndroidRuntime(3282):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-02 12:04:32.705: E/AndroidRuntime(3282):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-02 12:04:32.705: E/AndroidRuntime(3282):     at dalvik.system.NativeStart.main(Native Method)
07-02 12:04:32.705: E/AndroidRuntime(3282): Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class android.support.v4.widget.DrawerLayout
07-02 12:04:32.705: E/AndroidRuntime(3282):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698)
07-02 12:04:32.705: E/AndroidRuntime(3282):     at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
07-02 12:04:32.705: E/AndroidRuntime(3282):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-02 12:04:32.705: E/AndroidRuntime(3282):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
07-02 12:04:32.705: E/AndroidRuntime(3282):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
07-02 12:04:32.705: E/AndroidRuntime(3282):     at android.app.Activity.setContentView(Activity.java:1881)
07-02 12:04:32.705: E/AndroidRuntime(3282):     at com.example.talkreflection.StartActivity.onCreate(StartActivity.java:29)
07-02 12:04:32.705: E/AndroidRuntime(3282):     at android.app.Activity.performCreate(Activity.java:5104)
07-02 12:04:32.705: E/AndroidRuntime(3282):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-02 12:04:32.705: E/AndroidRuntime(3282):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-02 12:04:32.705: E/AndroidRuntime(3282):     ... 11 more
07-02 12:04:32.705: E/AndroidRuntime(3282): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v4.widget.DrawerLayout" on path: /data/app/com.example.talkreflection-2.apk
07-02 12:04:32.705: E/AndroidRuntime(3282):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
07-02 12:04:32.705: E/AndroidRuntime(3282):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
07-02 12:04:32.705: E/AndroidRuntime(3282):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
07-02 12:04:32.705: E/AndroidRuntime(3282):     at android.view.LayoutInflater.createView(LayoutInflater.java:552)
07-02 12:04:32.705: E/AndroidRuntime(3282):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
07-02 12:04:32.705: E/AndroidRuntime(3282):     ... 20 more
抱歉我的英语不好。

1 个答案:

答案 0 :(得分:8)

不会使用APK文件导出支持库。确保你的libs文件夹中有andorid-support.jar(或类似的文件),然后转到你项目的属性。

(如果您没有该jar文件,请右键单击该项目并选择“Android工具” - >“添加支持库...”。)

在项目属性中转到Java Build Path - &gt;订购和导出,并确保选中Android私有库。然后清理您的项目并尝试再次运行该应用程序。

我假设您正在使用Eclipse。