错误:“RecyclerView:未附加适配器;跳过布局”

huangapple go评论72阅读模式
英文:

Error "RecyclerView: No adapter attached; skipping layout"

问题

以下是您提供的代码部分的翻译:

MainActivity.java

public class MainActivity extends AppCompatActivity {
    // ... (其他部分省略)

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);

        Read_network_state(cm);

        loadingIndicator = findViewById(R.id.loading_indicator);
        emptyView = findViewById(R.id.empty_view);

        mRecycleView = this.findViewById(R.id.recycle_list);
        mRecycleView.setHasFixedSize(true);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        mRecycleView.setLayoutManager(manager);
        mEarthquake = new ArrayList<>();

        mRequestQueue = Volley.newRequestQueue(this);

        if (isConnected) {
            Uri uri = Uri.parse(USS_REQUEST_URL);
            Uri.Builder builder = uri.buildUpon();

            parseEarthquake(builder.toString());
        }
    }

    // ... (其他部分省略)
}

EarthquakeAdapter.java

public class EarthquakeAdapter extends RecyclerView.Adapter<EarthquakeAdapter.MyViewHolder> {
    // ... (其他部分省略)

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        LayoutInflater inflater = LayoutInflater.from(mContext);
        view = inflater.inflate(R.layout.earthquake_raw, parent, false);
        final MyViewHolder viewHolder = new MyViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int i) {
        Earthquake earthquake = mData.get(i);
        double earthquakeMagnitude = earthquake.getMagnitude();

        DecimalFormat decimalFormat = new DecimalFormat("0.0");
        String formattedMagnitude = decimalFormat.format(earthquakeMagnitude);

        holder.tvMagnitude.setText(formattedMagnitude);

        Date dateObject = new Date(earthquake.getTimeInMilliseconds());
        String formattedDate = formatDate(dateObject);
        holder.tvDate.setText(formattedDate);

        String formattedTime = formatTime(dateObject);
        holder.tvTime.setText(formattedTime);

        // ... (其他部分省略)
    }

    // ... (其他部分省略)
}

activity_main.xml

<?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" 
    xmlns:tools="http://schemas.android.com/tools">
        
    <TextView 
        android:id="@+id/empty_view" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerInParent="true" 
        android:textAppearance="?android:textAppearanceMedium"/>
    
    <androidx.recyclerview.widget.RecyclerView 
        android:id="@+id/recycle_list" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:orientation="vertical" 
        tools:listitem="@layout/earthquake_raw"/>
        
    <ProgressBar 
        android:id="@+id/loading_indicator" 
        style="@style/Widget.AppCompat.ProgressBar" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerInParent="true"/>
</RelativeLayout>

earthquake_raw.xml

<?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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:orientation="horizontal"
    android:paddingStart="16dp"
    android:paddingLeft="16dp"
    android:paddingEnd="16dp"
    android:paddingRight="16dp">
    <TextView
        android:id="@+id/magnitude"
        android:layout_width="36dp"
        android:layout_height="36dp"
        android:layout_gravity="center_vertical"
        android:background="@drawable/magnitude_circle"
        android:fontFamily="sans-serif-medium"
        android:gravity="center"
        android:textColor="@android:color/black"
        android:textSize="16sp"
        tools:text="8.9" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_weight="1"
        android:orientation="vertical">
        <TextView
            android:id="@+id/location_offset"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:fontFamily="sans-serif-medium"
            android:maxLines="1"
            android:textAllCaps="true"
            android:textColor="@android:color/black"
            android:textSize="12sp"
            tools:text="30km S of" />

        <TextView
            android:id="@+id/primary_location"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:maxLines="2"
            android:textColor="@android:color/black"
            android:textSize="12sp"
            tools:text="Long placeholder that should wrap to more than 2 lines of text" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:orientation="vertical">
        <TextView
            android:id="@+id/date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:textColor="@color/textColorEarthquakeDetails"
            android:textSize="12sp"
            tools:text="Mar 6, 2010" />

        <TextView
            android:id="@+id/time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:textColor="@color/textColorEarthquakeDetails"
            android:textSize="12sp"
            tools:text="3:00 PM" />
    </LinearLayout>
</LinearLayout>

以上是您提供的代码的翻译部分。如有其他问题或需要进一步帮助,请随时提问。

英文:

I am getting the following error: "RecyclerView: No adapter attached; skipping layout" but I have attached the adapter... I have tried many different ways but I couldn't solve the error.

I am using Volley library to get data. When I launch the activity, only the progress bar is visible until the end, and I receive the Logcat message above. I have added the Recycleview with the adapter in the MainActivity. Could you please help me?

Here is my code:

MainActivity

public class MainActivity extends AppCompatActivity {
private RequestQueue mRequestQueue;
private ArrayList&lt;Earthquake&gt; mEarthquake;
private RecyclerView mRecycleView;
private EarthquakeAdapter adapter;
boolean isConnected;
TextView emptyView;
ProgressBar loadingIndicator;
private static final String USS_REQUEST_URL = &quot;https://earthquake.usgs.gov/fdsnws/event/1/query&quot;;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
Read_network_state(cm);
loadingIndicator = findViewById(R.id.loading_indicator);
emptyView = findViewById(R.id.empty_view);
mRecycleView = this.findViewById(R.id.recycle_list);
mRecycleView.setHasFixedSize(true);
LinearLayoutManager manager = new LinearLayoutManager(this);
mRecycleView.setLayoutManager(manager);
mEarthquake = new ArrayList&lt;&gt;();
mRequestQueue = Volley.newRequestQueue(this);
if (isConnected) {
Uri uri = Uri.parse(USS_REQUEST_URL);
Uri.Builder buider = uri.buildUpon();
parseEarthquake(buider.toString());
}
}
private void parseEarthquake(String key) {
final JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, key.toString(), null, response -&gt; {
double magnitude = Double.parseDouble(&quot;&quot;);
String location = &quot;&quot;;
long time = Long.parseLong(&quot;&quot;);
String url = &quot;&quot;;
try {
JSONArray feature = response.getJSONArray(&quot;feature&quot;);
for (int i = 0; i &lt; feature.length(); i++) {
JSONObject features = feature.getJSONObject(i);
JSONObject properties = features.getJSONObject(&quot;properties&quot;);
magnitude = properties.getDouble(&quot;mag&quot;);
location = properties.getString(&quot;place&quot;);
time = properties.getLong(&quot;time&quot;);
url = properties.getString(&quot;url&quot;);
}
Earthquake earthquake = new Earthquake(magnitude, location, time, url);
mEarthquake.add(earthquake);
adapter = new EarthquakeAdapter(MainActivity.this, mEarthquake);
mRecycleView.setAdapter(adapter);
Log.d(&quot;Ruhul&quot;, &quot;No adapter&quot;);
} catch (JSONException e) {
e.printStackTrace();
}
}, error -&gt; {
// Nothing
});
mRequestQueue.add(request);
}
public void Read_network_state(ConnectivityManager connectivityManager) {
NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
if (activeNetwork != null &amp;&amp; activeNetwork.isConnectedOrConnecting()) {
isConnected = true;
Log.d(&quot;Ruhul&quot;, &quot;CONNECTED TO INTERNET&quot;);
} else {
isConnected = false;
}
}
}

EarthquakeAdapter

public class EarthquakeAdapter extends RecyclerView.Adapter&lt;EarthquakeAdapter.MyViewHolder&gt; {
private Context mContext;
private List&lt;Earthquake&gt; mData;
public EarthquakeAdapter(Context mContext, List&lt;Earthquake&gt; mData) {
this.mContext = mContext;
this.mData = mData;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view;
LayoutInflater inflater = LayoutInflater.from(mContext);
view = inflater.inflate(R.layout.earthquake_raw, parent, false);
final MyViewHolder viewHolder = new MyViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int i) {
Earthquake earthquake = mData.get(i);
double earthquakeMagnitude = earthquake.getMagnitude();
DecimalFormat decimalFormat = new DecimalFormat(&quot;0.0&quot;);
String formattedMagnitude = decimalFormat.format(earthquakeMagnitude);
holder.tvMagnitude.setText(formattedMagnitude);
Date dateObject = new Date(earthquake.getTimeInMilliseconds());
String formattedDate = formatDate(dateObject);
holder.tvDate.setText(formattedDate);
String formattedTime = formatTime(dateObject);
holder.tvTime.setText(formattedTime);
String originalLocation = earthquake.getLocation();
holder.magnitudeCircle = (GradientDrawable) holder.tvMagnitude.getBackground();
int magnitudecolor = getMagnitudeColor(earthquake.getMagnitude());
holder.magnitudeCircle.setColor(magnitudecolor);
if (originalLocation.contains(&quot;of&quot;)) {
String[] parts = originalLocation.split(&quot;of&quot;);
holder.tvLocationOffSet.setText(parts[0] + &quot;of&quot;);
holder.tvPrimaryLocation.setText(parts[1]);
} else {
holder.tvLocationOffSet.setText(&quot;Near The&quot;);
holder.tvPrimaryLocation.setText(originalLocation);
}
}
@Override
public int getItemCount() {
return mData.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView tvMagnitude, tvLocationOffSet, tvPrimaryLocation, tvDate, tvTime;
LinearLayout container;
GradientDrawable magnitudeCircle;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
tvMagnitude = itemView.findViewById(R.id.magnitude);
tvLocationOffSet = itemView.findViewById(R.id.location_offset);
tvPrimaryLocation = itemView.findViewById(R.id.primary_location);
tvDate = itemView.findViewById(R.id.date);
tvTime = itemView.findViewById(R.id.time);
container = itemView.findViewById(R.id.container);
}
}
/**
* Return the color of the magitude circle based on the intensity of the earthquake.
*
* @param magnitude of the earthquake
*/
private int getMagnitudeColor(double magnitude) {
int magnitudeColorResourceId;
int magnitudeFloor = (int) Math.floor(magnitude);
switch (magnitudeFloor) {
case 0:
case 1:
magnitudeColorResourceId = R.color.magnitude1;
break;
case 2:
magnitudeColorResourceId = R.color.magnitude2;
break;
case 3:
magnitudeColorResourceId = R.color.magnitude3;
break;
case 4:
magnitudeColorResourceId = R.color.magnitude4;
break;
case 5:
magnitudeColorResourceId = R.color.magnitude5;
break;
case 6:
magnitudeColorResourceId = R.color.magnitude6;
break;
case 7:
magnitudeColorResourceId = R.color.magnitude7;
break;
case 8:
magnitudeColorResourceId = R.color.magnitude8;
break;
case 9:
magnitudeColorResourceId = R.color.magnitude9;
break;
default:
magnitudeColorResourceId = R.color.magnitude10plus;
break;
}
return ContextCompat.getColor(mContext, magnitudeColorResourceId);
}
private String formatDate(Date dateObject) {
SimpleDateFormat dateFormat = new SimpleDateFormat(&quot;LLL dd, yyyy&quot;);
return dateFormat.format(dateObject);
}
private String formatTime(Date dateObject) {
SimpleDateFormat timeFormat = new SimpleDateFormat(&quot;h:mm a&quot;);
return timeFormat.format(dateObject);
}
}

MainActivity.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; 
android:layout_width=&quot;match_parent&quot; 
android:layout_height=&quot;match_parent&quot; 
xmlns:tools=&quot;http://schemas.android.com/tools&quot;&gt;
&lt;TextView 
android:id=&quot;@+id/empty_view&quot; 
android:layout_width=&quot;wrap_content&quot; 
android:layout_height=&quot;wrap_content&quot; 
android:layout_centerInParent=&quot;true&quot; 
android:textAppearance=&quot;?android:textAppearanceMedium&quot;/&gt;
&lt;androidx.recyclerview.widget.RecyclerView 
android:id=&quot;@+id/recycle_list&quot; 
android:layout_width=&quot;match_parent&quot; 
android:layout_height=&quot;match_parent&quot; 
android:orientation=&quot;vertical&quot; 
tools:listitem=&quot;@layout/earthquake_raw&quot;/&gt;
&lt;ProgressBar 
android:id=&quot;@+id/loading_indicator&quot; 
style=&quot;@style/Widget.AppCompat.ProgressBar&quot; 
android:layout_width=&quot;wrap_content&quot; 
android:layout_height=&quot;wrap_content&quot; 
android:layout_centerInParent=&quot;true&quot;/&gt;
&lt;/RelativeLayout&gt;

EarthquakeRaw.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:id=&quot;@+id/container&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;150dp&quot;
android:orientation=&quot;horizontal&quot;
android:paddingStart=&quot;16dp&quot;
android:paddingLeft=&quot;16dp&quot;
android:paddingEnd=&quot;16dp&quot;
android:paddingRight=&quot;16dp&quot;&gt;
&lt;TextView
android:id=&quot;@+id/magnitude&quot;
android:layout_width=&quot;36dp&quot;
android:layout_height=&quot;36dp&quot;
android:layout_gravity=&quot;center_vertical&quot;
android:background=&quot;@drawable/magnitude_circle&quot;
android:fontFamily=&quot;sans-serif-medium&quot;
android:gravity=&quot;center&quot;
android:textColor=&quot;@android:color/black&quot;
android:textSize=&quot;16sp&quot;
tools:text=&quot;8.9&quot; /&gt;
&lt;LinearLayout
android:layout_width=&quot;0dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_gravity=&quot;center_vertical&quot;
android:layout_marginStart=&quot;16dp&quot;
android:layout_marginLeft=&quot;16dp&quot;
android:layout_weight=&quot;1&quot;
android:orientation=&quot;vertical&quot;&gt;
&lt;TextView
android:id=&quot;@+id/location_offset&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:ellipsize=&quot;end&quot;
android:fontFamily=&quot;sans-serif-medium&quot;
android:maxLines=&quot;1&quot;
android:textAllCaps=&quot;true&quot;
android:textColor=&quot;@android:color/black&quot;
android:textSize=&quot;12sp&quot;
tools:text=&quot;30km S of&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/primary_location&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:ellipsize=&quot;end&quot;
android:maxLines=&quot;2&quot;
android:textColor=&quot;@android:color/black&quot;
android:textSize=&quot;12sp&quot;
tools:text=&quot;Long placeholder that should wrap to more than 2 line of text&quot; /&gt;
&lt;/LinearLayout&gt;
&lt;LinearLayout
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_gravity=&quot;center_vertical&quot;
android:layout_marginStart=&quot;16dp&quot;
android:layout_marginLeft=&quot;16dp&quot;
android:orientation=&quot;vertical&quot;&gt;
&lt;TextView
android:id=&quot;@+id/date&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_gravity=&quot;end&quot;
android:textColor=&quot;@color/textColorEarthquakeDetails&quot;
android:textSize=&quot;12sp&quot;
tools:text=&quot;Mar 6, 2010&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/time&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_gravity=&quot;end&quot;
android:textColor=&quot;@color/textColorEarthquakeDetails&quot;
android:textSize=&quot;12sp&quot;
tools:text=&quot;3:00 PM&quot; /&gt;
&lt;/LinearLayout&gt;
&lt;/LinearLayout&gt;

答案1

得分: 1

始终尝试在主 UI 线程中连接适配器,或者在异步任务之外进行连接。
此外,您可以在 onCreate() 中添加此代码。当 mEarthquake 数组列表被填充时,调用 adapter.notifyDataSetChanged()

adapter = new EarthquakeAdapter(MainActivity.this, mEarthquake);
mRecyclerView.setAdapter(adapter);
英文:

Always try to attach the adapter in main UI thread, or outside your asynchronous task.
Also, you can add this code in oncreate(). and when mEarthquake arraylist is populated, call adapter.notifydatasetchanged().

adapter = new EarthquakeAdapter(MainActivity.this, mEarthquake);
mRecycleView.setAdapter(adapter); 

答案2

得分: 0

你应该在循环内部向模型添加数据。
尝试以下方式会正常工作:

JSONArray feature = response.getJSONArray("feature");
for (int i = 0; i < feature.length(); i++) {
    JSONObject features = feature.getJSONObject(i);
    JSONObject properties = features.getJSONObject("properties");
    magnitude = properties.getDouble("mag");
    location = properties.getString("place");
    time = properties.getLong("time");
    url = properties.getString("url");
    Earthquake earthquake = new Earthquake(magnitude, location, time, url);
    mEarthquake.add(earthquake);
    adapter = new EarthquakeAdapter(MainActivity.this, mEarthquake);
    mRecycleView.setAdapter(adapter);
    adapter.notifyDatasetChanged();
}
英文:

You should add data to model inside for loop.
Try like below will work fine,

JSONArray feature = response.getJSONArray(&quot;feature&quot;);
for (int i = 0; i &lt; feature.length(); i++) {
JSONObject features = feature.getJSONObject(i);
JSONObject properties = features.getJSONObject(&quot;properties&quot;);
magnitude = properties.getDouble(&quot;mag&quot;);
location = properties.getString(&quot;place&quot;);
time = properties.getLong(&quot;time&quot;);
url = properties.getString(&quot;url&quot;);
Earthquake earthquake = new Earthquake(magnitude, location, time, url);
mEarthquake.add(earthquake);
adapter = new EarthquakeAdapter(MainActivity.this, mEarthquake);
mRecycleView.setAdapter(adapter);
adapter.notifyDatasetChanged();
}

huangapple
  • 本文由 发表于 2020年9月25日 18:23:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/64062265.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定