2015-07-21 55 views
0

使用RecyclerView时,我收到以下错误:NPE在android系统

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)' on a null object reference 
      at com.ylg.sms.Main_Recycler.fetchData(Main_Recycler.java:139) 
      at com.ylg.sms.Main_Recycler.Main_Recycler.onCreate(Main_Recycler.java:109) 
      at android.app.Activity.performCreate(Activity.java:5966) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2408) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2519) 
            at android.app.ActivityThread.access$800(ActivityThread.java:162) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412) 
            at android.os.Handler.dispatchMessage(Handler.java:106) 
            at android.os.Looper.loop(Looper.java:189) 
            at android.app.ActivityThread.main(ActivityThread.java:5532) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:372) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745) 

我收到上述错误。我不知道为什么这个未来我尝试了所有在网上给出的方式。有人可以帮我解决这个问题吗?

下面是我想要的代码:

public class Main_Recycler extends AppCompatActivity 
{ 

    @Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
      rv = (RecyclerView)findViewById(R.id.cardList); 

     final LinearLayoutManager layoutManager = new LinearLayoutManager(this); 
     layoutManager.setOrientation(LinearLayoutManager.VERTICAL); 
     rv.setLayoutManager(layoutManager); 
     rv.setHasFixedSize(true); 

     noDataText = (TextView) findViewById(R.id.noData); 

     SMSList= new ArrayList<UnParsedDetails>(); 
    fetchData(); 
} 


private void fetchData() 
{ 
    if (dbvalue.getunMappedMessageCount() > 0) 
    { 
     SMSList.clear(); 

     List<SMSDetails> smsDetails = dbmbip.getsmsData(); 
     for (SMSDetails upd : smsDetails) 
     { 

      String smsitems = upd.getSmsText(); 

      listofSMS = new SMSDetails(smsitems); 
      SMSList.add(listofSMS); 
     } 

     noDataText.setVisibility(View.GONE); 

     adapter = new Main_Adapter_Recycler(SMSList); 
     rv.setAdapter(adapter); 
    } 
    else 
    { 
     noDataText.setVisibility(View.VISIBLE); 
    } 
} 

这里是我的XML为cardView:

<?xml version="1.0" encoding="utf-8"?> 

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/card_view" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_margin="5dp" 
    card_view:cardCornerRadius="4dp"> 

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/layoutbackground" 
     tools:ignore="HardcodedText"> 

     <TextView 
      android:id="@+id/smstext" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:padding="10dp" 
      android:text="@string/backupfooter" 
      android:textColor="@color/blackText" 
      android:textSize="14sp" /> 

    </RelativeLayout> 
</android.support.v7.widget.CardView> 

RecyclerView XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/layoutcolor_background" 
    android:orientation="vertical" 
    tools:ignore="ContentDescription,RtlHardcoded,NestedWeights"> 

    <include 
     android:id="@+id/toolbar" 
     layout="@layout/ylg_toolbar" /> 

    <TextView 
     android:id="@+id/noData" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_above="@+id/imagefooter" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:drawSelectorOnTop="true" 
     android:gravity="center" 
     android:text="@string/smsnodata" 
     android:textColor="#000000" 
     android:textSize="18sp" 
     android:textStyle="italic" 
     android:visibility="gone" /> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/cardList" 
     android:layout_width="match_parent" 
     android:layout_below="@+id/toolbar" 
     android:layout_height="match_parent" /> 

    <ImageView 
     android:id="@+id/imagefooter" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:background="@drawable/footer" 
     tools:ignore="ObsoleteLayoutParam" /> 

</RelativeLayout> 

谢谢!

+0

张贴的代码是没有帮助解决issue.Please后相关的代码。 –

+0

@AnirudhSharma:你想要什么其他的代码? –

+0

你在哪里设置你的适配器回收视图? –

回答

2

您尚未设置此活动的内容视图。

这样做: -

setContentView(R.layout.activity_main);

其中activity_main.xml是你的布局代码, 在活动的onCreate()的第一行