gpt4 book ai didi

java - 在MainActivity中使用我的布局资源文件

转载 作者:行者123 更新时间:2023-12-03 18:13:58 25 4
gpt4 key购买 nike

我正在遵循“ Apps entwickeln mit Android Studio 5”教程,该指南使我通过New > Layout resource file创建了自己的Layout资源文件,然后为其指定了文件名scroll_design和根元素ScrollView

我使用自己喜欢的小部件在XML编辑器/设计编辑器中创建自己想要的布局。

<?xml version="1.0" encoding="utf-8"?>




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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="My custom Layout File"
android:id="@+id/textView2"
android:layout_gravity="center_horizontal"
android:layout_margin="32sp" />
</LinearLayout>




然后该教程告诉我转到MainActivity并更改代码中的布局,但是当我这样做时:将 setContentView(R.layout.activity_main);更改为 setContentView(R.layout.scroll_design);并使用API​​21启动模拟器,它将使我的应用程序崩溃,并且“已停止工作”,并且几行错误,直到我将布局更改回activity_main.xml

那么,如何将新创建的xml布局连接到我的项目文档?

Logcat:

11-08 20:31:25.187 25776-25776/de.myworkplace.layouts2 E/AndroidRuntime: FATAL EXCEPTION: main
11-08 20:31:25.187 25776-25776/de.myworkplace.layouts2 E/AndroidRuntime: Process: de.myworkplace.layouts2, PID: 25776
11-08 20:31:25.187 25776-25776/de.myworkplace.layouts2 E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{de.myworkplace.layouts2/de.myworkplace.layouts2.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
11-08 20:31:25.187 25776-25776/de.myworkplace.layouts2 E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
11-08 20:31:25.187 25776-25776/de.myworkplace.layouts2 E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
11-08 20:31:25.187 25776-25776/de.myworkplace.layouts2 E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:144)
11-08 20:31:25.187 25776-25776/de.myworkplace.layouts2 E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
11-08 20:31:25.187 25776-25776/de.myworkplace.layouts2 E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
11-08 20:31:25.187 25776-25776/de.myworkplace.layouts2 E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
11-08 20:31:25.187 25776-25776/de.myworkplace.layouts2 E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5221)
11-08 20:31:25.187 25776-25776/de.myworkplace.layouts2 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-08 20:31:25.187 25776-25776/de.myworkplace.layouts2 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-08 20:31:25.187 25776-25776/de.myworkplace.layouts2 E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
11-08 20:31:25.187 25776-25776/de.myworkplace.layouts2 E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
11-08 20:31:25.187 25776-25776/de.myworkplace.layouts2 E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()' on a null object reference
11-08 20:31:25.187 25776-25776/de.myworkplace.layouts2 E/AndroidRuntime: at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:100)
11-08 20:31:25.187 25776-25776/de.myworkplace.layouts2 E/AndroidRuntime: at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:93)
11-08 20:31:25.187 25776-25776/de.myworkplace.layouts2 E/AndroidRuntime: at android.support.v7.internal.app.ToolbarActionBar.<init>(ToolbarActionBar.java:78)
11-08 20:31:25.187 25776-25776/de.myworkplace.layouts2 E/AndroidRuntime: at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:206)
11-08 20:31:25.187 25776-25776/de.myworkplace.layouts2 E/AndroidRuntime: at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:99)
11-08 20:31:25.187 25776-25776/de.myworkplace.layouts2 E/AndroidRuntime: at de.myworkplace.layouts2.MainActivity.onCreate(MainActivity.java:19)
11-08 20:31:25.187 25776-25776/de.myworkplace.layouts2 E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5937)
11-08 20:31:25.187 25776-25776/de.myworkplace.layouts2 E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
11-08 20:31:25.187 25776-25776/de.myworkplace.layouts2 E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
11-08 20:31:25.187 25776-25776/de.myworkplace.layouts2 E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
11-08 20:31:25.187 25776-25776/de.myworkplace.layouts2 E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:144) 
11-08 20:31:25.187 25776-25776/de.myworkplace.layouts2 E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
11-08 20:31:25.187 25776-25776/de.myworkplace.layouts2 E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102) 
11-08 20:31:25.187 25776-25776/de.myworkplace.layouts2 E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135) 
11-08 20:31:25.187 25776-25776/de.myworkplace.layouts2 E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5221) 
11-08 20:31:25.187 25776-25776/de.myworkplace.layouts2 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 
11-08 20:31:25.187 25776-25776/de.myworkplace.layouts2 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372) 
11-08 20:31:25.187 25776-25776/de.myworkplace.layouts2 E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
11-08 20:31:25.187 25776-25776/de.myworkplace.layouts2 E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
11-08 20:31:25.191 1230-1600/system_process W/ActivityManager: Force finishing activity de.myworkplace.layouts2/.MainActivity


enter image description here

最佳答案

问题的根源在Java文件中。

您引用的是在ActionBar中存在但在activity_main中不存在的scroll_design
检查您的#onCreate()方法,应该在其中getActionBar()getSupportActionBar()

您的异常消息是:


尝试在空对象引用上调用虚拟方法'java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()'


那是对ActionBar的引用上的NullPointerException。

关于java - 在MainActivity中使用我的布局资源文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33598791/

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