通过扩展BaseActivity在不同的活动中使用导航抽屉

时间:2014-02-19 22:55:16

标签: android android-activity nullpointerexception navigation-drawer

我正试图让HomeActivity扩展BaseActivity(如建议的here),这样我就可以在我想要的每个活动中使用相同的导航抽屉。但是我在HomeActivity的onCreate方法中获得NullPointerException。这是BaseActivity的代码:

import android.os.Bundle;
import android.app.Activity;
import android.content.res.Configuration;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class BaseActivity extends Activity
{
    public DrawerLayout drawerLayout;
    public ListView drawerList;
    private String[] drawerListEntries;
    private ActionBarDrawerToggle drawerToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {

        drawerListEntries = new String[] {"Bla bla", "Profile", "Home"};

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

        drawerToggle = new ActionBarDrawerToggle((Activity) this, drawerLayout, R.drawable.ic_navigation_drawer, 0, 0) 
        {
            public void onDrawerClosed(View view) 
            {
                getActionBar().setTitle(R.string.app_name);
            }

            public void onDrawerOpened(View drawerView) 
            {
                getActionBar().setTitle(R.string.hello_world);
            }
        };
        drawerLayout.setDrawerListener(drawerToggle);

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

        //layers = getResources().getStringArray(R.array.layers_array);
        drawerList = (ListView) findViewById(R.id.drawer_list);
//        View header = getLayoutInflater().inflate(R.layout.drawer_list_header, null);
//        drawerList.addHeaderView(header, null, false);
        drawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, drawerListEntries));
//        View footerView = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
//                R.layout.drawer_list_footer, null, false);
//        drawerList.addFooterView(footerView);

        drawerList.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
                //map.drawerClickEvent(pos);
                String selectedValue = (String) drawerList.getAdapter().getItem(pos);
                Toast.makeText(getBaseContext(), selectedValue, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (drawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);

    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        drawerToggle.syncState();
    }

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

这是扩展BaseActivity的HomeActivity代码:

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.facebook.Session;

public class HomeActivity extends BaseActivity {

    TextView greeting;
    Button orderButton, deliverButton, logout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_home);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.home, menu);
        //return super.onCreateOptionsMenu(menu);
        return true;
    }


}

我在:

中得到NullPointerException
super.onCreate(savedInstanceState); // HomeActivity

当我点击它时,它会转到:

drawerLayout.setDrawerListener(drawerToggle); // BaseActivity

有谁知道如何解决这个问题?

更新:

drawer_list.xml:

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


    <ListView
        android:id="@+id/drawer_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="5dp"
        android:background="@android:color/transparent"
        android:cacheColorHint="@android:color/transparent"
        android:divider="#CCCCCC"
        android:dividerHeight="1dp"
        android:paddingLeft="2dp" />


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

drawer_list_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="@+id/drawer_list_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="12dp" 
        android:textSize="24sp" 
        android:layout_gravity="left"
        android:textColor="@color/belize_hole"
        android:fontFamily="sans-serif-light">
    </TextView>



</LinearLayout>

1 个答案:

答案 0 :(得分:2)

一些帖子中的相同问题。我是android的新手但是这样解决了。如果有更好的&#34;对&#34;这样做的方式。 (是的,没有片段,但我知道我最终需要学习它们。)

问题似乎是基类不知道布局。不知道如何或为什么它为其他人工作而不必做这样的事情..但我是Android新手。

我得到了同样的错误,并通过onCreate将我的布局传递给基地来解决。

所以在基础中,我将onCreate修改为:

protected void onCreate(Bundle savedInstanceState, int resLayoutID)
{
    super.onCreate(savedInstanceState);
    setContentView(resLayoutID);

然后,从我的新活动开始,我有了这个:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState, R.layout.my_activity);

我的基础中的代码现在可以使用而不是保持为null:

drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerList = (ListView) findViewById(R.id.left_drawer);
顺便说一下,这个结果是朋友向我推荐的,对我有用。