2016-07-19 16 views
6

ich gerade lese über Datenbindung Funktion in Android setzen. Ich habe versucht, eine Ansicht aus einem verschachtelten Layout zu binden. Meine Tätigkeit ist die Android-Standardvorlage von DrawerMenuActivity und es hat content_my_activity Layout innerhalb activity_my_activity Layout verschachtelt.Wie Android Databinding in verschachtelten Layouts

Ich habe 3 Auto-Bindung Klassen generiert. MyActivityBinding, AppBarMyActivityBinding und ContentMyActivityBinding.

Ich habe versucht, wie unten alle 3 Klasse Initialisierung:

MyActivity.java

MyActivityBinding activityBinding; 
AppBarMyActivityBinding appBarBinding; 
ContentMyActivityBinding contentBinding; 

protected void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 

    activityBinding = DataBindingUtils.setContentView(this, 
           R.layout.activity_my_activity); 
    appBarBinding = DataBindingUtils.setContentView(this, 
           R.layout.app_bar_my_activity); 
    contentBinding = DataBindingUtils.setContentView(this, 
           R.layout.content_my_activity); 

    setName(); 
} 

private void setName(){ 
    contentBinding.setFirstName("Omkar"); 
} 

Aber es wirft Runtime wie folgt:

E/AndroidRuntime: FATAL EXCEPTION: main 
Process: com.example, PID: 18305 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.userInterface.activity.MyActivity}: android.view.InflateException: Binary XML file line #35: Binary XML file line #27: Error inflating class fragment 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490) 
    at android.app.ActivityThread.access$900(ActivityThread.java:154) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5443) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
Caused by: android.view.InflateException: Binary XML file line #35: Binary XML file line #27: Error inflating class fragment 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:539) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
    at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280) 
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
    at android.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:276) 
    at com.example.userInterface.activity.MyActivity.onCreate(MyActivity.java:88) 
    at android.app.Activity.performCreate(Activity.java:6259) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)  
    at android.app.ActivityThread.access$900(ActivityThread.java:154)  
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)  
    at android.os.Handler.dispatchMessage(Handler.java:102)  
    at android.os.Looper.loop(Looper.java:148)  
    at android.app.ActivityThread.main(ActivityThread.java:5443)  
    at java.lang.reflect.Method.invoke(Native Method)  
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)  
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)  
Caused by: android.view.InflateException: Binary XML file line #27: Error inflating class fragment 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782) 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:838) 
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
    at android.view.LayoutInflater.parseInclude(LayoutInflater.java:971) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:831) 
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)  
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374)  
    at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)  
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)  
    at android.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:276)  
    at com.example.userInterface.activity.MyActivity.onCreate(MyActivity.java:88)  
    at android.app.Activity.performCreate(Activity.java:6259)  
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)  
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)  
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)  
    at android.app.ActivityThread.access$900(ActivityThread.java:154)  
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)  
    at android.os.Handler.dispatchMessage(Handler.java:102)  
    at android.os.Looper.loop(Looper.java:148)  
    at android.app.ActivityThread.main(ActivityThread.java:5443)  
    at java.lang.reflect.Method.invoke(Native Method)  
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)  
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)  
Caused by: java.lang.IllegalArgumentException: Binary XML file line #27: Duplicate id 0x7f0e0081, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.SupportMapFragment 
    at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2416) 
    at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120) 
    at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:374) 
    at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:33) 
    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:75) 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:754) 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)  
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)  
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)  
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:838)  
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)  
    at android.view.LayoutInflater.parseInclude(LayoutInflater.java:971)  
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:831)  
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)  
    at android.view.LayoutInflater.inflate(LayoutInflater.java:515)  
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)  
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374)  
    at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)  
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)  
    at android.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:276)  
    at com.example.userInterface.activity.MyActivity.onCreate(MyActivity.java:88)  
    at android.app.Activity.performCreate(Activity.java:6259)  
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)  
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)  
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)  
    at android.app.ActivityThread.access$900(ActivityThread.java:154)  
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)  
    at android.os.Handler.dispatchMessage(Handler.java:102)  
    at android.os.Looper.loop(Looper.java:148)  
    at android.app.ActivityThread.main(ActivityThread.java:5443)  
    at java.lang.reflect.Method.invoke(Native Method)  
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)  
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)  

Wie kann ich Ansicht von content_my_activity binden In der MyActivity.java-Klasse wird jede Hilfe geschätzt.

Danke.

+0

Sie folgen können [diese] (https://github.com/kevalpatel2106/data-binding-demo/tree/master/app/src/main). Dies könnte Ihnen helfen zu verstehen. –

Antwort

8

DataBindingUtils.setContentView() tut genau, wie es genannt wird: Es setzt die aktuelle Ansicht auf den angegebenen Parameter. Ich glaube nicht, dass Sie Ihre AppBar als die ganze Ansicht wollen, oder?

Trotzdem nehme ich an, dass Sie include Ihre Layouts in Ihrem layout_activity_main.xml. George Mount hat eine whole blog Post über diese Funktion geschrieben. Die Codebeispiele stammen aus diesem Beitrag.

Das erste Beispiel wäre Ihre layout_activity_main.xml (Oder wie auch immer Sie es genannt haben), wo Sie Ihre AppBar, Ihre Content und so weiter einschließen.

hello_world.xml 
<layout xmlns:android="http://schemas.android.com/apk/res/android"> 
    <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 

     <TextView 
       android:id="@+id/hello" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"/> 
     <include 
       android:id="@+id/world1" 
       layout="@layout/included_layout"/> 
     <include 
       android:id="@+id/world2" 
       layout="@layout/included_layout"/> 
    </LinearLayout> 
</layout> 


included_layout.xml 
<?xml version="1.0" encoding="utf-8"?> 
<layout xmlns:android="http://schemas.android.com/apk/res/android"> 
    <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/world"/> 
</layout> 

Nun, da die verwendeten Layouts klar sind, müssen Sie in Ihrem ActivityMain springen, initialisieren Sie die DataBinding und Zugriff auf Ihre Felder:

//This works if you have used a variable in your <data> tag and you have built your project afterwards, if you don't have an activity 
HelloWorldBinding binding = HelloWorldBinding.inflate(getLayoutInflater()); 

//if you have an activity, you can use setContentView from the DataBindingUtils. Don't forget to delete the generic setContentView 
HelloWorldBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_my_activity); 


//Once you have accomplished the above, you can access your data-bound fields like this: 
binding.hello.setText(“Hello”); 
binding.world1.world.setText(“First World”); 
binding.world2.world.setText(“Second World”); 

Es ist wichtig, Ids auf Ihre <include>-Tags zu setzen um sie richtig zu erreichen i Ihre Activity.

+0

Großartig! Also auch wenn es drei Schicht verschachteltes Layout, kann ich einfach darauf zugreifen wie diese 'binding.world1.world2.world.setText („Dritten Welt“);' 'durch inflater' für nicht-Aktivität vorbei und durch Initialisieren' setContentView' von Außen die meisten Layout-Bindungsklasse? – Omkar

+0

Ja, das wird funktionieren. :) – yennsarah

+1

Stellen Sie sicher, dass Sie Android Studio säubern und möglicherweise neu starten, nachdem Sie diese verschachtelte Struktur eingerichtet haben. Android Studio erkannte die Verschachtelung nicht, bis ich diese Dinge getan hatte. – Justin