使用RecyclerViewAdapter时为空对象参考

问题描述 投票:0回答:2

我正在尝试在我的Fragment中实现RecyclerView。我在Fragment中,所以我不能在RecyclerViewAdapter中使用“ this”。我使用getContext()代替“ this”。这是错误吗?

这是代码:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_news, container, false); 
return rootView;
    }
public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
    RecyclerView recyclerView = view.findViewById(R.id.idRecycleView);
    RecyclerViewAdapter adapter = new RecyclerViewAdapter(getContext(), listOfLinks);
    recyclerView.setAdapter(adapter); //here error
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
}

这是一个RecyclerViewAdapter:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
public ArrayList<String> links = new ArrayList<String>();
public Context mContext;
public RecyclerViewAdapter(Context context, ArrayList<String> link ) {
    links = link;
    mContext = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(mContext).inflate(R.layout.layout_listitem, parent, false);
    ViewHolder holder = new ViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(ViewHolder holder, final int position) {

    holder.richLink.setLink(links.get(position), new ViewListener() {
        @Override
        public void onSuccess(boolean status) {
        }
        @Override
        public void onError(Exception e) {
        }
    });
}

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


public class ViewHolder extends RecyclerView.ViewHolder{

    RichLinkViewTwitter richLink;

    public ViewHolder(View itemView) {
        super(itemView);
        Log.d("SONO IN :","VIEW HOLDER");
        richLink = itemView.findViewById(R.id.richLinkView);
    }
}

我收到此错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)' on a null object reference

这是我的xml,名为fragment_news.xml:

<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".News"
        android:paddingLeft="3dp"
        android:paddingRight="3dp">
        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center_horizontal|center_vertical"
            app:cardCornerRadius="24dp">
            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/bg"
                >
                <RelativeLayout
                    android:layout_marginTop="20dp"
                    android:layout_width="fill_parent"
                    android:layout_height="48dp" >
                    <TextView
                        android:text="NEWS"
                        android:gravity="center"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:id="@+id/textView1"
                        android:textStyle="bold"
                        android:textSize="30dp"
                        android:textColor="#FFFFFF" />
                </RelativeLayout>
            <LinearLayout
                android:id="@+id/idLayoutNews"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:background="@drawable/bg"
                >
                <androidx.recyclerview.widget.RecyclerView //this is RecyclerView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/idRecycleView"></androidx.recyclerview.widget.RecyclerView>

            </LinearLayout>
            </LinearLayout>
            <ProgressBar
                android:id="@+id/pBar"
                style="?android:attr/progressBarStyleLarge"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:backgroundTint="@color/md_blue_900"
                android:layout_gravity="center"
                android:scrollbarSize="3dp"
                android:layout_below="@+id/btnShow"
                android:layout_centerHorizontal="true"
                android:visibility="gone"/>
        </androidx.cardview.widget.CardView>
    </FrameLayout>
</ScrollView>

怎么了?想得到答案。

android android-recyclerview recycler-adapter
2个回答
-1
投票

替换

getContext()

with

getActivity()

getActivity()在片段中用于获取对其进行夸大的活动的上下文。


-1
投票

似乎您的适配器中不需要上下文引用。您可以像这样使用父布局的上下文;

LayoutInflater.from(parent.context)

尽可能避免外部依赖。

© www.soinside.com 2019 - 2024. All rights reserved.