当我使用 Android数据绑定在我的xml文件中的非根元素中使用include
标记时,应用程序崩溃了。但是,当include
标记放在根元素中或没有数据绑定时,该应用程序可以正常工作。
这是我的源代码,任何人都可以帮我弄清楚原因吗?
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");
}
}
堆栈跟踪
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
答案 0 :(得分:4)
报告在这里: https://code.google.com/p/android/issues/detail?id=176649
内部修正,应尽快释放。
答案 1 :(得分:-3)
我在使用include标记时遇到问题,而不是代码。我建议使用片段而不是它们是可靠的并且是更好的编程方式。创建包含在片段中的文件,然后在布局文件中调用片段,如下所示
<fragment android:name="your fragment here"
android:id="@+id/article_fragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
我建议点击google获取片段,然后填写剩下的部分。