导航抽屉不显示在多个活动中

时间:2014-08-26 09:30:24

标签: android navigation-drawer slidingdrawer

这是抽屉活动代码 滑动:

public class sliding extends Activity {

// Within which the entire activity is enclosed
private DrawerLayout mDrawerLayout;

// ListView represents Navigation Drawer
private ListView mDrawerList;

// ActionBarDrawerToggle indicates the presence of Navigation Drawer in the
// action bar
private ActionBarDrawerToggle mDrawerToggle;

// Title of the action bar
private String mTitle = "";

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_sliding);

}

void Drawer() {
    mTitle = "Care&Cure";
    getActionBar().setTitle(mTitle);

    // Getting reference to the DrawerLayout
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    mDrawerList = (ListView) findViewById(R.id.drawer_list);

    // Getting reference to the ActionBarDrawerToggle
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ic_drawer, R.string.drawer_open,
            R.string.drawer_close) {

        /** Called when drawer is closed */
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
            invalidateOptionsMenu();

        }

        /** Called when a drawer is opened */
        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle("Care&Cure");
            invalidateOptionsMenu();
        }

    };

    // Setting DrawerToggle on DrawerLayout
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    // Creating an ArrayAdapter to add items to the listview mDrawerList
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
            getBaseContext(), R.layout.drawer_list_item, getResources()
                    .getStringArray(R.array.menus));

    // Setting the adapter on mDrawerList
    mDrawerList.setAdapter(adapter);

    // Enabling Home button
    getActionBar().setHomeButtonEnabled(true);

    // Enabling Up navigation
    getActionBar().setDisplayHomeAsUpEnabled(true);

    // Setting item click listener for the listview mDrawerList
    mDrawerList.setOnItemClickListener(new OnItemClickListener() {

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

        }
    });
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/** Called whenever we call invalidateOptionsMenu() */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    // If the drawer is open, hide action items related to the content view
    boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);

    menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
    return super.onPrepareOptionsMenu(menu);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

和主要活动     MainActivty:

public class MainActivity extends sliding implements OnClickListener {

ImageButton hosp, doc;

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



    hosp = (ImageButton) findViewById(R.id.btn_hosp);
    doc = (ImageButton) findViewById(R.id.btn_doc);

    hosp.setOnClickListener(this);
    doc.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    int id = v.getId();
    if (id == R.id.btn_doc) {
        Intent dc = new Intent(MainActivity.this, sliding.class);
        startActivity(dc);
    }

activity_main.xml中

 <?xml version="1.0" encoding="utf-8"?>
<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" >

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bacground" >

    <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <!-- 2 columns -->

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp" >

            <ImageButton
                android:id="@+id/btn_hos"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/selector"
                android:contentDescription="@string/image"
                android:src="@drawable/hos" />

            <ImageButton
                android:id="@+id/btn_doc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/selector"
                android:contentDescription="@string/image"
                android:src="@drawable/doc" />
        </TableRow>


        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp" >

            <ImageButton
                android:id="@+id/btn_lab"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/selector"
                android:contentDescription="@string/image"
                android:src="@drawable/lab" />

            <ImageButton
                android:id="@+id/btn_b"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/selector"
                android:contentDescription="@string/image"
                android:src="@drawable/b" />
        </TableRow>

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

logcat的:

 08-26 14:25:24.268: W/dalvikvm(17531): threadid=1: thread exiting with uncaught exception (group=0x40aa8228)
 08-26 14:25:24.268: E/AndroidRuntime(17531): FATAL EXCEPTION: main
 08-26 14:25:24.268: E/AndroidRuntime(17531): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.careandcure/com.example.careandcure.MainActivity}: java.lang.NullPointerException
 08-26 14:25:24.268: E/AndroidRuntime(17531):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
 08-26 14:25:24.268: E/AndroidRuntime(17531):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
 08-26 14:25:24.268: E/AndroidRuntime(17531):   at android.app.ActivityThread.access$600(ActivityThread.java:139)
 08-26 14:25:24.268: E/AndroidRuntime(17531):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
 08-26 14:25:24.268: E/AndroidRuntime(17531):   at android.os.Handler.dispatchMessage(Handler.java:99)
 08-26 14:25:24.268: E/AndroidRuntime(17531):   at android.os.Looper.loop(Looper.java:156)
 08-26 14:25:24.268: E/AndroidRuntime(17531):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
 08-26 14:25:24.268: E/AndroidRuntime(17531):   at dalvik.system.NativeStart.main(Native Method)
  08-26 14:25:24.268: E/AndroidRuntime(17531): Caused by: java.lang.NullPointerException
  08-26 14:25:24.268: E/AndroidRuntime(17531):  at com.example.careandcure.sliding.Drawer(sliding.java:78)
  08-26 14:25:24.268: E/AndroidRuntime(17531):  at com.example.careandcure.MainActivity.onCreate(MainActivity.java:20)
  08-26 14:25:24.268: E/AndroidRuntime(17531):  at android.app.Activity.performCreate(Activity.java:4538)
  08-26 14:25:24.268: E/AndroidRuntime(17531):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
  08-26 14:25:24.268: E/AndroidRuntime(17531):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)

我跟随Android Navigation Drawer with multiple activites在多个活动中创建抽屉,它在滑动时工作得很好但在主要活动中没有...给出null pointer exception。如何解决它....提前感谢

2 个答案:

答案 0 :(得分:1)

您在覆盖setContentView()的类中调用sliding,在该类中传递要膨胀的新xml文件(R.layout.activity_main)。我的猜测是这个xml文件没有包含sliding Activity所需的布局。

您所关注的示例并不是一个好例子。您正在尝试拥有一个托管NavigationDrawer的Activity,然后扩展该Activity。这个概念很好,但你的执行是错误的。您的滑块类需要能够创建并显示NavigationDrawer并且仍然可以扩展,因此您需要以覆盖标准方法(如onCreate()和调用setContentView())的方式编写它)不影响滑块类显示NavigationDrawer

的能力

我建议在片段中创建NavigationDrawer,并在滑动onCreate()的{​​{1}}中对该片段进行充气。这样Activity完全包含在片段中,您不必担心覆盖哪些方法以及使用它们做什么。

答案 1 :(得分:0)

您正在尝试查找ID为btn_hos和btn_hosp的按钮。它给出了一个空指针。

  

hosp =(ImageButton)findViewById( R.id.btn_hosp );

<ImageButton
   android:id="@+id/btn_hos" <----------
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_weight="1"
   android:background="@drawable/selector"
   android:contentDescription="@string/image"
   android:src="@drawable/hos" />