Vlad Kramarenko
Vlad Kramarenko

Reputation: 286

Binary XML file line #22: Error inflating class fragment

I'm doing a training program in which there is a list of questions that can be answered Yes or no. Depending on the answer text to the question changes color. The results should be displayed in a separate activity. I suggested to use fragments to save what is the color of the text issue but I have them an error Binary XML file line #22: Error inflating class fragment

Here is the code:

MainActivity:

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);
    }
}

RVAdapter:

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");


        }
    }
}

TextFragment:

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>

item:

<?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>

fragment1:

<?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>

Upvotes: 2

Views: 1584

Answers (2)

Csaba Toth
Csaba Toth

Reputation: 10729

So after several weeks I found the problem. While updating to Sceneform SDK 1.16 there's a breaking change you guys. Minor version bump but that causes those crashes I cite in my question. The solution is:

  1. Follow the steps in the README of the Sceneform SDK repo (copy over two src directories into your source code, modify two build files).
  2. Remove the existing com.google.ar.sceneform.ux:sceneform-ux:1.x reference from your build.gradle file
  3. Refactor those two directories from the SDK to use androidx imports in case your project is not relying on the old AppCompat ones.

See also my blog post about it: https://csaba.page/blog/sceneform-breaking-change.html

Upvotes: 0

Sanjeet
Sanjeet

Reputation: 2403

Fragments can't be added using xml.
You need to add it using fragment manager by doing fragment transaction in activity or some other fragment.

In your example it's unnecessary to use fragment, as it contains only single TextView.

You can replace your fragment tag in item xml with a TextView directly.

Upvotes: 2

Related Questions