0

当我启动应用程序时,引用字符串值存在一些问题,它们是 navigation_drawer_open 和 navigation_drawer_close。此外,问题出现在 ActionBarDrawerToggle 以及drawer.addDrawerListener 语法中。

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

                Toolbar toolbar1 = findViewById(R.id.toolbar);
                setSupportActionBar(toolbar1);

                drawer=findViewById(R.id.drawer_layout);

                final ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar1,
                        R.string.navigation_drawer_open,
                        R.string.navigation_drawer_close);
                drawer.addDrawerListener(toggle);

                toggle.syncState();

            }

            @Override
            public void onBackPressed() {
                if(drawer.isDrawerOpen(GravityCompat.START)) {
                    drawer.closeDrawer(GravityCompat.START);
                } else
                {
                    super.onBackPressed();
                }

            }

由于我添加了 nav_draw 的页面,它不能进入​​主页(因为我有登录和注册),所以我创建了菜单的第一页: input.xml 引用它的菜单类。layout: input.xml // 没有什么,只是空布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello world" />
</LinearLayout>

最后一个是价值观。引用它的语法 ActionBarDrawerToggle。

    strings.xml: // the string values

    <resources>
        <string name="app_name">InformX</string>
        <string name="start">Start</string>
        <string name="title_activity_2">activity_2</string>
        <string name="todo">TODO</string>
        <string name="name">Name</string>
        <string name="first_name">First Name</string>
        <string name="title_activity_menu">menu</string>
        <string name="navigation_drawer_open">Open navigation drawer</string> //This one
        <string name="navigation_drawer_close">Close navigation drawer</string> // And this
        <string name="nav_header_title">Android Studio</string>
        <string name="nav_header_subtitle">android.studio@android.com</string>
        <string name="nav_header_desc">Navigation header</string>
        <string name="action_settings">Settings</string>

        <string name="menu_home">Home</string>
        <string name="menu_gallery">Gallery</string>
        <string name="menu_slideshow">Slideshow</string>
        <string name="menu_tools">Tools</string>
        <string name="menu_share">Share</string>
        <string name="menu_send">Send</string>
        <string name="tab_text_1">Tab 1</string>
        <string name="tab_text_2">Tab 2</string>
        <string name="hello_blank_fragment">Hello blank fragment</string>


        </resources>

日志猫:

Error:  
    Process: com.example.myapp, PID: 31593
        java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.myapp/com.example.myapp.menu}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2548)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
            at android.app.ActivityThread.-wrap12(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:154)
            at android.app.ActivityThread.main(ActivityThread.java:6077)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
         Caused by: java.lang.NullPointerException: Attempt to invoke virtual //The main problem method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
            at android.content.ContextWrapper.getResources(ContextWrapper.java:86)
            at android.view.ContextThemeWrapper.getResourcesInternal(ContextThemeWrapper.java:127)
            at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:121)
            at androidx.appcompat.app.AppCompatActivity.getResources(AppCompatActivity.java:566)
            at com.example.myapp.menu.<init>(menu.java:13)
            at java.lang.Class.newInstance(Native Method)
            at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2538)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
            at android.app.ActivityThread.-wrap12(ActivityThread.java) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:154) 
            at android.app.ActivityThread.main(ActivityThread.java:6077) 
            at java.lang.reflect.Method.invoke(Native Method) 
            atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 
4

1 回答 1

0

缺少 id="toolbar"的活动调用setContentView(R.layout.input),因此失败。也会失败,因为id=" drawer_layout " 它在. 您可以尝试这样的布局:input.xmlToolbarfindViewById(R.id.toolbar)findViewById(R.id.drawer_layout)DrawerLayoutinput.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"
    android:fitsSystemWindows="true">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="#ff6d7fe2"
        android:minHeight="?attr/actionBarSize"
        app:contentInsetEnd="0dp"
        app:contentInsetLeft="0dp"
        app:contentInsetRight="0dp"
        app:contentInsetStart="0dp"
        />


        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Hello world" />

     </LinearLayout>

</android.support.v4.widget.DrawerLayout>
于 2020-02-29T10:09:08.917 回答