-1

benim aktivitesinde RecycleView oluşturmaya çalışıyorum ancak lansman ve log köşesindeki uygulama çökerGeri dönüştürücümde NullPointerException hatasını nasıl düzeltebilirim?

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) 

Bütün gün bu düzeltemez diyor, bu yüzden My sınıfları edebilirsiniz ümit: 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(); 
} 

ve 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" /> 

+1

Lütfen etkinlik_main xml'nizi paylaşabilir misiniz? – mmcoder10

+0

Lütfen MainActivity ürününüzde bulunan içe aktarma ifadelerini yapıştırın. –

+0

MainActivity.xml dosyasını ekledim, fakat işitme engelli parazitler var –

cevap

0

Koymalısın contnent_main.xml için activity_main.xml numaralı bölüme, findViewById nolu numarayı kullanmak istemiyorsanız, bunun için null.

AppBarLayout altındaki <include layout="@layout/contnent_main"/> gibi bir şey.

0

Recycler Görünüm setLayoutManager girdi olarak RecyclerView.LayoutManager tipi yüzden

ile

final LinearLayoutManager layoutManager = new LinearLayoutManager(this); 

değiştirip

layoutManager.setOrientation(LinearLayoutManager.VERTICAL); 
layoutManager.generateDefaultLayoutParams(); 

doğru kullanım için geliştirici sayfasından bakabilirsiniz kaldırmak beklediğini http://developer.android.com/training/material/lists-cards.html

+0

Teşekkürler, ama yine de işe yaramıyor ( –

İlgili konular