InflateException 膨胀 class EditText 时出错
InflateException Error inflating class EditText
我在 Android Studio 上充气时遇到问题。
它在模拟器中运行良好。然而,我在 phone(HTC One M7 - 第一个)上测试了应用程序。
有人知道这个的解决方案吗?
布局如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1">
<android.support.v7.widget.CardView
android:id="@+id/ingcard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
card_view:cardElevation="10dp"
card_view:cardPreventCornerOverlap="true"
card_view:contentPadding="5dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Medium Text"
android:id="@+id/tvrecname"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<EditText
android:layout_width="100dp"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/etingquantity"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</FrameLayout>
Logcat:
E/Drive.UninstallOperation: Package still installed jdev.recipehelper
E/AndroidRuntime: FATAL EXCEPTION: main
Process: jdev.recipehelper, PID: 27305
android.view.InflateException: Binary XML file line #32: Error inflating class EditText
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at jdev.recipehelper.IngredientAdapter.onCreateViewHolder(IngredientAdapter.java:41)
at jdev.recipehelper.IngredientAdapter.onCreateViewHolder(IngredientAdapter.java:18)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5482)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4707)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1994)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1390)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3028)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:2625)
at android.view.View.measure(View.java:17536)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463)
at android.view.View.measure(View.java:17536)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5478)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:668)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:735)
at android.view.View.measure(View.java:17536)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5478)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:17536)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5478)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17536)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5478)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17536)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5478)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17536)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5478)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2629)
at android.view.View.measure(View.java:17536)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2102)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1218)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1435)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1106)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6018)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:792)
at android.view.Choreographer.doCallbacks(Choreographer.java:596)
at android.view.Choreographer.doFrame(Choreographer.java:557)
at androi
E/ActivityManager: App crashed! Process: jdev.recipehelper
Activity(以防万一):
package jdev.recipehelper;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by dracc on 21/04/2016.
*/
public class IngredientAdapter extends RecyclerView.Adapter<IngredientAdapter.IngredientHolder>{
// Adapter personalizado que usaremos para mostrar los ingredientes en la calculadora, por el momento no funciona.
// Instanciamos la activity para poder acceder a sus metodos.
private Calculator calc;
// Dataset que usara.
private ArrayList<Ingredient> data;
// Constructor que define el dataset
public IngredientAdapter(ArrayList<Ingredient> data) {
this.data = data;
}
// Constructor con dataset y activity
public IngredientAdapter(Calculator calc, ArrayList<Ingredient> data) {
this.calc = calc;
this.data = data;
}
// Creamos el IngredientHolder con el layout seleccionado
@Override
public IngredientHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.ingredient_card, parent, false);
return new IngredientHolder(v);
}
@Override
public void onBindViewHolder(final IngredientHolder holder, final int position) {
Ingredient item = data.get(position);
holder.setIngText(item);
holder.quantity.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
}
});
holder.quantity.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
setQuantities(position, data, holder);
}
});
}
// Importante pedirle a este metodo que nos devuelva el tamaño del array, o no creara ni una card
@Override
public int getItemCount() {
return data.size();
}
// Con este metodo modificamos la cantidad del resto de ingredientes y actualizamos el coste total.
private void setQuantities(int position, ArrayList<Ingredient> data, IngredientHolder holder){
float qprev = data.get(position).getQuantity();
float qnext;
float mult;
try{
qnext = Float.parseFloat(holder.quantity.getText().toString());
}catch(NumberFormatException ex){
qnext = 1;
}
if (qnext == 0){
qnext = 1;
}
mult = qnext/qprev;
for (int i = 0; i<data.size(); i++){
Ingredient e = data.get(i);
e.setQuantity(e.getQuantity()*mult);
Log.d("TAG", e.getName() + " " + e.getQuantity());
data.set(i, e);
try{
notifyItemChanged(i);
}catch (Exception ex){
Log.d("peta", "setQuantities: " + ex);
}
}
Log.d("TAG","mult: " + mult +" Siguiente");
calc.setCostText(data);
}
// IngredientHolder personalizado con el que manejaremos los elementos necesarios del layout
public static class IngredientHolder extends RecyclerView.ViewHolder{
// Componentes a los que accederemos.
TextView name;
EditText quantity;
// Constructor para inicializar los distintos componentes
public IngredientHolder(View v) {
super(v);
name = (TextView) v.findViewById(R.id.tvrecname);
quantity = (EditText) v.findViewById(R.id.etingquantity);
}
// Set para modificar todos los campos de la carta a la vez
public void setIngText(Ingredient item){
name.setText(item.getName());
quantity.setText(Float.toString(item.getQuantity()));
}
public void setQuantityText(Ingredient e) {
this.quantity.setText("" + e.getQuantity());
}
}
}
感谢您的帮助
这个答案对我有用:
我的 alpha gradle 似乎有问题。
感谢大家的帮助。
我在 Android Studio 上充气时遇到问题。
它在模拟器中运行良好。然而,我在 phone(HTC One M7 - 第一个)上测试了应用程序。
有人知道这个的解决方案吗?
布局如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1">
<android.support.v7.widget.CardView
android:id="@+id/ingcard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
card_view:cardElevation="10dp"
card_view:cardPreventCornerOverlap="true"
card_view:contentPadding="5dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Medium Text"
android:id="@+id/tvrecname"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<EditText
android:layout_width="100dp"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="@+id/etingquantity"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</FrameLayout>
Logcat:
E/Drive.UninstallOperation: Package still installed jdev.recipehelper
E/AndroidRuntime: FATAL EXCEPTION: main
Process: jdev.recipehelper, PID: 27305
android.view.InflateException: Binary XML file line #32: Error inflating class EditText
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at jdev.recipehelper.IngredientAdapter.onCreateViewHolder(IngredientAdapter.java:41)
at jdev.recipehelper.IngredientAdapter.onCreateViewHolder(IngredientAdapter.java:18)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5482)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4707)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1994)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1390)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3028)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:2625)
at android.view.View.measure(View.java:17536)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:727)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:463)
at android.view.View.measure(View.java:17536)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5478)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:668)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:735)
at android.view.View.measure(View.java:17536)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5478)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:17536)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5478)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17536)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5478)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17536)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5478)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17536)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5478)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2629)
at android.view.View.measure(View.java:17536)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2102)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1218)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1435)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1106)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6018)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:792)
at android.view.Choreographer.doCallbacks(Choreographer.java:596)
at android.view.Choreographer.doFrame(Choreographer.java:557)
at androi
E/ActivityManager: App crashed! Process: jdev.recipehelper
Activity(以防万一):
package jdev.recipehelper;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by dracc on 21/04/2016.
*/
public class IngredientAdapter extends RecyclerView.Adapter<IngredientAdapter.IngredientHolder>{
// Adapter personalizado que usaremos para mostrar los ingredientes en la calculadora, por el momento no funciona.
// Instanciamos la activity para poder acceder a sus metodos.
private Calculator calc;
// Dataset que usara.
private ArrayList<Ingredient> data;
// Constructor que define el dataset
public IngredientAdapter(ArrayList<Ingredient> data) {
this.data = data;
}
// Constructor con dataset y activity
public IngredientAdapter(Calculator calc, ArrayList<Ingredient> data) {
this.calc = calc;
this.data = data;
}
// Creamos el IngredientHolder con el layout seleccionado
@Override
public IngredientHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.ingredient_card, parent, false);
return new IngredientHolder(v);
}
@Override
public void onBindViewHolder(final IngredientHolder holder, final int position) {
Ingredient item = data.get(position);
holder.setIngText(item);
holder.quantity.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
}
});
holder.quantity.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
setQuantities(position, data, holder);
}
});
}
// Importante pedirle a este metodo que nos devuelva el tamaño del array, o no creara ni una card
@Override
public int getItemCount() {
return data.size();
}
// Con este metodo modificamos la cantidad del resto de ingredientes y actualizamos el coste total.
private void setQuantities(int position, ArrayList<Ingredient> data, IngredientHolder holder){
float qprev = data.get(position).getQuantity();
float qnext;
float mult;
try{
qnext = Float.parseFloat(holder.quantity.getText().toString());
}catch(NumberFormatException ex){
qnext = 1;
}
if (qnext == 0){
qnext = 1;
}
mult = qnext/qprev;
for (int i = 0; i<data.size(); i++){
Ingredient e = data.get(i);
e.setQuantity(e.getQuantity()*mult);
Log.d("TAG", e.getName() + " " + e.getQuantity());
data.set(i, e);
try{
notifyItemChanged(i);
}catch (Exception ex){
Log.d("peta", "setQuantities: " + ex);
}
}
Log.d("TAG","mult: " + mult +" Siguiente");
calc.setCostText(data);
}
// IngredientHolder personalizado con el que manejaremos los elementos necesarios del layout
public static class IngredientHolder extends RecyclerView.ViewHolder{
// Componentes a los que accederemos.
TextView name;
EditText quantity;
// Constructor para inicializar los distintos componentes
public IngredientHolder(View v) {
super(v);
name = (TextView) v.findViewById(R.id.tvrecname);
quantity = (EditText) v.findViewById(R.id.etingquantity);
}
// Set para modificar todos los campos de la carta a la vez
public void setIngText(Ingredient item){
name.setText(item.getName());
quantity.setText(Float.toString(item.getQuantity()));
}
public void setQuantityText(Ingredient e) {
this.quantity.setText("" + e.getQuantity());
}
}
}
感谢您的帮助
这个答案对我有用:
我的 alpha gradle 似乎有问题。
感谢大家的帮助。