This question already has an answer here:
I'm try to get data from a SQLite
database into a listView. The application runs fine but when I open the New_Recipe
activity I get a logcat error reading : `
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
It say that the error is in the productHolder.tx_items.setText(recipeGetterandSetter.getItems().toString());
line of the RecipeAdapter
class.
Any solutions?
DatabaseHelper class:
public class DatabaseHelper extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 4;
public static final String DATABASE_NAME = "Recipes.db";
public static final String CREATE_QUERRY = "create table " + RecipeContract.RecipeEntry.TABLE_NAME +"( NAME text, ITEMS text, STEPS text)";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Log.d("Recipe Database", "Database should be made!");
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_QUERRY);
Log.d("Recipe Database", "Table should be made!");
}
public void addnameandsteps(SQLiteDatabase db,String name,String steps){
ContentValues contentValues = new ContentValues();
contentValues.put(RecipeContract.RecipeEntry.COL1,name);
contentValues.put(RecipeContract.RecipeEntry.COL3, steps);
db.insert(RecipeContract.RecipeEntry.TABLE_NAME, null, contentValues);
Log.d("Recipe Database", "Name and steps data inserted!");
}
public void additems(SQLiteDatabase db,String items) {
ContentValues contentValues = new ContentValues();
contentValues.put(RecipeContract.RecipeEntry.COL2,items);
db.insert(RecipeContract.RecipeEntry.TABLE_NAME, null, contentValues);
Log.d("Recipe Database", "Item row inserted!");
}
public Cursor getItems(SQLiteDatabase db) {
String[] projections = {RecipeContract.RecipeEntry.COL2};
Cursor cursor = db.query(RecipeContract.RecipeEntry.TABLE_NAME,projections,
null,null,null,null,null);
return cursor;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS" + RecipeContract.RecipeEntry.TABLE_NAME);
Log.d("Database", "Table exists");
onCreate(db);
}
}
RecipeBackgroundTask class:
public class RecipeBackgroudTask extends AsyncTask<String,RecipeGetterandSetter,String> {
Context ctx;
RecipeAdapter recipeAdapter;
Activity activity;
ListView listView;
RecipeBackgroudTask(Context ctx)
{
this.ctx = ctx;
activity = (Activity)ctx;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
String method = params [0];
DatabaseHelper databaseHelper = new DatabaseHelper(ctx);
if (method.equals("add_name/steps"))
{
String name = params [1];
String steps = params [2];
SQLiteDatabase db = databaseHelper.getWritableDatabase();
databaseHelper.addnameandsteps(db, name, steps);
}
if (method.equals("add_items"))
{
String items = params [1];
SQLiteDatabase db = databaseHelper.getWritableDatabase();
databaseHelper.additems(db, items);
} else if(method.equals("get_items"))
{
listView = (ListView) activity.findViewById(R.id.listView);
SQLiteDatabase db = databaseHelper.getReadableDatabase();
Cursor cursor = databaseHelper.getItems(db);
recipeAdapter = new RecipeAdapter(ctx,R.layout.display_recipe_row);
String items;
while (cursor.moveToNext())
{
items = cursor.getString(cursor.getColumnIndex(RecipeContract.RecipeEntry.COL2));
RecipeGetterandSetter getterandSetter = new RecipeGetterandSetter(items);
publishProgress(getterandSetter);
}
return "get_items";
}
return null;
}
@Override
protected void onProgressUpdate(RecipeGetterandSetter... values) {
recipeAdapter.add(values[0]);
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
if (result.equals("get_items"))
{
listView.setAdapter(recipeAdapter);
}
}
}
RecipeGetterandSetter class:
public class RecipeGetterandSetter {
private String items;
public RecipeGetterandSetter(String items)
{
this.setItems(items);
}
public String getItems() {
return items;
}
public void setItems(String items) {
this.items = items;
}
}
RecipeAdapter class:
public class RecipeAdapter extends ArrayAdapter {
List list = new ArrayList();
public RecipeAdapter(Context context, int resource) {
super(context, resource);
}
public void add(RecipeGetterandSetter object) {
list.add(object);
super.add(object);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ProductHolder productHolder;
if (row == null)
{
LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.activity_new__recipe,parent,false);
productHolder = new ProductHolder();
productHolder.tx_items = (TextView) row.findViewById(R.id.tx_items);
row.setTag(productHolder);
} else {
productHolder = (ProductHolder) row.getTag();
}
RecipeGetterandSetter recipeGetterandSetter = (RecipeGetterandSetter)getItem(position);
productHolder.tx_items.setText(recipeGetterandSetter.getItems().toString());
return row;
}
static class ProductHolder
{
TextView tx_items;
}
}
I'm not sure if this has anything to do with the error but display_recipe_row layout:
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent" android:layout_height="75dp"
android:background="#2787A4">
<TextView
android:layout_width="70dp"
android:layout_height="match_parent"
android:id="@+id/tx_items"
android:layout_alignParentLeft="true"
android:text="12A"
android:gravity="center"
android:textAppearance="?android:textAppearanceLarge"/>
Aucun commentaire:
Enregistrer un commentaire