0

I am making a custom adapter in card view which loads data from a locally saved json file. I am able to fetch the data but the view is not being inflated and I keep on getting this error:

    E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.visan.pulse, PID: 1630
    Theme: themes:{default=overlay:com.meow.os, iconPack:system, fontPkg:com.your.zappy.markui, com.android.systemui=overlay:system, com.android.systemui.navbar=overlay:system}
    android.view.InflateException: Binary XML file line #30: Binary XML file line #30: Error inflating class <unknown>
    at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
    at com.example.visan.pulse.CardAdapter.onCreateViewHolder(CardAdapter.java:25)
    at com.example.visan.pulse.CardAdapter.onCreateViewHolder(CardAdapter.java:12)
    at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5288)
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4551)
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4461)
    at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1962)
    at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1371)
    at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1334)
    at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:563)
    at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2847)
    at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3145)
    at android.view.View.layout(View.java:16639)
    at android.view.ViewGroup.layout(ViewGroup.java:5437)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1735)
    at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1724)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1490)
    at android.view.View.layout(View.java:16639)
    at android.view.ViewGroup.layout(ViewGroup.java:5437)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
    at android.view.View.layout(View.java:16639)
    at android.view.ViewGroup.layout(ViewGroup.java:5437)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1735)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1579)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1488)
    at android.view.View.layout(View.java:16639)
    at android.view.ViewGroup.layout(ViewGroup.java:5437)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
    com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2934)
    at android.view.View.layout(View.java:16639)
    at android.view.ViewGroup.layout(ViewGroup.java:5437)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2179)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1939)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1115)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6023)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
    at android.view.Choreographer.doCallbacks(Choreographer.java:670)
    at android.view.Choreographer.doFrame(Choreographer.java:606)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5461)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
    Caused by: android.view.InflateException: Binary XML file line #30: Error inflating class <unknown>
    at android.view.LayoutInflater.createView(LayoutInflater.java:645)
    at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694)

My Custom Adapter class:

package com.example.visan.pulse;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder>{
    private List<Data> dataSet;
    private Context context;

public CardAdapter(List<Data> dataSet, Context context){
    super();
    this.dataSet=dataSet;
    this.context=context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.card_layout, parent, false);
    ViewHolder viewHolder=new ViewHolder(v);
    return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    Data data=dataSet.get(position);
    holder.event.setText(data.getName());
    holder.venue.setText(data.getVenue());
    holder.time.setText(data.getTime());
    holder.notify.setImageResource(R.drawable.ic_action_bell);
    holder.main.setImageResource(R.drawable.image_16);
}

@Override
public int getItemCount() {
    return dataSet.size();
}

public class ViewHolder extends RecyclerView.ViewHolder{
    public TextView event,venue,time;
    public ImageView main,notify;

    public ViewHolder(View itemView){
        super(itemView);
        main=(ImageView) itemView.findViewById(R.id.card_image);
        notify=(ImageView) itemView.findViewById(R.id.imageView_notify);
        event=(TextView) itemView.findViewById(R.id.textView_event);
        venue=(TextView) itemView.findViewById(R.id.textView_venue);
        time=(TextView) itemView.findViewById(R.id.textView_time);
    }
}
}

My xml file for card View:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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:id="@+id/card_view"
    android:layout_gravity="center"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="4dp"
    card_view:cardElevation="10dp">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:id="@+id/card_image"
        android:src="@drawable/image_16"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="?android:textAppearanceLarge"
                android:id="@+id/textView_event"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="?android:textAppearanceSmall"
                android:id="@+id/textView_time"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="?android:textAppearanceSmall"
                android:id="@+id/textView_venue"/>
        </LinearLayout>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imageView_notify"
            android:contentDescription="@string/app_name"/>
    </LinearLayout>

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

</RelativeLayout>

My main activity class:

package com.example.visan.pulse;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Main2Activity extends Activity {
private List<Data> listData;

private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle extras=getIntent().getExtras();
    int data=extras.getInt("INDEX");
    setContentView(R.layout.activity_main2);

    recyclerView=(RecyclerView) findViewById(R.id.RecyclerView);
    recyclerView.setHasFixedSize(true);
    layoutManager=new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);

    listData=new ArrayList<>();

    try{
        JSONObject jsonObject=new JSONObject(loadJSON());
        Log.v("data",String.valueOf(data));
        JSONObject jsonObjectdata=jsonObject.getJSONObject(String.valueOf(data));

        for (int i=0;i<jsonObjectdata.length();i++){
            JSONArray jsonArray=jsonObjectdata.getJSONArray(String.valueOf(i));
            Data newData=new Data();
            newData.setName(jsonArray.getString(0));
            newData.setTime(jsonArray.getString(1));
            newData.setVenue(jsonArray.getString(2));
            newData.setDept(jsonArray.getString(3));
            listData.add(newData);
            Log.v("data", jsonArray.getString(0));
        }
    }catch (JSONException e){
        e.printStackTrace();
    }

    adapter=new CardAdapter(listData,this);
    recyclerView.setAdapter(adapter);
}

public String loadJSON(){
    StringBuilder stringBuilder=new StringBuilder();
    try {
        InputStream inputStream=getResources().openRawResource(R.raw.data);
        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));

        String line;
        while ((line=bufferedReader.readLine())!=null){
            stringBuilder.append(line);
            Log.v("abc",line);
        }

        bufferedReader.close();
    }catch (IOException e){
        e.printStackTrace();
    }
    return stringBuilder.toString();
}
}

My main activity xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.visan.pulse.Main2Activity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/RecyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </android.support.v7.widget.RecyclerView>

</LinearLayout>
4

1 回答 1

1

文本外观属性不应应用于android:textSize. 做这个:

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:textAppearanceLarge"
            android:id="@+id/textView_event"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:textAppearanceSmall"
            android:id="@+id/textView_time"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:textAppearanceSmall"
            android:id="@+id/textView_venue"/>
于 2016-09-05T19:39:34.373 回答