安卓导航抽屉与多个activites [英] Android Navigation Drawer with multiple activites

查看:164
本文介绍了安卓导航抽屉与多个activites的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用导航抽屉与一些活性及无碎片。我读我通过一些答案在这里和<一结束href="http://stackoverflow.com/questions/22137283/extending-navigation-drawer-activity-to-other-activities">this和我心中已经得到了相同的问题,因为用户Dediqated,这里是我的code:

BaseActivity.java:

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

公共类BaseActivity延伸活动{

    公共DrawerLayout drawerLayout;
    公众的ListView drawerList;
    公众的String []层;
    私人ActionBarDrawerToggle drawerToggle;

    保护无效的onCreate(包savedInstanceState){
        尝试 {
            // R.id.drawer_layout应在每一次活动完全一样
            // ID。
            drawerLayout =(DrawerLayout)findViewById(R.id.drawer_layout);

            drawerToggle =新ActionBarDrawerToggle((活动)这一点,drawerLayout,
                    R.drawable.ic_drawer,0,0){
                公共无效onDrawerClosed(查看视图){
                    。getActionBar()的setTitle(R.string.hello_world);
                }

                公共无效onDrawerOpened(查看drawerView){
                    。getActionBar()的setTitle(R.string.hello_world);
                }
            };
            drawerLayout.setDrawerListener(drawerToggle);

            getActionBar()setDisplayHomeAsUpEnabled(真)。
            getActionBar()setHomeButtonEnabled(真)。

            。层= getResources()getStringArray(R.array.planets_array);
            drawerList =(ListView控件)findViewById(R.id.left_drawer);
            drawerList.setAdapter(新ArrayAdapter&LT;字符串&GT;(这一点,
                    R.layout.drawer_list_item,android.R.id.text1,层));
        }赶上(例外五){
            // TODO自动生成的catch块
            e.printStackTrace();
            e.printStackTrace();
            e.printStackTrace();
        }

    }

    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){

        如果(drawerToggle.onOptionsItemSelected(项目)){
            返回true;
        }
        返回super.onOptionsItemSelected(项目);

    }

    @覆盖
    保护无效onPostCreate(包savedInstanceState){
        super.onPostCreate(savedInstanceState);
        drawerToggle.syncState();
    }

    @覆盖
    公共无效onConfigurationChanged(配置NEWCONFIG){
        super.onConfigurationChanged(NEWCONFIG);
        drawerToggle.onConfigurationChanged(NEWCONFIG);
    }
}
 

TestActivity.java:

 进口android.os.Bundle;

公共类TestActivity扩展BaseActivity {
    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_profile);
    }
}
 

activity_base.xml:

 &LT; android.support.v4.widget.DrawerLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / drawer_layout
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent&GT;

    &LT;! - 其主要内容视图 - &GT;
    &LT;的FrameLayout
        机器人:ID =@ + ID / content_frame
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent&GT;
        &LT;! - 添加的内容在这里 - &GT;
    &LT; /的FrameLayout&GT;

    &LT;! - 导航抽屉 - &GT;
    &LT; ListView的机器人:ID =@ + ID / left_drawer
        机器人:layout_width =240dp
        机器人:layout_height =match_parent
        机器人:layout_gravity =开始
        机器人:choiceMode =singleChoice
        机器人:分隔=@机器人:彩色/透明
        机器人:dividerHeight =0dp
        机器人:后台=#111/&GT;
&LT; /android.support.v4.widget.DrawerLayout>
 

activity_profile.xml:

 &LT; RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:paddingBottom会=@扪/ activity_vertical_margin
    机器人:以下属性来=@扪/ activity_horizo​​ntal_margin
    机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
    机器人:paddingTop =@扪/ activity_vertical_margin
    工具:上下文=ProfileActivity。&GT;

    &LT;的TextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=@字符串/参考hello world/&GT;

&LT; / RelativeLayout的&GT;
 

drawer_list_item.xml

 &LT;! - 
  版权所有2013年Android开源项目

  Apache许可证下授权,版本2.0(以下简称许可证);
  你可能不使用这个文件除了在遵守许可。
  您可以在获得许可证的副本

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

  除非适用法律要求或书面同意,软件
  该许可证下发布分布在原样的基础上,
  无担保或任何形式的条件,无论是EX preSS或暗示的保证。
  查看许可证的特定语言的管理权限和
  该许可证下的限制。
   - &GT;

&LT; TextView中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@机器人:ID / text1中
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:textAppearance =机器人:ATTR / textAppearanceListItemSmall
    机器人:重力=center_vertical
    机器人:以下属性来=16DP
    机器人:paddingRight =16DP
    机器人:文字颜色=#FFF
    机器人:背景=机器人:ATTR / activatedBackgroundIndicator
    机器人:=了minHeight:/&gt;中的Andr​​oid ATTR /列表preferredItemHeightSmall?
 

在BaseActivity.java符合 drawerLayout.setDrawerListener(drawerToggle); 的Eclipse抛出一个NullPointerException异常,我只是不知道为什么 <。 / P>

我很抱歉,使对同样的问题已经问一个额外的问题,但我新,不允许添加注释。

解决方案

你在做什么不行,因为你调用 findViewById 之前设置内容查看。这意味着你正在尝试,你甚至设置布局或者换句话说,要调用之前访问你的活动布局中的元素 View.findViewById 初始化您查看之前。< BR> 另外,据我所知,你必须调用 super.onCreate 在你的活动。您还没有这样做。你叫 super.onCreate TestActivity ,但你必须把它在你的 BaseActivity
我没有一直在与AB的几个活动,但我会建议做到以下几点:

BaseActivity

 保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
}

无效initDrawer(){
    //把所有的东西你必须在你的onCreate在这里:
    尝试 {
        // R.id.drawer_layout应在每一次活动完全一样
        // ID。
        drawerLayout =(DrawerLayout)findViewById(R.id.drawer_layout);

        drawerToggle =新ActionBarDrawerToggle((活动)这一点,drawerLayout,
                R.drawable.ic_drawer,0,0){
            公共无效onDrawerClosed(查看视图){
                。getActionBar()的setTitle(R.string.hello_world);
            }

            公共无效onDrawerOpened(查看drawerView){
                。getActionBar()的setTitle(R.string.hello_world);
            }
        };
        drawerLayout.setDrawerListener(drawerToggle);

        getActionBar()setDisplayHomeAsUpEnabled(真)。
        getActionBar()setHomeButtonEnabled(真)。

        。层= getResources()getStringArray(R.array.planets_array);
        drawerList =(ListView控件)findViewById(R.id.left_drawer);
        drawerList.setAdapter(新ArrayAdapter&LT;字符串&GT;(这一点,
                R.layout.drawer_list_item,android.R.id.text1,层));
    }赶上(例外五){
        // TODO自动生成的catch块
        e.printStackTrace();
        e.printStackTrace();
        e.printStackTrace();
    }

}
 

TestActivity

  @覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_profile);
    initDrawer();
}
 

I'm trying to to use a Navigation Drawer with a few activites and no fragments. I read me through a few answers here and ended up in this and I'v got the same Problem as the user Dediqated, here is my Code:

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

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

drawer_list_item.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"/>

In BaseActivity.java in line drawerLayout.setDrawerListener(drawerToggle); Eclipse throws a NullPointerException and I just don't get why.

I'm sorry to make an extra question for the same problem as already asked, but I'm new and not allowed to add comments.

解决方案

What you are doing can't work because you are calling findViewById before setting the contentview. This means you are trying to access elements within the Layout of your Activity before you even set the Layout or in other words, you are calling View.findViewById before initializing your View.
Also, as far as I know, you have to call super.onCreate in your Activity. You are not doing this. You call super.onCreate in your TestActivity but you have to call it in your BaseActivity.
I haven't been working with AB for several Activities, but I would suggest to do the following:

BaseActivity

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

}

TestActivity

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

这篇关于安卓导航抽屉与多个activites的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆