带有片段的 RecyclerView
Posted
技术标签:
【中文标题】带有片段的 RecyclerView【英文标题】:RecyclerView with Fragments 【发布时间】:2014-09-18 09:58:12 【问题描述】:我正在尝试在 Fragment
中使用新的 RecyclerView
小部件,但出现此错误:
无法开始活动 组件信息com.example.myapplication/com.example.myapplication.MyActivity: java.lang.NullPointerException:尝试调用接口方法 空对象上的“布尔 java.util.List.add(java.lang.Object)” 参考
我做错了什么?
这是我的Fragment
:
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
public class RecyclerviewFragment extends Fragment
private RecyclerView mRecyclerView;
private RecyclerviewAdapter mRecyclerviewAdapter;
private LinearLayoutManager mLinearLayoutManager;
private List<ViewModel> viewModel;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
View rootView = inflater.inflate(R.layout.fragment_recyclerview, container, false);
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
return rootView;
@Override
public void onActivityCreated(Bundle savedInstanceState)
super.onActivityCreated(savedInstanceState);
mRecyclerviewAdapter = new RecyclerviewAdapter(viewModel);
mLinearLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setAdapter(mRecyclerviewAdapter);
mRecyclerView.setLayoutManager(mLinearLayoutManager);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
还有我的活动:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import java.util.List;
public class MyActivity extends Activity
private List<ViewModel> viewModel;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
RecyclerviewFragment recyclerviewFragment = new RecyclerviewFragment();
getFragmentManager().beginTransaction().add(android.R.id.content, recyclerviewFragment).commit();
viewModel.add(new ViewModel("View"));
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item)
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings)
return true;
return super.onOptionsItemSelected(item);
谢谢!
【问题讨论】:
您还没有为 viewModel 创建对象,这就是错误的原因。 拜托 - 您刚刚遇到了一个愚蠢的 Java 编程问题,忘记初始化变量。这对任何人都没有帮助(除了你)请删除这个问题,因为人们最终来到这里确实对 RecyclerViews 和 Fragments 有问题!感谢您的合作! 【参考方案1】:你没有初始化private List<ViewModel> viewModel
【讨论】:
以上是关于带有片段的 RecyclerView的主要内容,如果未能解决你的问题,请参考以下文章
片段 - 带有 DatePickerDialog 片段的 EditText
我无法从另一个带有 recyclerview.adaper 的片段中打开带有 recyclerview.adapter 的片段