gpt4 book ai didi

java - 从资源加载时 ActionBarDrawerToggle NullPointerException

转载 作者:行者123 更新时间:2023-12-01 18:11:50 26 4
gpt4 key购买 nike

当我启动应用程序时,引用字符串值时出现一些问题,它们是navigation_drawer_open和navigation_drawer_close。此外,问题出现在 ActionBarDrawerToggle 以及抽屉.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>

最后一个是值(value)观。 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) 

最佳答案

Activity 调用 setContentView(R.layout.input) 但进入 input.xml id="toolbar"的 Toolbar 是缺少,因此 findViewById(R.id.toolbar) 失败。此外,findViewById(R.id.drawer_layout) 也会失败,因为 input.xml 中缺少 id="drawer_layout"的 DrawerLayout。您可以尝试使用这样的布局:

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

关于java - 从资源加载时 ActionBarDrawerToggle NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60455970/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com