Ashwani Prasad Ashwani Prasad - 3 months ago 11
Android Question

i am getting an error while calling a method and storing its object

I'm trying to create an activity where i can select a particular row from the ListView which is being retrived from the database so that i can manipulate it but when I'm selecting that row my application unfortunately stops.

Here I am getting a android.content.res.Resources$NotFoundException: String resource ID #0x0
While trying to select a row from the database i'm getting an runtime exception.

this is my databasehelper class

databasehelper.java

package com.example.ashwanislaptop.expensemanager;

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;
public class DatabaseHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME="Transaction.db";
public static final String TABLE_NAME="transaction_table";
public static final String COL_1="ID";
public static final String COL_2="PRICE";
public static final String COL_3="DATE";
public static final String COL_4="TIME";
public static final String COL_5="DESCRIPTION";
public static final String COL_6="CATEGORY";
public static final String COL_7="PAYMENT_MODE";
private static final String TAG="DatabaseHelper";


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

}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS "+TABLE_NAME+"(ID INTEGER PRIMARY KEY AUTOINCREMENT,PRICE INTEGER NOT NULL,DATE TEXT NOT NULL,TIME TEXT NOT NULL,DESCRIPTION TEXT NOT NULL,CATEGORY TEXT NOT NULL,PAYMENT_MODE TEXT NOT NULL);");


}

@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS transaction_table");
onCreate(db);
}



void InsertData(Transaction t){
SQLiteDatabase db=getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(COL_2,t.getPrice());
cv.put(COL_3,t.getDate());
cv.put(COL_4,t.getTime());
cv.put(COL_5,t.getDescription());
cv.put(COL_6,t.getCategory());
cv.put(COL_7,t.getPayment_mode());
db.insert(TABLE_NAME,null,cv);
db.close();
}
public ArrayList<Transaction> getAllTransaction() {

SQLiteDatabase db = getReadableDatabase();
Log.i(TAG, "getAllTransaction: getting Transaction Record");
Cursor c = db.rawQuery("Select * From transaction_table", null);
if (c.moveToFirst()) {
ArrayList<Transaction> list = new ArrayList<>();
do {
Transaction t = new Transaction(c.getString(1),c.getString(2), c.getString(5));
list.add(t);
Log.i(TAG, "GetAllTranscRecords: one record retrieved");

}while (c.moveToNext());
return list;
}
else
{
Log.i(TAG,"No record found");
db.close();
return null;
}
}
[getting an runtime error while calling it from the Transaction_Detail.java]


***Transaction getAllTransactionssecond(int idd) {
Log.i(TAG, "getAllTransactionssecond: "+idd);
SQLiteDatabase db = getReadableDatabase();
Log.i(TAG, "getAllTransaction: getting Transaction Record of id "+idd);
Cursor c = db.rawQuery("Select * From transaction_table where id="+idd+";", null);


[The Execution crashes exactly at this point how do i send the data to the Transaction_details so that i can print the data in the textviews]
Transaction t = new Transaction(c.getString(1),c.getString(2),c.getString(3),c.getString(4),c.getString(5),c.getString(6));
Log.i(TAG, "GetAllTranscRecords: one record retrieved");
// Log.i(TAG,"No record found");
db.close();
return t;
}***

public ArrayList<Transaction> getAllTransactioncategorywise(){

SQLiteDatabase db=getReadableDatabase();
Log.i(TAG,"getAllTransaction: getting Transaction Record");
Cursor c=db.rawQuery("Select * From transaction_table order by "+COL_6+";",null);
if(c.moveToFirst()){
ArrayList<Transaction> list=new ArrayList<>();
do{
Transaction t=new Transaction(c.getString(1),c.getString(2),c.getString(5));
list.add(t);
Log.i(TAG,"GetAllTranscRecords: one record retrieved");

}while (c.moveToNext());
return list;
}
else
{
Log.i(TAG,"No record found");
db.close();
return null;
}
}
public ArrayList<Transaction> getAllTransactiondatewise(){

SQLiteDatabase db=getReadableDatabase();
Log.i(TAG,"getAllTransaction: getting Transaction Record");
Cursor c=db.rawQuery("Select * From transaction_table order by "+COL_3+" desc",null);
if(c.moveToFirst()){
ArrayList<Transaction> list=new ArrayList<>();
do{
Transaction t=new Transaction(c.getString(1),c.getString(2),c.getString(5));
list.add(t);
Log.i(TAG,"GetAllTranscRecords: one record retrieved");

}while (c.moveToNext());
return list;
}
else
{
Log.i(TAG,"No record found");
db.close();
return null;
}
}

}


[This is my Transaction Details Class]

transaction detail.java
package com.example.ashwanislaptop.expensemanager;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import java.util.ArrayList;

public class Transaction_Details extends AppCompatActivity {
private TextView txtid,txtprice,txtdate,txttime,txtcateg,txtpayment,txtdecription;
private DatabaseHelper ta;
public static final String TAG="Trasaction detail";
public int sentid;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_transaction__details);
Log.i(TAG, "onCreate: before intent");
Intent i1=getIntent();
sentid=i1.getIntExtra("STRING",0);
Log.i(TAG, "onCreate: after intent "+sentid);

txtid=(TextView)findViewById(R.id.txtid);
txtdate=(TextView)findViewById(R.id.txtdate);
txttime=(TextView)findViewById(R.id.txttime);
txtcateg=(TextView)findViewById(R.id.txtcateg);
txtpayment=(TextView)findViewById(R.id.txtpayment);
txtdecription=(TextView)findViewById(R.id.txtdescription);
ta=new DatabaseHelper(this);


[ I'm able to retrieve the data]

**Transaction details=ta.getAllTransactionssecond(sentid);**



txtid.setText(details.getId());
txtdate.setText(details.getDate());
txttime.setText(details.getTime());
txtcateg.setText(details.getCategory());
txtprice.setText(details.getPrice());
txtpayment.setText(details.getPayment_mode());
txtdecription.setText(details.getDescription());
}
}

Transaction.java
package com.example.ashwanislaptop.expensemanager;


public class Transaction {
private int id;
private String price,date,time,description,category,payment_mode;

public Transaction() {
}

public Transaction(int id, String price, String date, String time, String description, String category, String payment_mode) {
this.id = id;
this.price = price;
this.date = date;
this.time = time;
this.description = description;
this.category = category;
this.payment_mode = payment_mode;
}

public Transaction(String price, String date, String time, String description, String category, String payment_mode) {
this.price = price;
this.date = date;
this.time = time;
this.description = description;
this.category = category;
this.payment_mode = payment_mode;
}

public Transaction(String price, String date, String category) {
this.category = category;
this.date = date;
this.price = price;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getPrice() {
return price;
}

public void setPrice(String price) {
this.price = price;
}

public String getDate() {
return date;
}

public void setDate(String date) {
this.date = date;
}

public String getTime() {
return time;
}

public void setTime(String time) {
this.time = time;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getCategory() {
return category;
}

public void setCategory(String category) {
this.category = category;
}

public String getPayment_mode() {
return payment_mode;
}

public void setPayment_mode(String payment_mode) {
this.payment_mode = payment_mode;
}
}


[This is my AllTransaction Class]

AllTansactionFragment.java
package com.example.ashwanislaptop.expensemanager;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
* Created by Welcome on 06-Aug-16.
*/
public class AllTransactionFragment extends Fragment {
private static final String TAG="AllTransactionFragment";
private ListView listView;
private DatabaseHelper ta=null;
//private ArrayAdapter<String> aa=null;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View vu=inflater.inflate(R.layout.alltransactionlistview,container,false);
listView=(ListView)vu.findViewById(R.id.lvtransaction);
ta=new DatabaseHelper(getActivity());
Log.i(TAG, "onCreateView:"+ta);
ArrayList<Transaction> list=ta.getAllTransactiondatewise();

Collections.sort(list, new Comparator<Transaction>() {
@Override
public int compare(Transaction t1, Transaction t2) {
SimpleDateFormat dateFormat=new SimpleDateFormat("dd-mm-yyyy");
if(t1.getDate()==null || t2.getDate()==null)
return 0;
return t1.getDate().compareTo(t2.getDate());
}
});

TransactionListAdapter aa=new TransactionListAdapter(getActivity(),list);
listView.setAdapter(aa);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Log.i(TAG, " onItemClick: ");
Toast.makeText(getActivity(),"Clicked on:"+i,Toast.LENGTH_SHORT).show();
Intent ii=new Intent(getActivity(),Transaction_Details.class);
ii.putExtra("STRING",i);
Log.i(TAG, "onItemClick: after");
startActivity(ii);
}
});

return vu;
}
}

Answer

Initialize your DatabaseHelper. It is null.

Comments