gpt4 book ai didi

android - 二进制 XML 文件行 #22 : Error inflating class fragment

转载 作者:搜寻专家 更新时间:2023-11-01 09:40:50 27 4
gpt4 key购买 nike

我正在做一个培训计划,其中有一个问题列表,可以回答是或否。根据问题的答案文本改变颜色。结果应显示在单独的 Activity 中。我建议使用 fragment 来保存文本问题的颜色是什么,但我给他们一个错误 Binary XML file line #22: Error inflating class fragment

代码如下:

主要 Activity :

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

// public static String TAG = "MYLOG_main_activity";
public static String KEY = "KEY";
RecyclerView mRecyclerView;
LinearLayoutManager mLinearLayoutManager;
Context mContext;
Button mResult;
public static int[] iResult;

public static void MyReceiver(int[] result)
{
iResult = result;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null)
{
iResult = savedInstanceState.getIntArray(KEY);
}
mResult = (Button) findViewById(R.id.result);
mResult.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View asv) {if(iResult != null) {
/* Intent intent1 = new Intent("Intent");
intent1.putExtra("message", iResult);
intent1.setAction("broadcast");
sendBroadcast(intent1);
*/

Intent intent = new Intent(MainActivity.this, Result.class);
startActivity(intent);
Result.receiver(iResult);
}

}
});
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
mLinearLayoutManager = new LinearLayoutManager(mContext);
mRecyclerView.setLayoutManager(mLinearLayoutManager);
ArrayList<Ask> asks = new ArrayList<>();
asks.add(0,new Ask(0,R.drawable.image1,R.string.one));
asks.add(1,new Ask(1,R.drawable.image2,R.string.two));
asks.add(2,new Ask(2,R.drawable.image3,R.string.three));
asks.add(3,new Ask(3,R.drawable.image4,R.string.four));
asks.add(4,new Ask(4,R.drawable.image5,R.string.five));
//iResult = new int[asks.size()];
RVAdapter adapter = new RVAdapter(asks);
mRecyclerView.setAdapter(adapter);

}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putIntArray(KEY, iResult);
}
}

RV适配器:

import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;


/**
* Created by 1 on 2.09.2016.
*/
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.CardViewHolder>{
public static final String TAG = "MYLOG_RVAdapter";
List<Ask> asks;
int[] iResult;
RVAdapter(List<Ask> asks) {
this.asks = asks;
iResult = new int[asks.size()];
for (int i = 0; i < asks.size(); i++) {
iResult[i] = 0;
}

}
@Override
public CardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false); //На эту строку ругается
Log.d(TAG,"onCreateViewHolder");
return new CardViewHolder(v);
}
@Override
public int getItemCount() {
return asks.size();
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
Log.d(TAG,"onAttachedToRecyclerView");
super.onAttachedToRecyclerView(recyclerView);
}


@Override
public void onBindViewHolder(final CardViewHolder holder, final int position) {


holder.mButtonNo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.mTextView.setBackgroundResource(R.color.No);
iResult[position] = 2;
MainActivity.MyReceiver(iResult);
}
});
holder.mButtonYes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.mTextView.setBackgroundResource(R.color.Yes);
iResult[position] = 1;
MainActivity.MyReceiver(iResult);
}
});
switch (position) {

case 0:
holder.mTextView.setText(R.string.one);
holder.mImageView.setImageResource(R.drawable.image1);
break;
case 1:
holder.mTextView.setText(R.string.two);
holder.mImageView.setImageResource(R.drawable.image2);
break;
case 2:
holder.mTextView.setText(R.string.three);
holder.mImageView.setImageResource(R.drawable.image3);
break;
case 3:
holder.mTextView.setText(R.string.four);
holder.mImageView.setImageResource(R.drawable.image4);
break;
case 4:
holder.mTextView.setText(R.string.five);
holder.mImageView.setImageResource(R.drawable.image5);
break;
}

Log.d(TAG,"onBindViewHolder");
Log.d(TAG, String.valueOf(position));

}

public static class CardViewHolder extends RecyclerView.ViewHolder {
ImageView mImageView;
TextView mTextView;
CardView mCardVew;
Button mButtonYes;
Button mButtonNo;
public CardViewHolder(View itemView) {
super(itemView);
mButtonNo = (Button) itemView.findViewById(R.id.No);
mButtonYes = (Button) itemView.findViewById(R.id.Yes);
mTextView = (TextView) itemView.findViewById(R.id.textView);
mImageView = (ImageView) itemView.findViewById(R.id.imageView);
mCardVew = (CardView) itemView.findViewById(R.id.cardView);

Log.d(TAG,"CardViewHolder");


}
}
}

文本 fragment :

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
* Created by 1 on 04.10.2016.
*/
public class TextFragment extends Fragment{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//return super.onCreateView(inflater, container, savedInstanceState);
View rootView =
inflater.inflate(R.layout.fragment1, container,false);
return rootView;
}
}

activity_main:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.vkramarenko.myapplication.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/result"
android:id="@+id/result"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_toStartOf="@+id/Yes"
android:layout_alignParentEnd="true" />
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyclerView"
android:layout_above="@+id/result">

</android.support.v7.widget.RecyclerView>
</RelativeLayout>

项目:

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

<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/cardView"
card_view:cardCornerRadius="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no"
android:id="@+id/No"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
<fragment
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.vkramarenko.myapplication.TextFragment"
android:id="@+id/textFragment"
android:layout_above="@+id/No"
android:layout_alignEnd="@+id/No" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/yes"
android:id="@+id/Yes"
android:layout_alignParentBottom="true"
android:layout_toStartOf="@+id/No" />

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView"
android:src="@drawable/image1"
android:contentDescription=""
android:layout_above="@+id/textFragment"
android:layout_alignEnd="@+id/No" />
</RelativeLayout>


</android.support.v7.widget.CardView>
</LinearLayout>

fragment 1:

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

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/one"
android:id="@+id/textView"
android:layout_gravity="center_horizontal"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</RelativeLayout>

最佳答案

无法使用 xml 添加 fragment 。
您需要使用 fragment 管理器通过在 Activity 或其他 fragment 中执行 fragment 事务来添加它。

在您的示例中,没有必要使用 fragment ,因为它只包含一个 TextView。

您可以直接将项目 xml 中的 fragment 标记替换为 TextView。

关于android - 二进制 XML 文件行 #22 : Error inflating class fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39941752/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com