我在导航抽屉中有菜单,其中包括"电影,音乐,书籍和#34;我决定为每个类别创建Fragment和adapter,因此它将显示为不同的列表。
这是我的第一个片段的代码 - MoviesFragment.java
public class MoviesFragment extends Fragment {
private MovieAdapter movieAdapter;
@BindView(R.id.moviesRecyclerView)
RecyclerView moviesRecyclerView;
public MoviesFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_movies, container, false);
ButterKnife.bind(this, view);
moviesRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
moviesRecyclerView.setLayoutManager(layoutManager);
moviesRecyclerView.setAdapter(movieAdapter);
moviesRecyclerView.setItemAnimator(new DefaultItemAnimator());
return view;
}}
我尝试将其与我的MovieAdapter.java连接,代码是:
public class MovieAdapter extends RecyclerView.Adapter<MovieViewHolder>{
private final FragmentActivity layoutInflater;
private List<String> movies = new ArrayList<>();
public MovieAdapter(FragmentActivity layoutInflater){
this.layoutInflater = layoutInflater;
movies.add("Pulp Fcition");
}
@Override
public MovieViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// View view = layoutInflater.inflate(android.R.layout.simple_list_item_1, parent, false);
// return new MovieViewHolder(view);
return null;
}
@Override
public void onBindViewHolder(MovieViewHolder holder, int position) {
String movieTitle = movies.get(position);
holder.setMovieTitle(movieTitle);
}
@Override
public int getItemCount() {
return movies.size();
}
}
class MovieViewHolder extends RecyclerView.ViewHolder{
private String movieTitle;
private TextView textView;
public MovieViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView;
}
public void setMovieTitle(String movieTitle) {
this.movieTitle = movieTitle;
textView.setText(movieTitle);
}
public String getMovieTitle() {
return movieTitle;
}
}
但我收到此错误:
7896-7896/zakrzk.myappname E/AndroidRuntime: FATAL EXCEPTION: main
Process: zakrzk.kurwtura, PID: 7896
Theme: themes:{default=overlay:system, iconPack:system, fontPkg:system, com.android.systemui=overlay:system, com.android.systemui.navbar=overlay:system}
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
at zakrzk.kurwtura.MoviesFragment.onCreateView(MoviesFragment.java:40)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5461)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102)
我该如何解决?
轻松向列表添加新职位的最佳解决方案是什么?
问候
答案 0 :(得分:0)
这可能是由此退货声明引起的:
@Override
public MovieViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// View view = layoutInflater.inflate(android.R.layout.simple_list_item_1, parent, false);
// return new MovieViewHolder(view);
return null;
}
为什么返回null?
答案 1 :(得分:0)
这里发生了什么?
class MovieViewHolder extends RecyclerView.ViewHolder{
private String movieTitle;
private TextView textView;
public MovieViewHolder(View itemView) {
super(itemView);
//you have to findViewById here and not just cast it!!
textView = (TextView) itemView;
}
public void setMovieTitle(String movieTitle) {
this.movieTitle = movieTitle;
textView.setText(movieTitle);
}
public String getMovieTitle() {
return movieTitle;
}
}
另外,请勿在适配器中返回null
!
修改强>
您似乎也在设置layoutManager
两次!!
我希望这有帮助!