在MainActivity

时间:2015-11-08 20:38:19

标签: java xml android-studio

我正在关注“Apps entwickeln mit Android Studio 5”教程,这导致我通过New > Layout resource file创建自己的布局资源文件,然后我给它一个文件名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布局连接到我的项目correclty呢?

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

3 个答案:

答案 0 :(得分:1)

您的问题的根源在您的Java文件中。

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

您的异常消息是:

  

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

ActionBar的引用有 NullPointerException

答案 1 :(得分:0)

-Did你将.xml扩展名添加到scroll_design文件中了吗? - 尝试将您的scroll_design.xml文件放在res / layout文件夹中

答案 2 :(得分:-1)

您有两个选项可以让您在主活动中创建的内容与滚动设计中的布局一起使用,反之亦然。

A。)将MainActivity.java中的代码复制并粘贴到DesignScroll.java中(最好选择创建新活动来创建xml和java已经相互设置)然后它将允许您更改{{1到setContentView();所以你有一个java和xml用于滚动设计

B。)使用MainActivity.java中的代码在activity_main.xml中创建布局,然后转到scroll_design.xml并添加setContentView(R.layout.scroll_design);,这将使您的主活动布局中的所有内容都显示在滚动设计布局中