【发布时间】:2017-10-08 20:06:45
【问题描述】:
当我尝试向我的班级添加片段时,我不断收到此错误,我不确定如何解决这个问题,我很确定我正确添加了片段,但是当我尝试运行它时,错误不断出现并使我的应用程序崩溃,不知道如何要解决这个问题?
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 文件
<?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>
【问题讨论】:
-
RecipeActivity是一个Activity。它不是Fragment。您不能将其视为Fragment。此外,每当您收到InflateException时,您都需要在堆栈跟踪中进一步查看实际原因。InflateException本身并没有告诉你太多。
标签: java android xml android-fragments