gpt4 book ai didi

java - 空对象引用上的 void android.support.v7.widget.Toolbar.setTitle(java.lang.CharSequence)'

转载 作者:行者123 更新时间:2023-11-30 00:55:19 29 4
gpt4 key购买 nike

一开始:我在这里尝试了所有解决方案。

FATAL EXCEPTION: main
Process: com.justfashion, PID: 5068
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.justfashion/com.justfashion.ActivityMainWallet}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.Toolbar.setTitle(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2521)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2595)
at android.app.ActivityThread.access$800(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5631)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)

带有错误行的 ActivityMainWallet.java:

drawerToolbar = (Toolbar)findViewById(R.id.drawer_toolbar);
drawerToolbar.setTitle(getString(R.string.app_name));
drawerToolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
drawerToolbar.setNavigationOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {

主要布局 XML: http://pastebin.com/4UjqqZUk

非常感谢你们!

编辑

我现在在我的 java 文件中:

  drawerToolbar = (Toolbar)findViewById(R.id.toolbarInner);
drawerToolbar.setTitle(getString(R.string.app_name));
drawerToolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
drawerToolbar.setNavigationOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
openAndCloseDrawer();
}
});

还有 toolbar.xml 中:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/toolbar" >

<android.support.v7.widget.Toolbar
android:id="@+id/toolbarInner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:titleTextAppearance="@style/AppTheme.Widget.Toolbar.Title"
android:theme="@style/AppTheme.Widget.Toolbar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:elevation="8dp"
android:paddingBottom="0dp"
android:layout_margin="0dp"/>


</RelativeLayout>

在主 xml 布局中也没有任何变化。 同样的错误。帮帮我!

最佳答案

drawerToolbar = (Toolbar)findViewById(R.id.toolbarInner);

should under the setContentView(R.layout.your_loayout);

因为否则 findViewById() 不知道如何找到 R.id.toolbarInner或要查找的文件。

关于java - 空对象引用上的 void android.support.v7.widget.Toolbar.setTitle(java.lang.CharSequence)',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40272164/

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