私は私の活動にRecycleViewを作成しようとしているが、起動時にアプリがクラッシュすると、ログが、私は一日中、この問題を解決することはできませんので、私はあなたが私のクラスを でき望んリサイクラーでNullPointerExceptionエラーを修正するにはどうすればよいですか?
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" />
あなたはactivity_main xmlを共有していただけますか? – mmcoder10
MainActivityに含まれるインポートステートメントを貼り付けてください。 –
MainActivity.xmlを追加しましたが、それには無効なパラメータがあります –