Fatal Exception when viewing/ writing database SQLite? - java

I get an error when I run my emulator, and try to start reading or viewing the database..
Please see my logcat at the bottum of this page!
I have a Database activity, a SQLView activity, and an SQLite activity.
The database activity is to run the database, the SQLView activity to view the database, and the last one, my SQLite activity is to wrie to my database.
I use SQLiteAssetHelper, and my .rar file is in my assets/databases/Voedsel.rar .
This are my activities:
My database activity:
package com.jacob.eindproject;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import java.sql.*;
import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;
public class Database extends SQLiteAssetHelper {
public static final String KEY_PRODUCT = "Product";
public static final String KEY_EENHEID = "Eenheid";
public static final String KEY_KCAL = "Kcal";
private static final String DATABASE_NAME = "Voedsel";
private static final String DATABASE_TABLE = "Voeding";
private static final int DATABASE_VERSION = 1;
private DbHelper ourHelper;
private Context Context;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteAssetHelper{
public DbHelper(Context context) {
super(context, DATABASE_NAME, context.getExternalFilesDir(null).getAbsolutePath(), null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
}
#Override
public void onUpgrade(SQLiteDatabase Voedsel, int oldVersion, int newVersion) {
Voedsel.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(Voedsel);
}
public Database(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public Database open() throws SQLException{
ourHelper = new DbHelper(Context);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close() {
ourHelper.close();
}
public long createEntry(String product, String kcal, String eenheid) {
ContentValues cv = new ContentValues();
cv.put(KEY_PRODUCT, product);
cv.put(KEY_EENHEID, eenheid);
cv.put(KEY_KCAL, kcal);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public String getData() {
// TODO Auto-generated method stub
String[] columns = new String[]{ KEY_PRODUCT, KEY_EENHEID, KEY_KCAL};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";
int iProduct = c.getColumnIndex(KEY_PRODUCT);
int iEenheid = c.getColumnIndex(KEY_EENHEID);
int iKcal = c.getColumnIndex(KEY_KCAL);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + c.getString(iProduct) + " " + c.getString(iEenheid) + " " + c.getString(iKcal) + "\n";
}
return result;
}
public void close(Database database) {
// TODO Auto-generated method stub
}
}
My SQLite activity:
public class SQLite extends Activity implements View.OnClickListener {
Button sqlUpdate, sqlView;
EditText sqlVoeding, sqlKcal, sqlEenheid;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sqllite);
sqlUpdate = (Button) findViewById(R.id.bSQLUpdate);
sqlVoeding = (EditText) findViewById(R.id.etSQLVoeding);
sqlEenheid = (EditText) findViewById(R.id.etSQLEenheid);
sqlKcal = (EditText) findViewById(R.id.etSQLKcal);
sqlView = (Button) findViewById(R.id.bSQLopenView);
sqlView.setOnClickListener((android.view.View.OnClickListener) this);
sqlUpdate.setOnClickListener((android.view.View.OnClickListener) this);
}
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.bSQLUpdate:
boolean didItWork = true;
try{
String voeding = sqlVoeding.getText().toString();
String Kcal = sqlKcal.getText().toString();
String eenheid = sqlEenheid.getText().toString();
Database entry = new Database(SQLite.this);
entry.open();
entry.createEntry(voeding, Kcal, eenheid);
entry.close();
}catch (Exception e ){
didItWork = false;
}finally{
if (didItWork){
Dialog d = new Dialog(this);
d.setTitle("Heak Yeay");
TextView tv = new TextView(this);
tv.setText("Succes");
d.setContentView(tv);
d.show();
}
}
break;
case R.id.bSQLopenView:
Intent i = new Intent(this, SQLView.class);
startActivity(i);
}
}
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}
}
And the SQLView activity:
public class SQLView extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sqlview);
TextView tv = (TextView) findViewById(R.id.tvSQLinfo);
Database info = new Database(this);
info.open();
String data = info.getData();
info.close();
tv.setText(data);
}
}
This is my logcat error when I click the SQLView activity:
12-16 14:28:26.883: E/AndroidRuntime(1170): FATAL EXCEPTION: main
12-16 14:28:26.883: E/AndroidRuntime(1170): java.lang.NoClassDefFoundError: com.jacob.eindproject.Database
12-16 14:28:26.883: E/AndroidRuntime(1170): at com.jacob.eindproject.SQLView.onCreate(SQLView.java:14)
12-16 14:28:26.883: E/AndroidRuntime(1170): at android.app.Activity.performCreate(Activity.java:5133)
12-16 14:28:26.883: E/AndroidRuntime(1170): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-16 14:28:26.883: E/AndroidRuntime(1170): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
12-16 14:28:26.883: E/AndroidRuntime(1170): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-16 14:28:26.883: E/AndroidRuntime(1170): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-16 14:28:26.883: E/AndroidRuntime(1170): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-16 14:28:26.883: E/AndroidRuntime(1170): at android.os.Handler.dispatchMessage(Handler.java:99)
12-16 14:28:26.883: E/AndroidRuntime(1170): at android.os.Looper.loop(Looper.java:137)
12-16 14:28:26.883: E/AndroidRuntime(1170): at android.app.ActivityThread.main(ActivityThread.java:5103)
12-16 14:28:26.883: E/AndroidRuntime(1170): at java.lang.reflect.Method.invokeNative(Native Method)
12-16 14:28:26.883: E/AndroidRuntime(1170): at java.lang.reflect.Method.invoke(Method.java:525)
12-16 14:28:26.883: E/AndroidRuntime(1170): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-16 14:28:26.883: E/AndroidRuntime(1170): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-16 14:28:26.883: E/AndroidRuntime(1170): at dalvik.system.NativeStart.main(Native Method)
And this is my error when I click my 'write' activity:
12-16 14:27:35.913: E/AndroidRuntime(999): FATAL EXCEPTION: main
12-16 14:27:35.913: E/AndroidRuntime(999): java.lang.NoClassDefFoundError: com.jacob.eindproject.Database
12-16 14:27:35.913: E/AndroidRuntime(999): at com.jacob.eindproject.SQLite.onClick(SQLite.java:45)
12-16 14:27:35.913: E/AndroidRuntime(999): at android.view.View.performClick(View.java:4240)
12-16 14:27:35.913: E/AndroidRuntime(999): at android.view.View$PerformClick.run(View.java:17721)
12-16 14:27:35.913: E/AndroidRuntime(999): at android.os.Handler.handleCallback(Handler.java:730)
12-16 14:27:35.913: E/AndroidRuntime(999): at android.os.Handler.dispatchMessage(Handler.java:92)
12-16 14:27:35.913: E/AndroidRuntime(999): at android.os.Looper.loop(Looper.java:137)
12-16 14:27:35.913: E/AndroidRuntime(999): at android.app.ActivityThread.main(ActivityThread.java:5103)
12-16 14:27:35.913: E/AndroidRuntime(999): at java.lang.reflect.Method.invokeNative(Native Method)
12-16 14:27:35.913: E/AndroidRuntime(999): at java.lang.reflect.Method.invoke(Method.java:525)
12-16 14:27:35.913: E/AndroidRuntime(999): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-16 14:27:35.913: E/AndroidRuntime(999): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-16 14:27:35.913: E/AndroidRuntime(999): at dalvik.system.NativeStart.main(Native Method)
Hopefully someone can help me fix my error. Thank you all in advance for taking your time.
Jacob.

I don't think you've added the SQLiteAssetHelper library to your project correctly.
You must ensure that the build path for your project is correct, and all referenced libraries are selected.
Right click on your project and select 'Properties'
Click on 'Android'
Make sure your SQLiteAssetHelper library is selected

Related

how to send data from one activity to another in android?

I am trying to send my web service response from one activity to another .when I move one activity to another activity without response it work fine .but when I send response it give me error .could you please help me removing this exception ..
Actually I am sending like this:I think problem in this line i.putExtra("test", data);
I do like this..
10-25 21:13:47.619: E/AndroidRuntime(962): FATAL EXCEPTION: main
10-25 21:13:47.619: E/AndroidRuntime(962): java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.firstgroup.applicationload/com.firstgroup.ui.screens.Departuredashboardscreen}: java.lang.NullPointerException
10-25 21:13:47.619: E/AndroidRuntime(962): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
10-25 21:13:47.619: E/AndroidRuntime(962): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-25 21:13:47.619: E/AndroidRuntime(962): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-25 21:13:47.619: E/AndroidRuntime(962): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-25 21:13:47.619: E/AndroidRuntime(962): at android.os.Handler.dispatchMessage(Handler.java:99)
10-25 21:13:47.619: E/AndroidRuntime(962): at android.os.Looper.loop(Looper.java:137)
10-25 21:13:47.619: E/AndroidRuntime(962): at android.app.ActivityThread.main(ActivityThread.java:5041)
10-25 21:13:47.619: E/AndroidRuntime(962): at java.lang.reflect.Method.invokeNative(Native Method)
10-25 21:13:47.619: E/AndroidRuntime(962): at java.lang.reflect.Method.invoke(Method.java:511)
10-25 21:13:47.619: E/AndroidRuntime(962): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-25 21:13:47.619: E/AndroidRuntime(962): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-25 21:13:47.619: E/AndroidRuntime(962): at dalvik.system.NativeStart.main(Native Method)
10-25 21:13:47.619: E/AndroidRuntime(962): Caused by: java.lang.NullPointerException
10-25 21:13:47.619: E/AndroidRuntime(962): at android.os.Parcel.readListInternal(Parcel.java:2237)
10-25 21:13:47.619: E/AndroidRuntime(962): at android.os.Parcel.readList(Parcel.java:1531)
10-25 21:13:47.619: E/AndroidRuntime(962): at com.firstgroup.dto.deparaturedaseboarddto.<init>(deparaturedaseboarddto.java:209)
10-25 21:13:47.619: E/AndroidRuntime(962): at com.firstgroup.dto.deparaturedaseboarddto.<init>(deparaturedaseboarddto.java:193)
10-25 21:13:47.619: E/AndroidRuntime(962): at com.firstgroup.dto.deparaturedaseboarddto$1.createFromParcel(deparaturedaseboarddto.java:185)
10-25 21:13:47.619: E/AndroidRuntime(962): at com.firstgroup.dto.deparaturedaseboarddto$1.createFromParcel(deparaturedaseboarddto.java:1)
10-25 21:13:47.619: E/AndroidRuntime(962): at android.os.Parcel.readParcelable(Parcel.java:2103)
10-25 21:13:47.619: E/AndroidRuntime(962): at android.os.Parcel.readValue(Parcel.java:1965)
10-25 21:13:47.619: E/AndroidRuntime(962): at android.os.Parcel.readMapInternal(Parcel.java:2226)
10-25 21:13:47.619: E/AndroidRuntime(962): at android.os.Bundle.unparcel(Bundle.java:223)
10-25 21:13:47.619: E/AndroidRuntime(962): at android.os.Bundle.getParcelable(Bundle.java:1173)
10-25 21:13:47.619: E/AndroidRuntime(962): at android.content.Intent.getParcelableExtra(Intent.java:4330)
10-25 21:13:47.619: E/AndroidRuntime(962): at com.firstgroup.ui.screens.Departuredashboardscreen.onCreate(Departuredashboardscreen.java:21)
10-25 21:13:47.619: E/AndroidRuntime(962): at android.app.Activity.performCreate(Activity.java:5104)
10-25 21:13:47.619: E/AndroidRuntime(962): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-25 21:13:47.619: E/AndroidRuntime(962): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
10-25 21:13:47.619: E/AndroidRuntime(962): ... 11 more
#Override
public void getWebserviceResponse(String result) {
// TODO Auto-generated method stub
//JSONObject js=new Jso(result);
deparaturedaseboarddto data = new Gson().fromJson(result, deparaturedaseboarddto.class);
/*listView=(ListView)findViewById(R.id.listView1);
adapter = new CustomListAdapter(this, data.getData());
listView.setAdapter(adapter);*/
Intent i = new Intent(Appliacationload.this,Departuredashboardscreen.class);
i.putExtra("test", data);
startActivity(i);
finish();
Log.d("----", "========");
}
Second Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.departure_dashboard);
Intent i = getIntent();
deparaturedaseboarddto myParcelableObject = (deparaturedaseboarddto) i.getParcelableExtra("test");
Log.d("===", "hhhh");
}
**Model class :**
public class deparaturedaseboarddto implements Parcelable{
ArrayList<deparaturedaseboarddto> data;
public ArrayList<deparaturedaseboarddto> getData() {
return data;
}
public void setData(ArrayList<deparaturedaseboarddto> data) {
this.data = data;
}
#SerializedName("alertsId")
int alertsId;
#SerializedName("destExpArrival")
String destExpArrival;
#SerializedName("destSchArrival")
String destSchArrival;
#SerializedName("expDepart")
String expDepart;
#SerializedName("filteredStation")
String filteredStation;
#SerializedName("platformNo")
String platformNo;
#SerializedName("rid")
String rid;
#SerializedName("schDepart")
String schDepart;
#SerializedName("toc")
String toc;
#SerializedName("toExpArrival")
String toExpArrival;
#SerializedName("toSchArrival")
String toSchArrival;
#SerializedName("trainID")
String trainID;
#SerializedName("trainLastReportedAt")
String trainLastReportedAt;
#SerializedName("destinationStation")
DestinationStation destinationStation;
public deparaturedaseboarddto(String trainID,String toc,String trainLastReportedAt, String platformNo, String schDepart, String expDepart, int alertsId, String rid, String destSchArrival, String filteredStation, String destExpArrival, String toSchArrival, String toExpArrival,DestinationStation destinationStation){
super();
this.trainID=trainID;
this.toc=toc;
this.trainLastReportedAt=trainLastReportedAt;
this.platformNo=platformNo;
this.schDepart=schDepart;
this.expDepart=expDepart;
this.alertsId=alertsId;
this.destinationStation=destinationStation;
this.toExpArrival=toExpArrival;
this.toSchArrival=toSchArrival;
this.destExpArrival=destExpArrival;
this.filteredStation=filteredStation;
this.destSchArrival=destSchArrival;
this.rid=rid;
}
public DestinationStation getDestinationStation() {
return destinationStation;
}
public void setDestinationStation(DestinationStation destinationStation) {
this.destinationStation = destinationStation;
}
public int getAlertsId() {
return alertsId;
}
public void setAlertsId(int alertsId) {
this.alertsId = alertsId;
}
public String getDestExpArrival() {
return destExpArrival;
}
public void setDestExpArrival(String destExpArrival) {
this.destExpArrival = destExpArrival;
}
public String getDestSchArrival() {
return destSchArrival;
}
public void setDestSchArrival(String destSchArrival) {
this.destSchArrival = destSchArrival;
}
public String getExpDepart() {
return expDepart;
}
public void setExpDepart(String expDepart) {
this.expDepart = expDepart;
}
public String getFilteredStation() {
return filteredStation;
}
public void setFilteredStation(String filteredStation) {
this.filteredStation = filteredStation;
}
public String getPlatformNo() {
return platformNo;
}
public void setPlatformNo(String platformNo) {
this.platformNo = platformNo;
}
public String getRid() {
return rid;
}
public void setRid(String rid) {
this.rid = rid;
}
public String getSchDepart() {
return schDepart;
}
public void setSchDepart(String schDepart) {
this.schDepart = schDepart;
}
public String getToc() {
return toc;
}
public void setToc(String toc) {
this.toc = toc;
}
public String getToExpArrival() {
return toExpArrival;
}
public void setToExpArrival(String toExpArrival) {
this.toExpArrival = toExpArrival;
}
public String getToSchArrival() {
return toSchArrival;
}
public void setToSchArrival(String toSchArrival) {
this.toSchArrival = toSchArrival;
}
public String getTrainID() {
return trainID;
}
public void setTrainID(String trainID) {
this.trainID = trainID;
}
public String getTrainLastReportedAt() {
return trainLastReportedAt;
}
public void setTrainLastReportedAt(String trainLastReportedAt) {
this.trainLastReportedAt = trainLastReportedAt;
}
#Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(alertsId);
dest.writeString(destExpArrival);
dest.writeString(destSchArrival);
dest.writeString(expDepart);
dest.writeString(filteredStation);
dest.writeString(platformNo);
dest.writeString(rid);
dest.writeString(schDepart);
dest.writeString(toc);
dest.writeString(toExpArrival);
dest.writeString(toSchArrival);
dest.writeString(trainID);
dest.writeString(trainLastReportedAt);
dest.writeParcelable(this.destinationStation, flags);
dest.writeList(data);
}
public static final Parcelable.Creator<deparaturedaseboarddto> CREATOR = new Parcelable.Creator<deparaturedaseboarddto>() {
public deparaturedaseboarddto createFromParcel(Parcel in) {
return new deparaturedaseboarddto(in);
}
public deparaturedaseboarddto[] newArray(int size) {
return new deparaturedaseboarddto[size];
}
};
private deparaturedaseboarddto(Parcel in) {
this.alertsId=in.readInt();
this.destExpArrival=in.readString();
this.destSchArrival=in.readString();
this.expDepart=in.readString();
this.filteredStation=in.readString();
this.platformNo=in.readString();
this.rid=in.readString();
this.schDepart=in.readString();
this.toc=in.readString();
this.toExpArrival=in.readString();
this.toSchArrival=in.readString();
this.trainID=in.readString();
this.trainLastReportedAt=in.readString();
this.destinationStation = in.readParcelable(DestinationStation.class.getClassLoader());
in.readList(data,deparaturedaseboarddto.class.getClassLoader());
}
}
package com.firstgroup.dto;
import android.os.Parcel;
import android.os.Parcelable;
import com.google.gson.annotations.SerializedName;
public class DestinationStation implements Parcelable {
#SerializedName("crsCode")
String crsCode;
#SerializedName("stationName")
String stationName;
public DestinationStation(String crsCode, String stationName) {
// TODO Auto-generated constructor stub
super();
this.crsCode=crsCode;
this.stationName=stationName;
}
public String getCrsCode() {
return crsCode;
}
public void setCrsCode(String crsCode) {
this.crsCode = crsCode;
}
public String getStationName() {
return stationName;
}
public void setStationName(String stationName) {
this.stationName = stationName;
}
#Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(crsCode);
dest.writeString(stationName);
}
public static final Parcelable.Creator<DestinationStation> CREATOR = new Parcelable.Creator<DestinationStation>() {
public DestinationStation createFromParcel(Parcel in) {
return new DestinationStation(in);
}
public DestinationStation[] newArray(int size) {
return new DestinationStation[size];
}
};
private DestinationStation(Parcel in) {
this.crsCode=in.readString();
this.stationName=in.readString();
}
}
Updated one..
#Override
public void getWebserviceResponse(String result) {
// TODO Auto-generated method stub
//JSONObject js=new Jso(result);
deparaturedaseboarddto data = new Gson().fromJson(result, deparaturedaseboarddto.class);
/*listView=(ListView)findViewById(R.id.listView1);
adapter = new CustomListAdapter(this, data.getData());
listView.setAdapter(adapter);*/
Intent i = new Intent(Appliacationload.this,Departuredashboardscreen.class);
EventBus.getDefault().postSticky(data);
//i.putExtra("test", (Parcelable)data);
startActivity(i);
finish();
Log.d("----", "========");
}
Next Activity:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.departure_dashboard);
Intent i = getIntent();
deparaturedaseboarddto myParcelableObject = (deparaturedaseboarddto) EventBus.getDefault().getStickyEvent(deparaturedaseboarddto.class);
Log.d("===", "hhhh");
}
Exception:
10-26 00:56:36.052: E/AndroidRuntime(806): FATAL EXCEPTION: AsyncTask #3
10-26 00:56:36.052: E/AndroidRuntime(806): java.lang.RuntimeException: An error occured while executing doInBackground()
10-26 00:56:36.052: E/AndroidRuntime(806): at android.os.AsyncTask$3.done(AsyncTask.java:299)
10-26 00:56:36.052: E/AndroidRuntime(806): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
10-26 00:56:36.052: E/AndroidRuntime(806): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
10-26 00:56:36.052: E/AndroidRuntime(806): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
10-26 00:56:36.052: E/AndroidRuntime(806): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-26 00:56:36.052: E/AndroidRuntime(806): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-26 00:56:36.052: E/AndroidRuntime(806): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-26 00:56:36.052: E/AndroidRuntime(806): at java.lang.Thread.run(Thread.java:856)
10-26 00:56:36.052: E/AndroidRuntime(806): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
10-26 00:56:36.052: E/AndroidRuntime(806): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4746)
10-26 00:56:36.052: E/AndroidRuntime(806): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:823)
10-26 00:56:36.052: E/AndroidRuntime(806): at android.view.View.requestLayout(View.java:15473)
10-26 00:56:36.052: E/AndroidRuntime(806): at android.view.View.setFlags(View.java:8444)
10-26 00:56:36.052: E/AndroidRuntime(806): at android.view.View.setVisibility(View.java:5716)
10-26 00:56:36.052: E/AndroidRuntime(806): at android.app.Dialog.hide(Dialog.java:294)
10-26 00:56:36.052: E/AndroidRuntime(806): at com.firstgroup.webservice.RequestTask.doInBackground(RequestTask.java:66)
10-26 00:56:36.052: E/AndroidRuntime(806): at com.firstgroup.webservice.RequestTask.doInBackground(RequestTask.java:1)
10-26 00:56:36.052: E/AndroidRuntime(806): at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-26 00:56:36.052: E/AndroidRuntime(806): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
10-26 00:56:36.052: E/AndroidRuntime(806): ... 4 more
10-26 00:56:40.424: E/WindowManager(806): Activity com.firstgroup.applicationload.Appliacationload has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40f6b308 G.E..... R.....ID 0,0-729,324} that was originally added here
10-26 00:56:40.424: E/WindowManager(806): android.view.WindowLeaked: Activity com.firstgroup.applicationload.Appliacationload has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40f6b308 G.E..... R.....ID 0,0-729,324} that was originally added here
10-26 00:56:40.424: E/WindowManager(806): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:354)
10-26 00:56:40.424: E/WindowManager(806): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216)
10-26 00:56:40.424: E/WindowManager(806): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
10-26 00:56:40.424: E/WindowManager(806): at android.app.Dialog.show(Dialog.java:281)
10-26 00:56:40.424: E/WindowManager(806): at com.firstgroup.webservice.RequestTask.onPreExecute(RequestTask.java:36)
10-26 00:56:40.424: E/WindowManager(806): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
10-26 00:56:40.424: E/WindowManager(806): at android.os.AsyncTask.execute(AsyncTask.java:534)
10-26 00:56:40.424: E/WindowManager(806): at com.firstgroup.applicationload.Appliacationload.calldeparutureWebservice(Appliacationload.java:130)
10-26 00:56:40.424: E/WindowManager(806): at com.firstgroup.applicationload.Appliacationload.access$1(Appliacationload.java:126)
10-26 00:56:40.424: E/WindowManager(806): at com.firstgroup.applicationload.Appliacationload$2.onClick(Appliacationload.java:91)
10-26 00:56:40.424: E/WindowManager(806): at android.view.View.performClick(View.java:4204)
10-26 00:56:40.424: E/WindowManager(806): at android.view.View$PerformClick.run(View.java:17355)
10-26 00:56:40.424: E/WindowManager(806): at android.os.Handler.handleCallback(Handler.java:725)
10-26 00:56:40.424: E/WindowManager(806): at android.os.Handler.dispatchMessage(Handler.java:92)
10-26 00:56:40.424: E/WindowManager(806): at android.os.Looper.loop(Looper.java:137)
10-26 00:56:40.424: E/WindowManager(806): at android.app.ActivityThread.main(ActivityThread.java:5041)
10-26 00:56:40.424: E/WindowManager(806): at java.lang.reflect.Method.invokeNative(Native Method)
10-26 00:56:40.424: E/WindowManager(806): at java.lang.reflect.Method.invoke(Method.java:511)
10-26 00:56:40.424: E/WindowManager(806): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-26 00:56:40.424: E/WindowManager(806): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-26 00:56:40.424: E/WindowManager(806): at dalvik.system.NativeStart.main(Native Method)
Should be something like this:
#Override
public void getWebserviceResponse(String result) {
// TODO Auto-generated method stub
//JSONObject js=new Jso(result);
deparaturedaseboarddto data = new Gson().fromJson(result, deparaturedaseboarddto.class);
/*listView=(ListView)findViewById(R.id.listView1);
adapter = new CustomListAdapter(this, data.getData());
listView.setAdapter(adapter);*/
Intent i = new Intent(Appliacationload.this,Departuredashboardscreen.class);
//i.putExtra("test", data);
EventBus.getDefault().postSticky(deparaturedaseboarddto);
startActivity(i);
finish();
Log.d("----", "========");
}
and in Departuredashboardscreen
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.departure_dashboard);
deparaturedaseboarddto myParcelableObject = EventBus.getDefault().getStickyEvent(deparaturedaseboarddto.class);
Log.d("===", "hhhh");
}
Try using getExtra()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.departure_dashboard);
//use getExtra()
Intent i = getIntent().getExtras();
deparaturedaseboarddto myParcelableObject =
(deparaturedaseboarddto) i.getParcelableExtra("test");
Log.d("===", "hhhh");
}

Retrieve SQLite data in edit text from Spinner selected item in Android

I have spinner in Activity and edittext's. When i run the app I'm getting warning of nullPointerException.I dosen't retrieve the SQLite data in edit text in onCreate().Can someone help me.
Here is my code.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.delete_entry);
databaseHelper = new DatabaseHelper(this);
databaseHelper.onOpen(db);
spinner_searchByEmpName = (Spinner)findViewById(R.id.searchByEmpName);
loadSerachBYProject() ;
spinner_searchByEmpName.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
selectedEmployeeName = spinner_searchByEmpName.getSelectedItem().toString().trim();
System.out.println("selectedProjectName " + selectedEmployeeName);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
etEmpName=(EditText)findViewById(R.id.Delete_editText_StaffEmployee_Name);
etDepartment=(EditText)findViewById(R.id.Delete_editText_Department);
etDesignation=(EditText)findViewById(R.id.Delete_editText_Designation);
try {
Cursor cursor = db.rawQuery("SELECT staff_employee_id, staff_emp_name, department, designation FROM employee_details WHERE staff_emp_name = ?",
new String[] { "" + selectedEmployeeName });
etEmpName.setText(cursor.getString(cursor
.getColumnIndex("staff_emp_name")));
etDepartment.setText(cursor.getString(cursor
.getColumnIndex("department")));
etDesignation.setText(cursor.getString(cursor
.getColumnIndex("designation")));
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
private void loadSerachBYProject()
{
DatabaseHelper databaseHelper = new DatabaseHelper(getApplicationContext());
// Spinner Drop down elements
List<String> projectsName = databaseHelper.getAllEmployeeName();
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, projectsName);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner_searchByEmpName.setAdapter(dataAdapter);
}
}
Thanks in Advance.
Here is My Log cat error info
06-26 18:08:09.073: W/System.err(601): java.lang.NullPointerException
06-26 18:08:09.083: W/System.err(601): at com.employee_review.Update_Entry.onCreate(Update_Entry.java:68)
06-26 18:08:09.083: W/System.err(601): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-26 18:08:09.083: W/System.err(601): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-26 18:08:09.083: W/System.err(601): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-26 18:08:09.092: W/System.err(601): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-26 18:08:09.092: W/System.err(601): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-26 18:08:09.103: W/System.err(601): at android.os.Handler.dispatchMessage(Handler.java:99)
06-26 18:08:09.103: W/System.err(601): at android.os.Looper.loop(Looper.java:123)
06-26 18:08:09.113: W/System.err(601): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-26 18:08:09.123: W/System.err(601): at java.lang.reflect.Method.invokeNative(Native Method)
06-26 18:08:09.133: W/System.err(601): at java.lang.reflect.Method.invoke(Method.java:507)
06-26 18:08:09.133: W/System.err(601): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-26 18:08:09.133: W/System.err(601): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-26 18:08:09.133: W/System.err(601): at dalvik.system.NativeStart.main(Native Method)
Try this , i guess
if(cursor!=null){
etEmpName.setText(cursor.getString(cursor
.getColumnIndex("staff_emp_name")));
etDepartment.setText(cursor.getString(cursor
.getColumnIndex("department")));
etDesignation.setText(cursor.getString(cursor
.getColumnIndex("designation")));
}
db is never initialized. It must be initialized in onCreate(), as that's the first method that is run in an Activity. I see this:
databaseHelper = new DatabaseHelper(this);
databaseHelper.onOpen(db);
I'm guessing you really should have this:
databaseHelper = new DatabaseHelper(this);
db=databaseHelper.getReadableDatabase();

Activity closes when trying to asyncronously connect to twitter

I have been trying to program a twitter client for Android (using twitter4j). So far the idea is to have a simple GUI, and if there is not a file with the OAuth token in the SD Card, connect to the Twitter API using AsyncTask, get the URL for the authorization and open the default browser. However, the browser never runs. Depending on the different modifications I have made trying to fix this, either the Activity starts normally but the browser never starts or the Activity crashes. I have come to a point of a a little of frustation and confussion. Can someone point out what's wrong with my code?
public class StatusActivity extends Activity {
private static final String TAG = "StatusActivity";
EditText editText;
Button updateButton;
File oauthfile = null;
public Context context = getApplicationContext();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_status);
Log.d(TAG, "started");
// Find views
editText = (EditText) findViewById(R.id.editText); //
updateButton = (Button) findViewById(R.id.buttonUpdate);
oauthfile = new File("sdcard/auth_file.txt");
//Check if the file with the keys exist
if (oauthfile.exists()==false){
Log.d(TAG, "file not created");
Context context = getApplicationContext();
Toast toast = Toast.makeText(context, "file not created.", Toast.LENGTH_SHORT);
toast.show();
new Authorization(context).execute();
}
}
public void openBrowser (View v){
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
Log.d(TAG, "onclick");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.status, menu);
return true;
}
}
class Authorization extends AsyncTask <String, Integer, String>{
String url = null;
private Context context;
Authorization(Context context) {
this.context = context.getApplicationContext();
}
public void onPreExecute() {
super.onPreExecute();
Toast.makeText(context, "Invoke onPreExecute()", Toast.LENGTH_SHORT).show();
}
#Override
public String doInBackground(String... params) {
ConfigurationBuilder configBuilder = new ConfigurationBuilder();
configBuilder.setDebugEnabled(true)
//I have eliminated the keys from the question :)
.setOAuthConsumerKey("XXXXXXXXXXXXXX")
.setOAuthConsumerSecret("XXXXXXXXXXXXXXX");
Twitter OAuthTwitter = new TwitterFactory(configBuilder.build()).getInstance();
RequestToken requestToken = null;
AccessToken accessToken = null;
do{
try {
requestToken = OAuthTwitter.getOAuthRequestToken();
url = requestToken.getAuthorizationURL();
}
catch (TwitterException ex) {
ex.printStackTrace();
}
}
while (accessToken==null);
return url;
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
Toast.makeText(context, "Opening browser.", Toast.LENGTH_SHORT).show();
Intent browserIntent = new Intent(Intent.ACTION_ALL_APPS, Uri.parse(url));
context.startActivity(browserIntent);
}
}
I know that at least checks if the file for the tokens exists because the toast "file not created" appears, and that the activity is able to run the browser if I press the button. The app has permissions to write in the SD card and use the Internet. Thanks in advance.
Logcat Trace:
03-28 19:02:32.816: E/AndroidRuntime(278): FATAL EXCEPTION: main
03-28 19:02:32.816: E/AndroidRuntime(278): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.versec.pardinus/com.versec.pardinus.StatusActivity}: java.lang.NullPointerException
03-28 19:02:32.816: E/AndroidRuntime(278): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
03-28 19:02:32.816: E/AndroidRuntime(278): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-28 19:02:32.816: E/AndroidRuntime(278): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-28 19:02:32.816: E/AndroidRuntime(278): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-28 19:02:32.816: E/AndroidRuntime(278): at android.os.Handler.dispatchMessage(Handler.java:99)
03-28 19:02:32.816: E/AndroidRuntime(278): at android.os.Looper.loop(Looper.java:123)
03-28 19:02:32.816: E/AndroidRuntime(278): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-28 19:02:32.816: E/AndroidRuntime(278): at java.lang.reflect.Method.invokeNative(Native Method)
03-28 19:02:32.816: E/AndroidRuntime(278): at java.lang.reflect.Method.invoke(Method.java:521)
03-28 19:02:32.816: E/AndroidRuntime(278): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-28 19:02:32.816: E/AndroidRuntime(278): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-28 19:02:32.816: E/AndroidRuntime(278): at dalvik.system.NativeStart.main(Native Method)
03-28 19:02:32.816: E/AndroidRuntime(278): Caused by: java.lang.NullPointerException
03-28 19:02:32.816: E/AndroidRuntime(278): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:100)
03-28 19:02:32.816: E/AndroidRuntime(278): at com.versec.pardinus.StatusActivity.<init>(StatusActivity.java:30)
03-28 19:02:32.816: E/AndroidRuntime(278): at java.lang.Class.newInstanceImpl(Native Method)
03-28 19:02:32.816: E/AndroidRuntime(278): at java.lang.Class.newInstance(Class.java:1429)
03-28 19:02:32.816: E/AndroidRuntime(278): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
03-28 19:02:32.816: E/AndroidRuntime(278): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
03-28 19:02:32.816: E/AndroidRuntime(278): ... 11 more
This is what is causing your crash.
public Context context = getApplicationContext();
You're not even using it when you need it, so you can just get rid of this line.
Btw, something else I noticed while looking at your code is this:
oauthfile = new File("sdcard/auth_file.txt");
Don't take the "sdcard/" path for granted. Use this instead:
File dir = Environment.getExternalStorageDirectory();
File oauthfile = new File(dir, "auth_file.txt");

Inserting into SQLite database - Android

I am trying to implement a SQLite database for a highscores table. I am just testing it to see if my database creation and insertion is working with the below code. But I am getting a NullPointerException on the below lines.
Thank you in advance!
Results.java
public class Results extends Activity {
DatabaseHelper dh;
public void onCreate(Bundle savedInstanceState) {
dh = DatabaseHelper.getInstance(this);
public void showResults() {
dh.openDB();
dh.insert(1231423, 436346); //Line 104
}
}
DatabaseHelper.java
private static final String SCORE = "score";
private static final String PERCENTAGE = "percentage";
public static DatabaseHelper mSingleton = null;
public synchronized static DatabaseHelper getInstance(Context context) {
if(mSingleton == null) {
mSingleton = new DatabaseHelper(context.getApplicationContext());
}
return mSingleton;
}
public SQLiteDatabase openDB() {
return this.getWritableDatabase();
}
public long insert(long score, int percentage) {
ContentValues values = new ContentValues();
values.put(SCORE, score);
values.put(PERCENTAGE, percentage);
return db.insert(TABLE, null, values); //Line 91
}
LogCat output
01-02 17:56:47.609: E/AndroidRuntime(1322): FATAL EXCEPTION: main
01-02 17:56:47.609: E/AndroidRuntime(1322): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.Results}: java.lang.NullPointerException
01-02 17:56:47.609: E/AndroidRuntime(1322): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
01-02 17:56:47.609: E/AndroidRuntime(1322): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
01-02 17:56:47.609: E/AndroidRuntime(1322): at android.app.ActivityThread.access$600(ActivityThread.java:130)
01-02 17:56:47.609: E/AndroidRuntime(1322): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
01-02 17:56:47.609: E/AndroidRuntime(1322): at android.os.Handler.dispatchMessage(Handler.java:99)
01-02 17:56:47.609: E/AndroidRuntime(1322): at android.os.Looper.loop(Looper.java:137)
01-02 17:56:47.609: E/AndroidRuntime(1322): at android.app.ActivityThread.main(ActivityThread.java:4745)
01-02 17:56:47.609: E/AndroidRuntime(1322): at java.lang.reflect.Method.invokeNative(Native Method)
01-02 17:56:47.609: E/AndroidRuntime(1322): at java.lang.reflect.Method.invoke(Method.java:511)
01-02 17:56:47.609: E/AndroidRuntime(1322): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-02 17:56:47.609: E/AndroidRuntime(1322): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-02 17:56:47.609: E/AndroidRuntime(1322): at dalvik.system.NativeStart.main(Native Method)
01-02 17:56:47.609: E/AndroidRuntime(1322): Caused by: java.lang.NullPointerException
01-02 17:56:47.609: E/AndroidRuntime(1322): at com.example.test.DatabaseHelper.insert(DatabaseHelper.java:91)
01-02 17:56:47.609: E/AndroidRuntime(1322): at com.example.test.Results.showResults(Results.java:104)
01-02 17:56:47.609: E/AndroidRuntime(1322): at com.example.test.Results.onCreate(Results.java:50)
01-02 17:56:47.609: E/AndroidRuntime(1322): at android.app.Activity.performCreate(Activity.java:5008)
01-02 17:56:47.609: E/AndroidRuntime(1322): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
01-02 17:56:47.609: E/AndroidRuntime(1322): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
01-02 17:56:47.609: E/AndroidRuntime(1322): ... 11 more
EDIT: The NullPointerException is at least jumping around but errors still are there. I have edited my code and posted the new LogCat output.
Change openDB() to save the SqliteDatabase it opens and open a writable database:
public void openDB() {
db = this.getWritableDatabase();
}
But this still doesn't match your stack trace, however it matches the code you provided.
You said you are doing:
DatabaseHelper dh;
dh.insert(1231423, 436346);
Looks like you are not instantiating dh:
dh = new DatabaseHelper(this);
A better approach would be to use the singleton pattern as you are ever going to need just one DatabaseHelper instance:
private static final String TABLE_NAME = "table_name";
private static final int SCHEME_VERSION = 1;
public static DatabaseHelper mSingleton = null;
private DatabaseHelper(Context ctxt) {
super(ctxt, DATABASE_NAME, null, SCHEME_VERSION);
}
public synchronized static DatabaseHelper getInstance(Context ctxt) {
if (mSingleton == null)
mSingleton = new DatabaseHelper(ctxt.getApplicationContext());
return mSingleton;
}
Now you can call like so:
DatabaseHelper dh = DatabaseHelper.getInstance(this);
If you are following the common SqlliteHelper pattern, you are likely to have forgotten to call the .open() method before calling insert.
package com.example.hostelmangement;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DataBaseHelper extends SQLiteOpenHelper {
public DataBaseHelper(Context context, String name, CursorFactory factory,
int version)
{
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
#Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL("create table demo(id integer primary key,join_date date,name text,father_name text,date_of_birth text,house_name text,place text,pin_no text,blood_group text,mobile text,email text,company text,company_place text,company_phone text,company_pin,rent text,pending integer,status text,photo text,Proof text)");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// TODO Auto-generated method stub
}
}

NullPointer with SQLite creation - Android (java)

I am getting a NullPointerException in my SQLite database. I am calling the DatabaseHelper class in Results.java by doing this:
public class Results extends Activity {
...
DatabaseHelper dh;
public void onCreate(Bundle savedInstanceState) {
...
dh = new DatabaseHelper(this);
}
public void showResults() {
...
dh.insert(1, score, percentage);
}
}
DatabaseHelper.java
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DB_NAME = "test1";
private static final String DB_PATH = "/data/data/com.example.test/databases/";
private static final String TABLE = "HighscoresList";
// Table columns names.
private static final String RANK = "rank";
private static final String SCORE = "score";
private static final String PERCENTAGE = "percentage";
private SQLiteDatabase myDB;
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DATABASE_VERSION);
myDB = getWritableDatabase(); //Line 25
}
//Insert new record.
public long insert(int rank, long score, int percentage) {
ContentValues values = new ContentValues();
values.put(RANK, rank);
values.put(SCORE, score);
values.put(PERCENTAGE, percentage);
return myDB.insert(TABLE, null, values);
}
//Delete record.
public boolean delete(long score) {
//Need this?
return true;
}
public void openDatabase() throws SQLException {
//Open the database.
String myPath = DB_PATH + DB_NAME;
myDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
public SQLiteDatabase getDatabase(){
return this.myDB;
}
public synchronized void close() {
if(myDB != null) {
myDB.close();
}
super.close();
}
public void onCreate(SQLiteDatabase db) {
myDB.execSQL("CREATE TABLE " + TABLE + " (" //Line 62
+ RANK + " INTEGER,"
+ SCORE + " LONG,"
+ PERCENTAGE + " INTEGER"
+ ");");
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
}
LogCat output
12-18 22:01:53.210: E/AndroidRuntime(6313): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.Results}: java.lang.NullPointerException
12-18 22:01:53.210: E/AndroidRuntime(6313): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
12-18 22:01:53.210: E/AndroidRuntime(6313): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-18 22:01:53.210: E/AndroidRuntime(6313): at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-18 22:01:53.210: E/AndroidRuntime(6313): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-18 22:01:53.210: E/AndroidRuntime(6313): at android.os.Handler.dispatchMessage(Handler.java:99)
12-18 22:01:53.210: E/AndroidRuntime(6313): at android.os.Looper.loop(Looper.java:137)
12-18 22:01:53.210: E/AndroidRuntime(6313): at android.app.ActivityThread.main(ActivityThread.java:4745)
12-18 22:01:53.210: E/AndroidRuntime(6313): at java.lang.reflect.Method.invokeNative(Native Method)
12-18 22:01:53.210: E/AndroidRuntime(6313): at java.lang.reflect.Method.invoke(Method.java:511)
12-18 22:01:53.210: E/AndroidRuntime(6313): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-18 22:01:53.210: E/AndroidRuntime(6313): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-18 22:01:53.210: E/AndroidRuntime(6313): at dalvik.system.NativeStart.main(Native Method)
12-18 22:01:53.210: E/AndroidRuntime(6313): Caused by: java.lang.NullPointerException
12-18 22:01:53.210: E/AndroidRuntime(6313): at com.example.test.DatabaseHelper.onCreate(DatabaseHelper.java:62)
12-18 22:01:53.210: E/AndroidRuntime(6313): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
12-18 22:01:53.210: E/AndroidRuntime(6313): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
12-18 22:01:53.210: E/AndroidRuntime(6313): at com.example.test.DatabaseHelper.<init>(DatabaseHelper.java:25)
12-18 22:01:53.210: E/AndroidRuntime(6313): at com.example.test.Results.onCreate(Results.java:33)
12-18 22:01:53.210: E/AndroidRuntime(6313): at android.app.Activity.performCreate(Activity.java:5008)
12-18 22:01:53.210: E/AndroidRuntime(6313): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-18 22:01:53.210: E/AndroidRuntime(6313): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
12-18 22:01:53.210: E/AndroidRuntime(6313): ... 11 more
What is causing the NPE?
Also, any help on code structure for this SQLite highscores I am trying to implement would be helpful as well.

Categories