0

我正在尝试使用带有一些活动且没有片段的导航抽屉。我在这里阅读了一些答案,最后得到了这个,我遇到了与用户 Dediqated 相同的问题,这是我的代码:

BaseActivity.java:

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

public class BaseActivity extends Activity {

    public DrawerLayout drawerLayout;
    public ListView drawerList;
    public String[] layers;
    private ActionBarDrawerToggle drawerToggle;

    protected void onCreate(Bundle savedInstanceState) {
        try {
            // R.id.drawer_layout should be in every activity with exactly the same
            // id.
            drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

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

                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.planets_array);
            drawerList = (ListView) findViewById(R.id.left_drawer);
            drawerList.setAdapter(new ArrayAdapter<String>(this,
                    R.layout.drawer_list_item, android.R.id.text1, layers));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            e.printStackTrace();
            e.printStackTrace();
        }

    }

    @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);
    }
}

测试活动.java:

import android.os.Bundle;

public class TestActivity extends BaseActivity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);
    }
}

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

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

    <!-- 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="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

activity_profile.xml:

<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"
    tools:context=".ProfileActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

抽屉列表项目.xml

<!--
  Copyright 2013 The Android Open Source Project

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
  -->

<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:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:textColor="#fff"
    android:background="?android:attr/activatedBackgroundIndicator"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"/>

在 BaseActivity.java 中,drawerLayout.setDrawerListener(drawerToggle);Eclipse 会抛出 NullPointerException,我只是不明白为什么。

很抱歉针对已经提出的相同问题提出一个额外的问题,但我是新手,不允许添加评论。

4

2 回答 2

2

你在做什么不能工作,因为你在设置内容视图findViewById 之前打电话。这意味着您甚至在设置布局之前就尝试访问 Activity 布局中的元素,或者换句话说,您是View.findViewById在初始化视图之前调用的。
另外,据我所知,您必须调用super.onCreate您的活动。你没有这样做。你打电话给super.onCreate你,TestActivity但你必须在你的BaseActivity.
我没有与 AB 合作过几个活动,但我建议执行以下操作:

基本活动

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

void initDrawer(){
    // put everything you have in your onCreate in here:
    try {
        // R.id.drawer_layout should be in every activity with exactly the same
        // id.
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

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

            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.planets_array);
        drawerList = (ListView) findViewById(R.id.left_drawer);
        drawerList.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_list_item, android.R.id.text1, layers));
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        e.printStackTrace();
        e.printStackTrace();
    }

}

测试活动

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);
    initDrawer();
}
于 2014-04-02T09:51:54.777 回答
0

BaseActivity 的方法并不适合我。原因是 Activity 和 Fragments 的混合以及 NavigationDrawerFragment 的使用,它是在 Eclipse 中创建新项目时生成的。

我有一个包含 5 个项目的菜单的活动 A。一个项目将启动另一个活动(因为它是一个 FragmentPagerAdaper),其他项目将只是切换 Activity A 中的 Fragment。

当我使用 BaseActivity 时,我将只有一个 onNavigationDrawerItemSelected(int) (点击事件)。

在 Activity B 中使用它也会导致问题,因为我需要先切换回 Activity A,然后再显示相应的 Fragment。这是本质上不同的点击处理。

我最终通过在两个活动中都有 onNavigationDrawerItemSelected(int) 来解决它。实现抽屉只有 2 行,所以这不是太多的双重工作。如果我有更多活动,那么我会选择 BaseActivity,但根据调用活动需要 switch 语句来处理案例。

于 2014-04-07T12:35:57.227 回答