When I use include
tag in a non-root element in my xml file with Android Data Binding, app crashes. But the app works well when the include
tag is placed in the root element or without data binding things.
Here is my source code, can any one help me to figure out the reason ?
activity_main.xml
<layout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/include_layout" />
</LinearLayout>
</LinearLayout>
</layout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
binding.textView.setText("hello world");
}
}
stack trace
I/ActivityManager(13254): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.liangfeizc.databindings/.MainActivity (has extras)} from uid 10008 on display 0
I/ActivityManager(13254): Start proc com.liangfeizc.databindings for activity com.liangfeizc.databindings/.MainActivity: pid=18181 uid=10065 gids={50065, 9997} abi=x86
I/art (18181): Not late-enabling -Xcheck:jni (already on)
D/AndroidRuntime(18181): Shutting down VM
E/AndroidRuntime(18181): FATAL EXCEPTION: main
E/AndroidRuntime(18181): Process: com.liangfeizc.databindings, PID: 18181
E/AndroidRuntime(18181): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.liangfeizc.databindings/com.liangfeizc.databindings.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.setTag(java.lang.Object)' on a null object reference
E/AndroidRuntime(18181): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
E/AndroidRuntime(18181): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
E/AndroidRuntime(18181): at android.app.ActivityThread.access$800(ActivityThread.java:144)
E/AndroidRuntime(18181): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
E/AndroidRuntime(18181): at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime(18181): at android.os.Looper.loop(Looper.java:135)
E/AndroidRuntime(18181): at android.app.ActivityThread.main(ActivityThread.java:5221)
E/AndroidRuntime(18181): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(18181): at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime(18181): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
E/AndroidRuntime(18181): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
E/AndroidRuntime(18181): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.setTag(java.lang.Object)' on a null object reference
E/AndroidRuntime(18181): at com.liangfeizc.databindings.databinding.ActivityMainBinding.<init>(ActivityMainBinding.java:27)
E/AndroidRuntime(18181): at com.liangfeizc.databindings.databinding.ActivityMainBinding.bind(ActivityMainBinding.java:88)
E/AndroidRuntime(18181): at android.databinding.DataBinderMapper.getDataBinder(DataBinderMapper.java:14)
E/AndroidRuntime(18181): at android.databinding.DataBindingUtil.bind(DataBindingUtil.java:107)
E/AndroidRuntime(18181): at android.databinding.DataBindingUtil.inflate(DataBindingUtil.java:68)
E/AndroidRuntime(18181): at android.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:185)
E/AndroidRuntime(18181): at com.liangfeizc.databindings.MainActivity.onCreate(MainActivity.java:18)
E/AndroidRuntime(18181): at android.app.Activity.performCreate(Activity.java:5937)
E/AndroidRuntime(18181): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
E/AndroidRuntime(18181): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
E/AndroidRuntime(18181): ... 10 more
W/ActivityManager(13254): Force finishing activity com.liangfeizc.databindings/.MainActivity
D/ (13254): HostConnection::get() New Host Connection established 0xa0a234d0, tid 13352
Reported here: https://code.google.com/p/android/issues/detail?id=176649
Fixed internally, should be released soon.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With