gpt4 book ai didi

java - viewPager 上的 RecyclerView 崩溃

转载 作者:太空宇宙 更新时间:2023-11-04 13:32:22 25 4
gpt4 key购买 nike

起初,我的 recyclerview 在没有 ViewPager 的情况下从头开始顺利运行。但是,当我尝试在 ViewPager 适配器上添加 recyclerview 时,它崩溃了,并显示错误位于 getAllData() 的数据库帮助程序中。提前TY。希望有人能帮我解决这个问题。

这是我的代码。

FragmentMeal.java

package inncharge.poy.madrigal.innchargev1.fragments;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import inncharge.poy.madrigal.innchargev1.R;
import inncharge.poy.madrigal.innchargev1.activities.DatabaseHelper;
import inncharge.poy.madrigal.innchargev1.adapters.DataBeanAdapter;
import inncharge.poy.madrigal.innchargev1.pojo.DataBean;


/**
* A simple {@link Fragment} subclass.
* Use the {@link FragmentMeal#newInstance} factory method to
* create an instance of this fragment.
*/
public class FragmentMeal extends Fragment {



// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
DatabaseHelper helper = new DatabaseHelper(getActivity());
private RecyclerView mRecyclerView;


/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment FragmentMeal.
*/
// TODO: Rename and change types and number of parameters
public static android.support.v4.app.Fragment newInstance(String param1, String param2) {
FragmentMeal fragment = new FragmentMeal();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}

public FragmentMeal() {
// Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}

}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_meal, container, false);
mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setAdapter(new DataBeanAdapter(helper.getAllData(), R.layout.item));
return view;
}



}

DatabaseHelper.java

    package inncharge.poy.madrigal.innchargev1.activities;

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

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

import inncharge.poy.madrigal.innchargev1.pojo.DataBean;

/**
* Created by Madrigal on 8/9/2015.
*/
public class DatabaseHelper extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "contacts.db";
private static final String TABLE_NAME = "contacts";
private static final String UID = "id";
private static final String NAME = "name";
private static final String CARD = "card";
private static final String CODE = "code";
SQLiteDatabase db;
private static final String TABLE_CREATE = "CREATE TABLE contacts (id INTEGER PRIMARY KEY NOT NULL," +
"name TEXT NOT NULL , card TEXT NOT NULL, code TEXT NOT NULL)";

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

}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABLE_CREATE);
this.db = db;
}

public void insertBean(DataBean bean) {

db = this.getWritableDatabase();
ContentValues values = new ContentValues();

String query = "SELECT * FROM contacts";
Cursor cursor = db.rawQuery(query, null);
int count = cursor.getCount();

values.put(UID, count);
values.put(NAME, bean.getName());
values.put(CARD, bean.getCard());
values.put(CODE, bean.getCode());

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

}

public List<DataBean> getAllData() {
db = this.getWritableDatabase();
String query = "SELECT * FROM contacts";
Cursor cursor = db.rawQuery(query, null);

List<DataBean> list = new ArrayList<>();

while (cursor.moveToNext()) {
int index = cursor.getColumnIndex(UID);
int index2 = cursor.getColumnIndex(NAME);
int index3 = cursor.getColumnIndex(CARD);
int index4 = cursor.getColumnIndex(CODE);
int id = cursor.getInt(index);
String name = cursor.getString(index2);
String card = cursor.getString(index3);
String code = cursor.getString(index4);
DataBean bean = new DataBean();
bean.setId(id);
bean.setName(name);
bean.setCard(card);
bean.setCode(code);
list.add(bean);
}
return list;
}


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String query = "DROP TABLE IF EXIST"+TABLE_NAME;
db.execSQL(query);
this.onCreate(db);
}
}

ViewPager适配器

private class ViewPagerAdapter extends FragmentStatePagerAdapter {

int icons[] = {R.drawable.ic_action_meal,
R.drawable.ic_action_drinks,
R.drawable.ic_action_snack};

public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}

public Fragment getItem(int num) {

Fragment fragment = null;

switch (num) {
case MEAL:
fragment = FragmentMeal.newInstance("", "");
break;
case DRINKS:
fragment = FragmentDrinks.newInstance("", "");
break;
case SNACK:
fragment = FragmentSnack.newInstance("", "");
break;
}
return fragment;
}

@Override
public int getCount() {

return 3;
}

@Override
public CharSequence getPageTitle(int position) {
return getResources().getStringArray(R.array.tabs)[position];
}

private Drawable getIcon(int position) {

return ResourcesCompat.getDrawable(getResources(), icons[position], null);

}

}

适配器

package inncharge.poy.madrigal.innchargev1.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 inncharge.poy.madrigal.innchargev1.R;
import inncharge.poy.madrigal.innchargev1.pojo.DataBean;

/**
* Created by Madrigal on 8/17/2015.
*/
public class DataBeanAdapter extends RecyclerView.Adapter<DataBeanAdapter.ViewHolder>{
private List<DataBean> items;
private int itemLayout;

public DataBeanAdapter(List<DataBean> items, int itemLayout){
this.items = items;
this.itemLayout = itemLayout;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(itemLayout, parent, false);
return new ViewHolder(v);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
DataBean item = items.get(position);
holder.name.setText(item.getName());
holder.card.setText(item.getCard());
//All the thing you gonna show in the item
}

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

public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView name;
public TextView card;

public ViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name);
card = (TextView) itemView.findViewById(R.id.card);
}
}
}

从操作评论中编辑添加的 logcat

08-17 17:17:26.299 1333-1333/? E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.NullPointerException at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java‌​:224) at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.ja‌​va:164) at inncharge.poy.madrigal.innchargev1.activities.DatabaseHelper.getAllData(Database‌​Helper.java:61) at inncharge.poy.madrigal.innchargev1.fragments.FragmentMeal.onCreateView(FragmentM‌​eal.java:82)

java:82 error is this. mRecyclerView.setAdapter(new DataBeanAdapter(helper.getAllData(), R.layout.item));

lang java:61 erro is this. public List getAllData() { db = this.getWritableDatabase();

请注意:
当我在没有 viewpager 的情况下尝试回收器 View 时,它工作得很好,但是当我尝试将其添加到我的 View 寻呼机时。出现此错误。

最佳答案

movetonext是将光标移动到下一行,isafterlast会判断是否有下一行。另外,将光标移动到第一个条目。

http://developer.android.com/reference/android/database/Cursor.html

并尝试获取列索引处的值作为列索引。

更改您的列表和适配器,如下所示。

在你的 oncreate 中。

List items = helper.getAllData();
ArrayAdapter adapter = new ArrayAdapter(getActivity(), R.layout.item,
R.id.textList, items);

在你的助手类中。

public List getAllData()
List list = new ArrayList();

更改 while 循环,如下所示。

cursor.moveToFirst();

while (!cursor.isAfterLast()) {
// int index = cursor.getColumnIndex(UID);
// int index2 = cursor.getColumnIndex(NAME);
// int index3 = cursor.getColumnIndex(CARD);
// int index4 = cursor.getColumnIndex(CODE);
int id = cursor.getInt(0);
String name = cursor.getString(1);
String card = cursor.getString(2);
String code = cursor.getString(3);
DataBean bean = new DataBean();
bean.setId(id);
bean.setName(name);
bean.setCard(card);
bean.setCode(code);
list.add(bean);
cursor.moveToNext();
}

我还建议在循环中分解您的实现,模块化,就目前而言,其中发生了很多事情。

反馈后进行编辑:

我看不到您在哪里使用ViewPagerAdapter。这可能是您的空指针异常。

我建议你看看这里:
http://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter.html .

如果这没有帮助,请回复我。

关于java - viewPager 上的 RecyclerView 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32049601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com