android - 无法将 recyclerview 添加到 fragment

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

我无法将 RecyclerView 添加到我的 fragment 中。 父类(super class)“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);
    }
}

我已经在 fragment 的xml文件中附加了一个recyclerview,但是我不能在我的 fragment 类中创建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>

最佳答案

将导入添加到您的 fragment 中,并检查您是否像这样将其添加到您的 build.gradle 中 compile 'com.android.support:recyclerview-v7:25.1.0'

import android.support.v7.widget.RecyclerView;

关于android - 无法将 recyclerview 添加到 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42299218/

相关文章:

java - 在 Java 中构造对象

javascript - 从 jquery 对象中删除元素

android - 同时编写调试和生产代码的最佳做法是什么?

java - 谷歌地图 Android fragment 崩溃

android - 如何将操作栏添加到 Android Studio 中的默认 Google Maps API?

android - 尝试在 android studio 上运行设备时出现 NullPointer 异常

java - 为什么静态对象作为参数传递给另一个类的构造函数时为 null

android - 启动默认 Android 壁纸选择器

java - 无法下载 JDK 8

android - 如何在Android中按@键打开对话框窗口?