简体   繁体   English

将导航抽屉活动扩展到其他活动

[英]extending navigation drawer activity to other activities

I'm trying to create a navigation drawer activity so I can extend that activity and use the menu in all activities by following the answer given in this question Link but my test app keeps crashing, here's my code: 我正在尝试创建一个导航抽屉活动,以便我可以扩展该活动,并按照此问题中给出的答案链接所有活动中的菜单但是我的测试应用程序一直在崩溃,这是我的代码:

BaseActivity.java BaseActivity.java

public class BaseActivity extends Activity {

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

    protected void onCreate(Bundle savedInstanceState) {
        // 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));

    }

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

MainActivity.java MainActivity.java

public class MainActivity extends BaseActivity {

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

}

activity_profile.xml 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>

Logcat logcat的

03-03 01:22:18.031: D/AndroidRuntime(27902): Shutting down VM
03-03 01:22:18.031: W/dalvikvm(27902): threadid=1: thread exiting with uncaught exception (group=0x2b542210)
03-03 01:22:18.041: E/AndroidRuntime(27902): FATAL EXCEPTION: main
03-03 01:22:18.041: E/AndroidRuntime(27902): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.slider/com.example.slider.MainActivity}: java.lang.NullPointerException
03-03 01:22:18.041: E/AndroidRuntime(27902):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
03-03 01:22:18.041: E/AndroidRuntime(27902):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
03-03 01:22:18.041: E/AndroidRuntime(27902):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
03-03 01:22:18.041: E/AndroidRuntime(27902):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
03-03 01:22:18.041: E/AndroidRuntime(27902):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-03 01:22:18.041: E/AndroidRuntime(27902):    at android.os.Looper.loop(Looper.java:137)
03-03 01:22:18.041: E/AndroidRuntime(27902):    at android.app.ActivityThread.main(ActivityThread.java:4441)
03-03 01:22:18.041: E/AndroidRuntime(27902):    at java.lang.reflect.Method.invokeNative(Native Method)
03-03 01:22:18.041: E/AndroidRuntime(27902):    at java.lang.reflect.Method.invoke(Method.java:511)
03-03 01:22:18.041: E/AndroidRuntime(27902):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-03 01:22:18.041: E/AndroidRuntime(27902):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-03 01:22:18.041: E/AndroidRuntime(27902):    at dalvik.system.NativeStart.main(Native Method)
03-03 01:22:18.041: E/AndroidRuntime(27902): Caused by: java.lang.NullPointerException
03-03 01:22:18.041: E/AndroidRuntime(27902):    at com.example.slider.BaseActivity.onCreate(BaseActivity.java:35)
03-03 01:22:18.041: E/AndroidRuntime(27902):    at com.example.slider.MainActivity.onCreate(MainActivity.java:12)
03-03 01:22:18.041: E/AndroidRuntime(27902):    at android.app.Activity.performCreate(Activity.java:4465)
03-03 01:22:18.041: E/AndroidRuntime(27902):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-03 01:22:18.041: E/AndroidRuntime(27902):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
03-03 01:22:18.041: E/AndroidRuntime(27902):    ... 11 more

The problem appears that the base class is not aware of the layout. 问题似乎是基类不知道布局。 Not sure how or why it worked for others without having to do something like this.. but I am new to Android. 不知道如何或为什么它为其他人工作而不必做这样的事情..但我是Android新手。

I was getting the same error and solved by passing my layout to the base via onCreate. 我得到了同样的错误,并通过onCreate将我的布局传递给基础解决了。

So in the base, I modified onCreate to be like this: 所以在基础上,我修改onCreate是这样的:

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

Then, from my new activity, I have this: 然后,从我的新活动,我有这个:

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

This code in my base now works instead of staying null: 我的基础中的代码现在可以工作而不是保持为null:

drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerList = (ListView) findViewById(R.id.left_drawer);

btw, this result was suggested to me by a friend and it worked for me. 顺便说一句,这个结果是朋友向我推荐的,它对我有用。

在main活动中,你必须在setContentView()之后调用super.onCreate(savedInstanceState)

On the parent classBaseActivity : 在父classBaseActivity上

  • declare a protected abstract int getLayoutId(); 声明一个protected abstract int getLayoutId();

  • onCreate() I added the line onCreate()我添加了这一行

     setContentView(getLayoutId()); 

Now implement it on your subclass MainActivity something like: 现在在您的子类MainActivity上实现它,如:

 protected int getLayoutId() {
    return R.layout.activity_profile;
}

Please tell me if you know a better solution. 如果您知道更好的解决方案,请告诉我。

As suggested by oli, you need to call setContentView(R.layout.activity_view) in your main activity . 正如oli所建议的那样,您需要在主活动中调用setContentView(R.layout.activity_view)

BaseActivity's onCreate must be called once content view in Main Activity has been set as shown below : 一旦设置了Main Activity中的内容视图,就必须调用BaseActivity的onCreate ,如下所示:

public class BaseActivity extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // REST Navigation drawer initialization
    }

}



public class MainActivity extends BaseActivity {

    protected void onCreate(Bundle savedInstanceState) {

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

        // REST APPLICATION CODE
    }
}

Your xml file should look something like this, taken from the android website: http://developer.android.com/training/implementing-navigation/nav-drawer.html 你的xml文件看起来应该是这样的,取自android网站: http//developer.android.com/training/implementing-navigation/nav-drawer.html

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

In you MainActivity just put set setContentView(R.layout.activity_main) before 在你MainActivity中之前只需设置setContentView(R.layout.activity_main)

super.onCreate(savedInstanceState);

like below: 如下:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM