【问题标题】:Can't add recyclerview to the fragment无法将 recyclerview 添加到片段
【发布时间】:2017-02-17 13:29:08
【问题描述】:

我无法将 RecyclerView 添加到我的片段中。 超类“RecyclerView”以红色显示。 帮帮我!

java代码:

package com.invincible.fragedit;

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


public class TableList extends Fragment {

    private RecyclerView recyclerView;//error line 
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_list_table,container,false);
    }
}

我已经在片段的 xml 文件中附加了一个 recyclerview,但我无法在我的片段类中创建 RecyclerView 的对象

xml代码:

<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="400dp"
    android:id="@+id/frag_2"
    android:layout_marginTop="80dp"
    android:background="#c61414"
    tools:context="com.invincible.fragedit.ListTable">

    <!-- TODO: Update blank fragment layout -->

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

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

    <TextView
        android:id="@+id/table_value"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

【问题讨论】:

  • 你是否将jar文件作为库添加到项目中?
  • 你在build.gradle 中添加了依赖compile 'com.android.support:appcompat-v7:25.1.1' 吗?

标签: android android-studio object android-fragments android-recyclerview


【解决方案1】:

将导入添加到您的片段中并检查您是否已将其添加到您的 build.gradle 中,例如 compile 'com.android.support:recyclerview-v7:25.1.0'

import android.support.v7.widget.RecyclerView;

【讨论】:

  • 完成!但仍然是导入包和片段类中的错误
【解决方案2】:

确保你在 build.gradle 中添加

    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile 'com.android.support:cardview-v7:23.1.1'
    compile 'com.android.support:recyclerview-v7:23.1.1'

【讨论】:

    【解决方案3】:

    在您的 java 类导入语句中缺少 Recycler 视图。 请执行以下步骤。

    1- 在 build.gralde 文件 'compile 'com.android.support:appcompat-v7:25.0.1'' 中添加以下行。 2- 清理并再次重建。

    3- 将此行粘贴到您的导入部分“import android.support.v7.widget.RecyclerView”;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多