0
Follow
5
View

recyclerView on android Studio [duplicate]

csxzdci 注册会员
2023-01-26 04:44

I lost 16 minutes of my life with this issue, so I'll just admit to this incredibly embarrassing mistake that I was making- I'm using Butterknife and I bind the view in onCreateView in this fragment.

It took a long time to figure out why I had no layoutmanager - but obviously the views are injected so they won't actually be null, so the the recycler will never be null .. whoops!

@BindView(R.id.recycler_view)
RecyclerView recyclerView;

    @Override
public View onCreateView(......) {
    View v = ...;
    ButterKnife.bind(this, v);
    setUpRecycler()
 }

public void setUpRecycler(Data data)
   if (recyclerView == null) {
 /*very silly because this will never happen*/
       LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
       //more setup
       //...
    }
    recyclerView.setAdapter(new XAdapter(data));
}

If you are getting an issue like this trace your view and use something like uiautomatorviewer

cyan1986 注册会员
2023-01-26 04:44

In my case it happened cause i embedded a RecyclerView in a LinearLayout.

I previously had a layout file only containing one root RecyclerView as follows



I believe the problem is within the 3 lines separated. Anyway, I think its a simple problem, ill be working on it tomorrow; thought i should write what i found before forgetting about this thread.

deareffen 注册会员
2023-01-26 04:44

Just add the following to RecyclerView

app:layoutManager="android.support.v7.widget.LinearLayoutManager"

Example:

 recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager"
        app:layout_constraintBottom_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    
dudula2193 注册会员
2023-01-26 04:44

Adding yet another answer since I came across this thread googling the error. I was trying to initialize a PreferenceFragmentCompat but I forgot to inflate the preference XML in onCreatePreferences like this:

class SettingsFragment : PreferenceFragmentCompat() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val inflater = LayoutInflater.from(context)
        inflater.inflate(R.layout.fragment_settings, null)
    }

    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        // Missing this line here:
        //   setPreferencesFromResource(R.xml.settings, rootKey)
    }
}

The error was a mystery until I realized that PreferenceFragmentCompat must be using a RecyclerView internally.

dhxqh1002 注册会员
2023-01-26 04:44

I had the same problem and realized I was setting both the r and after retrieving the data from my source instead of setting the two in the method.

salesAdapter = new SalesAdapter(this,ordersList);
        salesView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        salesView.setAdapter(salesAdapter);

Then notified the adapter on data change

               //get the Orders
                Orders orders;
                JSONArray ordersArray = jObj.getJSONArray("orders");
                    for (int i = 0; i < ordersArray.length() ; i++) {
                        JSONObject orderItem = ordersArray.getJSONObject(i);
                        //populate the Order model

                        orders = new Orders(
                                orderItem.getString("order_id"),
                                orderItem.getString("customer"),
                                orderItem.getString("date_added"),
                                orderItem.getString("total"));
                        ordersList.add(i,orders);
                        salesAdapter.notifyDataSetChanged();
                    }
dazhao2007 注册会员
2023-01-26 04:44

This issue is because you are not adding any LayoutManager for your RecyclerView.

Another reason is because you are calling this code in a NonUIThread. Make sure to call this call in the UIThread.

The solution is only you have to add a LayoutManager for the RecyclerView before you setAdapter in the UI Thread.

recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
zongcailala 注册会员
2023-01-26 04:44

Solved by setting the initialized empty list and adapter at the bottom and calling notifyDataSetChanged when results are fetched.

    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
    recyclerviewItems.setLayoutManager(linearLayoutManager);
    someAdapter = new SomeAdapter(getContext(),feedList);
    recyclerviewItems.setAdapter(someAdapter);
does2006 注册会员
2023-01-26 04:44

I have solved this error. You just need to add layout manager and add the empty adapter.

Like this code:

RecyclerView.setLayoutManager(...//your layout manager);
        myRecyclerView.setAdapter(new RecyclerView.Adapter() {
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                return null;
            }

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

            }

            @Override
            public int getItemCount() {
                return 0;
            }
        });
//other code's 
// and for change you can use if(mrecyclerview.getadapter != speacialadapter){
//replice your adapter
//}
duanbo2001 注册会员
2023-01-26 04:44

In case you're getting still error while using , make sure you're using the binding to return the inflated view ie

   @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return fragmentBinding.getRoot();
}
c198905 注册会员
2023-01-26 04:44

In my situation it was a forgotten component which locates in ViewHolder class but it wasn't located in layout file

About the Author

Question Info

Publish Time
2023-01-26 04:44
Update Time
2023-01-26 04:44

Related Question

如何设置绝对路径导入为默认的Android工作室?

关于hive on spark的一些问题

文件存储python

有可能在Android或Flutter上从零开始,在3个月内学会如何制作应用程序吗?

jmeter启动java.awt.HeadlessException: null

如何通过Python使用SeleniumUI自动化,来自定义保存PDF的名称?

android root后,怎么安装软件

Python抓取website_tab

如何在列表python中获得给定字符串的索引?

未捕获错误:期望' onClick '监听器是一个函数,而不是得到一个'字符串'类型的值