我从recylerview中获取错误,因为我试图从sqlite获取数据并将其设置为recylerview

时间:2018-09-06 10:01:47

标签: java

  

09-06 15:12:57.845 25003-25003 / enkay.ntucsingapoor E / AndroidRuntime:   致命异常:主要                                                                        程序:enkay.ntucsingapoor,PID:25003                                                                        java.lang.RuntimeException:无法启动活动   ComponentInfo {enkay.ntucsingapoor / enkay.ntucsingapoor.Activity.BabyDiapers}:   java.lang.NullPointerException:尝试调用虚拟方法'void   android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView $ LayoutManager)'   在空对象引用上                                                                            在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)                                                                            在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)                                                                            在android.app.ActivityThread.-wrap11(未知来源:0)                                                                            在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1593)                                                                            在android.os.Handler.dispatchMessage(Handler.java:105)                                                                            在android.os.Looper.loop(Looper.java:164)                                                                            在android.app.ActivityThread.main(ActivityThread.java:6541)                                                                            在java.lang.reflect.Method.invoke(本机方法)                                                                            在   com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)                                                                            在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)                                                                         原因:java.lang.NullPointerException:尝试调用虚拟   方法“无效”   android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView $ LayoutManager)'   在空对象引用上                                                                            在   enkay.ntucsingapoor.Activity.BabyDiapers.onCreate(BabyDiapers.java:55)                                                                            在android.app.Activity.performCreate(Activity.java:6975)                                                                            在   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)                                                                            在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)                                                                            在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)                                                                            在android.app.ActivityThread.-wrap11(未知来源:0)                                                                            在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1593)                                                                            在android.os.Handler.dispatchMessage(Handler.java:105)                                                                            在android.os.Looper.loop(Looper.java:164)                                                                            在android.app.ActivityThread.main(ActivityThread.java:6541)                                                                            在java.lang.reflect.Method.invoke(本机方法)                                                                            在   com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)                                                                            在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

     

这是我的Xml文件MArket_Recyler

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyler_data"
        android:visibility="gone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

    </android.support.v7.widget.RecyclerView>

</LinearLayout>
  

这是我的另一个xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_baby_diapers"
    android:layout_width="match_parent"
    android:background="@color/colorNavIcon"
    android:layout_height="match_parent"
    tools:context="enkay.ntucsingapoor.Activity.BabyDiapers">


    <TextView
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:text="Search Bar"
        android:textSize="34dp"
        android:textStyle="bold"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="53dp"
        android:layout_marginTop="48dp"
        android:orientation="horizontal"
        >


        <Button
            android:layout_width="99dp"
            android:layout_marginLeft="279dp"
            android:layout_height="wrap_content"
            android:text="GO"
            android:background="@color/Yellow"
            />
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="111dp">

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

   </android.support.v7.widget.RecyclerView>

    </LinearLayout>


</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

解决方案:

在您的setContentView中,

将R.layout.activity_baby尿布更改为R.layout.baby_diapers。然后将两个xml粘贴到问题中,例如项目xml和mainactivity xml

就这样