使用自定义导航抽屉运行活动时出错

时间:2016-04-24 08:21:19

标签: android android-navigation-drawer

当我尝试运行以下活动时出现错误。 该活动有一个自定义导航抽屉。 我是android新手.... 错误发生在涉及动作栏的地方。

当我尝试运行以下活动时出现错误。 该活动有一个自定义导航抽屉。 我是android新手.... 错误发生在涉及动作栏的地方。

当我尝试运行以下活动时出现错误。 该活动有一个自定义导航抽屉。 我是android新手.... 错误发生在涉及动作栏的地方。

活动

import android.app.ActionBar;
import android.content.Context;
import android.support.v4.widget.DrawerLayout;

import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class HomeActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    DrawerLayout drawer_layout;
    ListView drawer_list;
    ActionBarDrawerToggle drawer_toggle;
    ActionBar abar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        drawer_layout= (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer_list= (ListView) findViewById(R.id.drawer_list);
        drawer_list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1));
        drawer_list.setOnItemClickListener(this);
        abar=getActionBar();
        /*drawer_toggle = new ActionBarDrawerToggle(this, drawer_layout,
                R.drawable.tick, R.string.app_name, R.string.app_name) {

            *//** Called when a drawer has settled in a completely closed state. *//*
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                getActionBar().setTitle("Yuride");
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            *//** Called when a drawer has settled in a completely open state. *//*
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                getActionBar().setTitle("Yuride");
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };*/
        abar.setHomeButtonEnabled(true);
        abar.setDisplayHomeAsUpEnabled(true);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    class Myadapter extends BaseAdapter
    {
        Context context;
        String[] drawer_items;
        public Myadapter(Context context)
        {
            this.context=context;
            drawer_items=context.getResources().getStringArray(R.array.drawer_items);
        }
        @Override
        public int getCount() {
            return drawer_items.length;
        }

        @Override
        public Object getItem(int position) {
            return drawer_items[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row=null;
            if(convertView==null)
            {
                LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row=inflater.inflate(R.layout.drawer_row,parent,false);
            }
            else
            {
                row=convertView;
            }
            ImageView drawer_icon= (ImageView) row.findViewById(R.id.drawer_icon);
            TextView drawer_text= (TextView) row.findViewById(R.id.drawer_text);
            drawer_text.setText(drawer_items[position]);
            drawer_icon.setImageResource(R.drawable.tick);
            return null;
        }
    }
}

XML

    <android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawer_layout">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>
    <ListView
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:id="@+id/drawer_list">
    </ListView>
</android.support.v4.widget.DrawerLayout>

清单

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.yusoft.yuride.yuride">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".SplashActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Entrance" />
        <activity android:name=".HomeActivity"></activity>
    </application>

</manifest>

错误

java.lang.RuntimeException: Unable to start activity ComponentInfo{in.yusoft.yuride.yuride/in.yusoft.yuride.yuride.HomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setHomeButtonEnabled(boolean)' on a null object reference
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2622)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2683)
   at android.app.ActivityThread.-wrap12(ActivityThread.java)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1440)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:148)
   at android.app.ActivityThread.main(ActivityThread.java:6066)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:770)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:660)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setHomeButtonEnabled(boolean)' on a null object reference
   at in.yusoft.yuride.yuride.HomeActivity.onCreate(HomeActivity.java:52)
   at android.app.Activity.performCreate(Activity.java:6584)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2575)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2683) 
   at android.app.ActivityThread.-wrap12(ActivityThread.java) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1440) 
   at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:148) 
   at android.app.ActivityThread.main(ActivityThread.java:6066) 
   at java.lang.reflect.Method.invoke(Native Method) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:770) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:660) 

1 个答案:

答案 0 :(得分:0)

问题在于设置ActionBar。 而不是

    abar=getActionBar();
    abar.setHomeButtonEnabled(true);
    abar.setDisplayHomeAsUpEnabled(true);

用XML写:

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

     <android.support.design.widget.AppBarLayout
       android:id="@+id/app_bar"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:fitsSystemWindows="true"
       android:theme="@style/MyMaterialTheme.AppBarOverlay">

         <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/MyMaterialTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

     <FrameLayout
         android:layout_width="match_parent"
         android:layout_height="match_parent">
     </FrameLayout>
     <ListView
         android:layout_width="240dp"
         android:layout_height="match_parent"
         android:layout_gravity="left"
         android:id="@+id/drawer_list">
     </ListView>
 </android.support.v4.widget.DrawerLayout>

在setContentView(R.layout.activity_home)之后的Java类中;:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    if(getSupportActionBar()!=null){
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }