2017-10-08 9 views
0

私のクラスにフラグメントを追加しようとするとこのエラーが発生するimこの問題を修正する方法がわからない私はそれを修正する方法を任意のアイデアを実行しようとすると、エラーが表示され、私のアプリをクラッシュし続けますか?あなたが垂直のXMLにフラグメントを追加しているバイナリ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 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'です。 「断片」ではありません。あなたはそれを 'Fragment'として扱うことはできません。また、 'InflateException'を取得するたびに、実際の原因をスタックトレースでさらに調べる必要があります。 'InflateException'自体はそれほど多くのことを教えてくれません。 –

答えて

0

中の断片を入れてtriyng風景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

@MikeMとして。コメント内で言うと、フラグメントタグ内にandroid:name属性のアクティビティを使用することはできません。

Fragments documentationは言う:

アンドロイド:指定のname属性フラグメント クラスは、レイアウトにインスタンス化します。

だから、あなたは以下を使用することはできません。

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

あなたは断片にあなたのRecipeActivity活動を変換する必要があります。

関連する問題