2017-10-08 70 views
0

當試圖向我的類添加一個片段我不斷收到此錯誤im不知道如何解決此im相當確定即時添加片段的權利但錯誤不斷出現,並崩潰我的應用程序時,我試圖運行它任何想法如何解決它?二進制XML文件行#9:二進制XML文件行#9:錯誤擴展類片段

XML文件

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/layout_default" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.ericvuu.whatsfordinner.RecipeActivity"> 

    <fragment 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:name="com.example.ericvuu.whatsfordinner.RecipeActivity"/> 

    <Button 
     android:id="@+id/recipe1" 
     android:layout_width="0dp" 
     android:layout_height="50dp" 
     android:layout_marginBottom="484dp" 
     android:background="@android:color/darker_gray" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     tools:layout_constraintBottom_creator="1" 
     tools:layout_constraintLeft_creator="1" 
     tools:layout_constraintRight_creator="1" 
     app:layout_constraintHorizontal_bias="0.0" /> 

    <Button 
     android:id="@+id/recipe2" 
     android:layout_width="0dp" 
     android:layout_height="50dp" 
     android:background="@color/colorPrimaryDark" 
     app:layout_constraintHorizontal_bias="0.0" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/recipe1" 
     tools:layout_constraintLeft_creator="1" 
     tools:layout_constraintRight_creator="1" 
     tools:layout_constraintTop_creator="1" /> 

    <Button 
     android:id="@+id/recipe3" 
     android:layout_width="0dp" 
     android:layout_height="50dp" 
     android:layout_marginTop="-11dp" 
     android:background="@android:color/darker_gray" 
     app:layout_constraintHorizontal_bias="0.0" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/recipe2" 
     tools:layout_constraintLeft_creator="1" 
     tools:layout_constraintRight_creator="1" 
     tools:layout_constraintTop_creator="1" /> 
</android.support.constraint.ConstraintLayout> 

的Java文件

package com.example.ericvuu.whatsfordinner; 

    import android.support.v7.app.AppCompatActivity; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.widget.Button; 
    import android.widget.EditText; 
    import android.widget.TextView; 

    import java.io.BufferedReader; 
    import java.io.FileInputStream; 
    import java.io.FileNotFoundException; 
    import java.io.IOException; 
    import java.io.InputStreamReader; 

    public class RecipeActivity extends AppCompatActivity { 

     public Button recipeText; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_recipe); 

      recipeText = (Button) findViewById(R.id.recipe1); 


       try { 
        String message; 
        FileInputStream fis = openFileInput("RecipeText"); 
        InputStreamReader isr = new InputStreamReader(fis); 
        BufferedReader br = new BufferedReader(isr); 
        StringBuffer sb = new StringBuffer(); 
        while ((message = br.readLine()) != null) { 
         sb.append(message + "\n"); 
        } 
        recipeText.setText(sb.toString()); 

       } catch (FileNotFoundException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 

     } 

     public void onClick(View v) { 

     } 

    } 

和景觀xml文件IM triyng把片段

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:weightSum="1"> 

    <fragment 
     android:id="@+id/layout_default" 
     android:name="com.example.ericvuu.whatsfordinner.RecipeActivity" 
     android:layout_width="214dp" 
     android:layout_height="match_parent" 
     android:layout_weight="0.08" /> 

</LinearLayout> 
+1

'RecipeActivity'是一個'Activity'。它不是一個「片段」。你不能把它當作'片段'。此外,無論何時出現'InflateException',您都需要在堆棧跟蹤中進一步查看實際原因。 InflateException本身並沒有給你太多的提示。 –

回答

0

你加入片段XML進行垂直和風景模式。所以一旦你啓動你的應用程序,它會崩潰,因爲你已經在你的活動中添加了片段。我會建議更好地嘗試使用容器添加片段。

例如: - 把這個線在你的XML

<LinearLayout 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

,而不是直接加入片段插入XML的。

並嘗試以編程方式添加您的片段。

FragmentTransaction fragmentTransaction = 
getFragmentManager().beginTransaction(); 
....... 
...... 

並且還檢查RecipeActivity是一個不是片段的活動。此代碼僅適用於片段

+0

我把那個景觀xml? –

+0

是的,你必須把這兩個XML –

0

As @MikeM。在評論中告訴,你不能在片段標籤中使用android:name屬性的活動。

Fragments documentation說:

Android的:在指定name屬性的片段 類佈局中的實例。

所以,你不能使用下列內容:

<fragment 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:name="com.example.ericvuu.whatsfordinner.RecipeActivity"/> 

您需要將RecipeActivity活動轉換成片段。

相關問題