我的应用程序在显示RecyclerView之前突然关闭

时间:2017-07-25 17:59:15

标签: android android-recyclerview recyclerview-layout

我正在使用recyclerView我正在尝试制作一个有2个布局的recyclerView,但问题是,当我运行模拟器并单击按钮在recyclerview中显示项目时应用程序突然关闭 这是适配器的代码

package com.example.abdelmagied.myapplication;

import android.content.Context;
import android.database.Cursor;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by AbdELMagied on 7/25/2017.
 */
public class recycleradapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    public Context context;
    public Cursor mycursor;
    public ArrayList<String> mydata;
    private static final int VIEW_TYPE_DARK = 0;
    private static final int VIEW_TYPE_LIGHT = 1;

    public recycleradapter(Context context, ArrayList<String> mydata) {
        this.context = context;
        this.mydata = mydata;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        int viewid;
        switch (viewType) {
            case VIEW_TYPE_DARK:
                viewid = R.layout.recycler_row_dark;
                break;
            case VIEW_TYPE_LIGHT:
                viewid = R.layout.recycler_row_light;
                break;
            default:
                throw new IllegalArgumentException("Invaild Value type : " + viewType);
        }
        View view = LayoutInflater.from(context).inflate(viewid, parent, false);

       if(viewid == VIEW_TYPE_DARK){
           return new ViewHolder0(view);
       }else{
           return new ViewHolder1(view);
       }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
         int typeid = getItemViewType(position);
         String xx  = mydata.get(position);
         String[] x   = xx.split(",");

          switch (holder.getItemViewType())
          {
              case VIEW_TYPE_DARK:{
                  ViewHolder0 viewholder = (ViewHolder0) holder;
                  viewholder.txt6.setText(x[1]);
                  viewholder.txt7.setText(x[2]);
                  break;
              }
              case VIEW_TYPE_LIGHT:{
                  ViewHolder1 viewholder = (ViewHolder1) holder;
                  viewholder.txt8.setText(x[1]);
                  viewholder.txt9.setText(x[2]);
                  break;
              }
          }
    }


    @Override
    public int getItemViewType(int position) {
        if (position % 2 == 0)
            return VIEW_TYPE_DARK;
        return VIEW_TYPE_LIGHT;
    }


    @Override
    public int getItemCount() {
        return mydata.size();
    }


    // for the recycler_row_light layout..
   class ViewHolder0 extends RecyclerView.ViewHolder
   {
      public TextView txt6;
       public TextView txt7;
       public ViewHolder0(View itemView) {
           super(itemView);
           txt6 = (TextView) itemView.findViewById(R.id.textView6);
           txt7 = (TextView) itemView.findViewById(R.id.textView7);
       }
   }

    // for the recycler_row_dark layout...
    class ViewHolder1 extends RecyclerView.ViewHolder{
        public TextView txt9;
        public TextView txt8;
            public ViewHolder1(View itemView){
                super(itemView);
                txt8 = (TextView) itemView.findViewById(R.id.textView8);
                txt9 = (TextView) itemView.findViewById(R.id.textView9);
            }
    }
}

这里是我调整recyclelerview的展示活动

package com.example.abdelmagied.myapplication;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;

public class show extends AppCompatActivity{

    public RecyclerView mrecyclerview;
    public RecyclerView.LayoutManager mymanager;
    public RecyclerView.Adapter myadapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);


        // fetch all data from the database and put it in an arraylist
        mydatabase database = new mydatabase(this);
        Cursor alldatabase  = database.getData();
        String data = "";
        ArrayList<String> mydata = new ArrayList<String>();
        while(alldatabase.moveToNext()){
            data += alldatabase.getString(1);
            data += ",";
            data += alldatabase.getString(2);
            data += ",";
            data += alldatabase.getString(3);
            data += ",";
            data += alldatabase.getString(4);
            data += ",";
            data += alldatabase.getString(5);
            mydata.add(data);
            data = "";

        }
        mrecyclerview = (RecyclerView) findViewById(R.id.RecyclerId);
        mymanager     = new LinearLayoutManager(this);
        myadapter     = new recycleradapter(this , mydata);
        mrecyclerview.setLayoutManager(mymanager);
        mrecyclerview.setAdapter(myadapter);

    }

}

这是包含recyclerview的活动展示

<?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_show"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.abdelmagied.myapplication.show">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/RecyclerId" />

</RelativeLayout>

这是第一个名为recycler_row_dark

的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="@android:color/holo_blue_bright"
        android:id="@+id/textView8">

        <ImageView
            android:layout_width="90dp"
            android:layout_height="90dp"
            app:srcCompat="@android:drawable/stat_notify_missed_call"
            android:id="@+id/imageView" />

        <TextView
            android:text="NAme"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView8"
            android:textSize="35sp" />

        <TextView
            android:text=": Number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView9"
            android:layout_weight="1"
            android:textSize="25sp" />

    </LinearLayout>

</LinearLayout>

这是第二个名为recycler_row_light

的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
        android:background="@android:color/darker_gray">

        <ImageView
            android:layout_width="90dp"
            android:layout_height="90dp"
            app:srcCompat="@android:drawable/stat_notify_missed_call"
            android:id="@+id/imageView" />

        <TextView
            android:text="NAme"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView6"
            android:textSize="35sp" />

        <TextView
            android:text=": Number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView7"
            android:layout_weight="1"
            android:textSize="25sp" />

    </LinearLayout>

</LinearLayout>

这是我的logcat的照片 enter image description here

和尝试进行调试时的这张照片 enter image description here

1 个答案:

答案 0 :(得分:1)

将您的代码更改为

Cursor alldatabase  = database.getData();
String data = "";
ArrayList<String> mydata = new ArrayList<String>();

if (alldatabase.moveToFirst()) {
  while(!alldatabase.isAfterLast()) { 
  // If you use alldatabase.moveToNext() here, you will bypass the first row, which is WRONG
    ...
    alldatabase.moveToNext();
  } 
}

问题是你没有检查游标是否返回null值。所以请先检查然后再进行操作。

此日志条目也没有用,因为它不显示任何与应用程序有关的日志。

<强>更新

另外一个问题是在你的黑暗布局中,因为你已经给线性布局和textview都提供了相同的id,所以也要进行校正。