当我们尝试在recylerview中设置适配器时,从recylerview中获取错误

时间:2018-09-06 07:43:58

标签: java android xml

  

ComponentInfo {enkay.ntucsingapoor / enkay.ntucsingapoor.Activity.BabyDiapers}:    尝试调用虚方法'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.SetData(BabyDiapers.java:56)                                                                          在   enkay.ntucsingapoor.Activity.BabyDiapers.onCreate(BabyDiapers.java:42)                                                                          在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)

尝试将数据从Sqite设置为Reclyerview时出现此错误

  

这是我想在哪里收集数据的活动

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.analytics.ecommerce.Product;

import java.util.ArrayList;
import java.util.List;

import enkay.ntucsingapoor.Adapters.MarketAdapters;
import enkay.ntucsingapoor.Helper.SQlHelper;
import enkay.ntucsingapoor.R;
import enkay.ntucsingapoor.model.MarketModel;

public class BabyDiapers extends AppCompatActivity {


    RecyclerView market_data_recylerview;
    private static final String TAG = "BabyDiapers";
    SQlHelper myDatabaseHelper;
    private SQlHelper market;
    List<MarketModel> model;
    MarketAdapters adapters;
    MarketModel marketModel;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_baby_diapers);


        SetData();
    }

    private void SetData() {

            model = new ArrayList<>();
            market_data_recylerview = (RecyclerView) findViewById(R.id.List_items);
            model = new ArrayList<MarketModel>();
            myDatabaseHelper = new SQlHelper(BabyDiapers.this);
            model = myDatabaseHelper.getAllMarketData();
            adapters = new MarketAdapters(model);

            final LinearLayoutManager reLayoutManager = new LinearLayoutManager(getApplicationContext());
            reLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            market_data_recylerview.setLayoutManager(reLayoutManager);
            Log.i(TAG, "onCreate: RecylerView =" + market_data_recylerview);
            market_data_recylerview.setItemAnimator(new DefaultItemAnimator());
            market_data_recylerview.setHasFixedSize(true);
            market_data_recylerview.setAdapter(adapters);

            Log.i("HIteshdata", "" + adapters);




    }
}
  

此活动的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>
  

DbHelperClass

package enkay.ntucsingapoor.Helper;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import enkay.ntucsingapoor.Activity.DataInsert;
import enkay.ntucsingapoor.model.MarketModel;

/**
 * Created by lenovo on 8/8/2018.
 */

public class SQlHelper extends SQLiteOpenHelper {

    private static final String TAG="SQLHelper";
    MarketModel model=new MarketModel();

    SQLiteOpenHelper dbhandler;
    SQLiteDatabase database;

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "supermarketdata";
    private static final String TABLE_NAME = "market_comparision";

    private static final String KEY_ID = "id";

    private static final String KEY_BRAND_NAME ="brand_name";

    private static final String KEY__FAIRPRICE_PRODUCT_NAME = "product_fairprice";
    private static final String KEY_FAIRPRICE_PRICE_NO = "price_fairprice";

    private static final String KEY__All_IN_FOR_YOU_PRODUCT_NAME = "product_all_in_for_you";
    private static final String KEY_ALL_IN_FOR_PRODUCT_PRICE_NO = "price_all_in_for_you";

    private static final String KEY__GIANT_ONLINE_PRODUCT_NAME = "product_giant_online";
    private static final String KEY_GIANT_ONLINE_PRODUCT_PRICE_NO = "price_giant_online";
    private static final String KEY_GIANT_ONLINE_PRODUCT_CATEGERY="categery_giant_online";

    private static final String KEY__COLD_STORAGE_PRODUCT_NAME = "product_cold_storage";
    private static final String KEY_COLD_STORAGE_PRODUCT_PRICE_NO = "price_cold_storage";
    SQLiteDatabase db;




    public SQlHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }




    @Override
    public void onCreate(SQLiteDatabase db) {


        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_NAME + "("
                + KEY_ID + " INTEGER PRIMARY KEY,"
                + KEY_BRAND_NAME + " TEXT,"
                + KEY__FAIRPRICE_PRODUCT_NAME + " TEXT,"
                + KEY_FAIRPRICE_PRICE_NO + " TEXT,"
                + KEY__All_IN_FOR_YOU_PRODUCT_NAME + " TEXT,"
                + KEY_ALL_IN_FOR_PRODUCT_PRICE_NO + " TEXT,"
                + KEY__GIANT_ONLINE_PRODUCT_NAME + " TEXT,"
                + KEY_GIANT_ONLINE_PRODUCT_PRICE_NO + " TEXT,"
                + KEY_GIANT_ONLINE_PRODUCT_CATEGERY + " TEXT,"
                + KEY__COLD_STORAGE_PRODUCT_NAME + " TEXT,"
                + KEY_COLD_STORAGE_PRODUCT_PRICE_NO + " TEXT" + ")";


        db.execSQL(CREATE_CONTACTS_TABLE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {

        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }

    public void InsertMArket(MarketModel marketModel) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();

        values.put(KEY_BRAND_NAME, marketModel.getBrand_name());

        values.put(KEY__FAIRPRICE_PRODUCT_NAME, marketModel.getFair_Price_product());
        values.put(KEY_FAIRPRICE_PRICE_NO, marketModel.getFair_Price_Product_Price());

        values.put(KEY__All_IN_FOR_YOU_PRODUCT_NAME, marketModel.getAll_In_For_You_Product());
        values.put(KEY_ALL_IN_FOR_PRODUCT_PRICE_NO, marketModel.getAll_In_For_You_Product_Price());

        values.put(KEY__GIANT_ONLINE_PRODUCT_NAME, marketModel.getGiant_Online_Product());
        values.put(KEY_GIANT_ONLINE_PRODUCT_PRICE_NO, marketModel.getGiant_Online_product_price());
        values.put(KEY_GIANT_ONLINE_PRODUCT_CATEGERY, marketModel.getGiant_Online_product_categery());

        values.put(KEY__COLD_STORAGE_PRODUCT_NAME, marketModel.getCold_Storage_Product());
        values.put(KEY_COLD_STORAGE_PRODUCT_PRICE_NO, marketModel.getCold_Storage_price());

        db.insert(TABLE_NAME, null, values);
        db.close();
    }

//public    MarketModel getMArket(int id) {
//        SQLiteDatabase db = this.getReadableDatabase();
//
//        Cursor cursor = db.query(TABLE_NAME, new String[] {  KEY_ID,
//                        KEY_BRAND_NAME,
//                        KEY__All_IN_FOR_YOU_PRODUCT_NAME,
//                        KEY_ALL_IN_FOR_PRODUCT_PRICE_NO,
//                        KEY__FAIRPRICE_PRODUCT_NAME,
//                        KEY_FAIRPRICE_PRICE_NO,
//                        KEY__GIANT_ONLINE_PRODUCT_NAME,
//                        KEY_GIANT_ONLINE_PRODUCT_PRICE_NO,
//                        KEY_GIANT_ONLINE_PRODUCT_CATEGERY,
//                        KEY__COLD_STORAGE_PRODUCT_NAME,
//                        KEY_COLD_STORAGE_PRODUCT_PRICE_NO
//                }
//                , KEY_ID + "=?",
//                new String[] { String.valueOf(id) }, null, null, null, null);
//        if (cursor != null)
//            cursor.moveToFirst();
//
//        MarketModel market = new MarketModel(Integer.parseInt(cursor.getString(0)),
//                cursor.getString(1), cursor.getString(2));
//
//        return market;
//    }
    public List<MarketModel> getAllMarketData() {
        //Log.i(TAG, "getAllMarketData: checkData = " + MarketModel.class);
        String colums[]={
            KEY__FAIRPRICE_PRODUCT_NAME,
                KEY_FAIRPRICE_PRICE_NO,
                KEY_BRAND_NAME,
                KEY__All_IN_FOR_YOU_PRODUCT_NAME,
                KEY_ALL_IN_FOR_PRODUCT_PRICE_NO,
                KEY__GIANT_ONLINE_PRODUCT_NAME,
                KEY_GIANT_ONLINE_PRODUCT_PRICE_NO,
                KEY_GIANT_ONLINE_PRODUCT_CATEGERY,
                KEY__COLD_STORAGE_PRODUCT_NAME,
                KEY_COLD_STORAGE_PRODUCT_PRICE_NO

        };



        String sortOrder =
                KEY__FAIRPRICE_PRODUCT_NAME + " ASC";

        List<MarketModel> marketList = new ArrayList<MarketModel>();
        Log.i(TAG, "getAllMarketData: Data= " + marketList.isEmpty());
        //Select Fair_price_product from tablename
        //String whereClouse =KEY__FAIRPRICE_PRODUCT_NAME +"=?";
        String selectQuery = "SELECT  * FROM " + TABLE_NAME;
       // String [] selectionArgs=new String[]{"Product"};

        Log.i(TAG, "getAllMarketData: DataQuery=" + selectQuery);

        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.rawQuery(selectQuery, null);
        Log.i(TAG, "getAllMarketData: dataCursor=  " +  cursor.getCount());


        if (cursor.moveToFirst()) {
            do {
               MarketModel market=new MarketModel();
                market.setBrand_name(cursor.getString(cursor.getColumnIndex(KEY_BRAND_NAME)));
                market.setFair_Price_product(cursor.getString(cursor.getColumnIndex(KEY__FAIRPRICE_PRODUCT_NAME)));
                  Log.i(TAG, "getAllMarketData: coding=" + market.getFair_Price_product());
                market.setFair_Price_Product_Price(cursor.getString(cursor.getColumnIndex(KEY_FAIRPRICE_PRICE_NO)));
                  Log.i(TAG, "getAllMarketData: coding=" + market.getFair_Price_Product_Price());
//                market.setAll_In_For_You_Product(cursor.getString(cursor.getColumnIndex(KEY__All_IN_FOR_YOU_PRODUCT_NAME)));
//                Log.i(TAG, "getAllMarketData: coding=" + market.getFair_Price_product());
//                market.setAll_In_For_You_Product_Price(cursor.getString(cursor.getColumnIndex(KEY_ALL_IN_FOR_PRODUCT_PRICE_NO)));
//                market.setGiant_Online_Product(cursor.getString(cursor.getColumnIndex(KEY__GIANT_ONLINE_PRODUCT_NAME)));
//                market.setGiant_Online_product_price(cursor.getString(cursor.getColumnIndex(KEY_GIANT_ONLINE_PRODUCT_PRICE_NO)));
//                market.setGiant_Online_product_categery(cursor.getString(cursor.getColumnIndex(KEY_GIANT_ONLINE_PRODUCT_CATEGERY)));
//                market.setCold_Storage_Product(cursor.getString(cursor.getColumnIndex(KEY__COLD_STORAGE_PRODUCT_NAME)));
//                market.setCold_Storage_price(cursor.getString(cursor.getColumnIndex(KEY_COLD_STORAGE_PRODUCT_PRICE_NO)));

                marketList.add(market);

            } while (cursor.moveToNext());
            cursor.close();

            db.close();


            Log.i(TAG, "getAllUser: List empty = "+marketList.isEmpty());



        }


        return marketList;
    }



//   public List<MarketModel> getAllMarket()
//   {
//        String TABLE_NAME="market_comparision";
//        MarketModel market=new MarketModel();
//        String query = "SELECT * FROM  "+TABLE_NAME
//               +" WHERE "+KEY__FAIRPRICE_PRODUCT_NAME+" = ?";
//
//        Log.i(TAG, "getUserDetail: market = "+market);
//
//        Cursor cursor = getReadableDatabase().rawQuery(query, null);
//       Log.i(TAG, "getAllMarket: Data " + cursor.getCount());
//        while (cursor.moveToNext())
//        {
//            market.setBrand_name(cursor.getString(cursor.getColumnIndex(KEY_BRAND_NAME)));
//            market.setFair_Price_product(cursor.getString(cursor.getColumnIndex(KEY__FAIRPRICE_PRODUCT_NAME)));
//            market.setFair_Price_Product_Price(cursor.getString(cursor.getColumnIndex(KEY_FAIRPRICE_PRICE_NO)));
//            market.setAll_In_For_You_Product(cursor.getString(cursor.getColumnIndex(KEY__All_IN_FOR_YOU_PRODUCT_NAME)));
//            market.setAll_In_For_You_Product_Price(cursor.getString(cursor.getColumnIndex(KEY_ALL_IN_FOR_PRODUCT_PRICE_NO)));
//            market.setGiant_Online_Product(cursor.getString(cursor.getColumnIndex(KEY__GIANT_ONLINE_PRODUCT_NAME)));
//            market.setGiant_Online_product_price(cursor.getString(cursor.getColumnIndex(KEY_GIANT_ONLINE_PRODUCT_PRICE_NO)));
//            market.setGiant_Online_product_categery(cursor.getString(cursor.getColumnIndex(KEY_GIANT_ONLINE_PRODUCT_CATEGERY)));
//            market.setCold_Storage_Product(cursor.getString(cursor.getColumnIndex(KEY__COLD_STORAGE_PRODUCT_NAME)));
//            market.setCold_Storage_price(cursor.getString(cursor.getColumnIndex(KEY_COLD_STORAGE_PRODUCT_PRICE_NO)));
//
//            Log.i(TAG, "getAllMarket: market" +cursor);
//           // marketList.add(market);
//        }
//
//        return (List<MarketModel>) market;
//    }



    public int updateMArket(MarketModel market) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();

        values.put(KEY_BRAND_NAME,DataInsert.marketModel1.getBrand_name());

        values.put(KEY__FAIRPRICE_PRODUCT_NAME, DataInsert.marketModel1.getFair_Price_product());
        values.put(KEY_FAIRPRICE_PRICE_NO,DataInsert.marketModel1.getFair_Price_Product_Price());

        values.put(KEY__All_IN_FOR_YOU_PRODUCT_NAME,DataInsert.marketModel1.getAll_In_For_You_Product());
        values.put(KEY_ALL_IN_FOR_PRODUCT_PRICE_NO,DataInsert.marketModel1.getAll_In_For_You_Product_Price());

        values.put(KEY__GIANT_ONLINE_PRODUCT_NAME,DataInsert.marketModel1.getGiant_Online_Product());
        values.put(KEY_GIANT_ONLINE_PRODUCT_PRICE_NO,DataInsert.marketModel1.getGiant_Online_product_price());
        values.put(KEY_GIANT_ONLINE_PRODUCT_CATEGERY,DataInsert.marketModel1.getGiant_Online_product_categery());

        values.put(KEY__COLD_STORAGE_PRODUCT_NAME,DataInsert.marketModel1.getCold_Storage_Product());
        values.put(KEY_COLD_STORAGE_PRODUCT_PRICE_NO,DataInsert.marketModel1.getCold_Storage_price());


        // updating row
        return db.update(TABLE_NAME, values, KEY_ID + " = ?",
                new String[] { String.valueOf(market.getId()) });
    }

    // Deleting single contact
    public void deleteMarket(MarketModel market) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_NAME, KEY_ID + " = ?",
                new String[] { String.valueOf(market.getId()) });
        db.close();
    }


    // Getting contacts Count
    public int getContactsCount() {
        String countQuery = "SELECT  * FROM " + TABLE_NAME;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();

        // return count
        return cursor.getCount();
    }
}
  

我的适配器类

package enkay.ntucsingapoor.Adapters;


import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import enkay.ntucsingapoor.R;
import enkay.ntucsingapoor.model.MarketModel;

/**
 * Created by lenovo on 9/1/2018.
 */

public class MarketAdapters extends RecyclerView.Adapter<MarketAdapters.MarketViewHolder> {


    List<MarketModel> modelList;

    public MarketAdapters(List<MarketModel> modelList)
    {
       this.modelList=modelList;
    }

    @Override
    public MarketViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_baby_diapers,null);
        return new MarketViewHolder(view);
    }

    @Override

    public void onBindViewHolder(MarketViewHolder holder, int position) {
     MarketModel model=modelList.get(position);

    }

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

    public class MarketViewHolder extends RecyclerView.ViewHolder {


        public MarketViewHolder(View itemView) {
            super(itemView);

        }
    }
}

0 个答案:

没有答案