我试图在我的活动中创建RecycleView,但应用程序在启动时崩溃并且日志显示
Caused by: 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 com.punk.home.snoopwatches.MainActivity.onCreate(MainActivity.java:55)
我无法整天解决这个问题,所以我希望你能这样做 我的课程: MainActivity.class:
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import java.util.ArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imagesIdList=new ArrayList<>();
imagesIdList.add(R.drawable.palm);
imagesIdList.add(R.drawable.aliens);
imagesIdList.add(R.drawable.palm);
imagesIdList.add(R.drawable.aliens);
imagesIdList.add(R.drawable.palm);
imagesIdList.add(R.drawable.aliens);
imagesIdList.add(R.drawable.palm);
imagesIdList.add(R.drawable.aliens);
final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
layoutManager.generateDefaultLayoutParams();
recyclerView=(RecyclerView) findViewById(R.id.my_recycler_view);
recyclerView.setLayoutManager(layoutManager);
// recyclerView.setHasFixedSize(true);
adapter=new ImageAdapter(MainActivity.this, imagesIdList);
recyclerView.setAdapter(adapter);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(this);
}
RecyclerView recyclerView;
ArrayList<Integer> removedItems;
ArrayList<Integer> imagesIdList;
ImageAdapter adapter;
ImageAdapter.class
ImageView imageView;
public ImageViewHolder(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.image);
}
}
List<Integer> imageId;
ImageAdapter(Context context, List<Integer> imageId) {
this.imageId = imageId;
this.context = context;
}
Context context;
@Override
public ImageViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_image_activity, viewGroup, false);
ImageViewHolder ivh = new ImageViewHolder(view);
return ivh;
}
@Override
public void onBindViewHolder(ImageViewHolder holder, int position) {
holder.imageView.setImageResource(imageId.get(position));
}
@Override
public int getItemCount() {
return imageId.size();
}
和contnent_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.punk.home.snoopwatches.MainActivity"
tools:showIn="@layout/activity_main">
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
item_image_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
tools:showIn="@layout/content_main"
android:id="@+id/item_layout">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image"/>
</LinearLayout>
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/add" />
答案 0 :(得分:0)
RecyclerView setLayoutManager期望将RecyclerView.LayoutManager类型作为输入进行更改
final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
与
final RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
并删除
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
layoutManager.generateDefaultLayoutParams();
查看开发者页面以了解正确用法 http://developer.android.com/training/material/lists-cards.html
答案 1 :(得分:0)
如果您要使用ContextMenuStrip
并且没有返回null,则需要将<include>
的{{1}}部分放入contnent_main.xml
。
AppBarLayout下面的activity_main.xml
之类的东西。