在android中使用RecyclerView时的NPE

时间:2015-07-21 04:16:55

标签: android android-recyclerview

我收到以下错误:

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);
    }
}   

这是我的cardView XML:

<?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>

谢谢!

1 个答案:

答案 0 :(得分:2)

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

这样做: -

import com.eviware.soapui.config.OutgoingWssConfig; OutgoingWssConfig config; config.setName("Test"); config.setUsername("Test"); config.setPassword("Test123"); config.setActor("someendpoint"); config.setMustUnderstand(true);

其中setContentView(R.layout.activity_main);是您的布局代码, 在你的活动的第一行onCreate()