Heyho,
nen paar Tutorials weiter steh ich wieder einmal (trotz vermeindlich richtigen codes) mit eclipse auf kriegsfuß. Das Problem ist, dass meine Funktion "loadProject" scheinbar noch vor Aufruf ne NullPointerException wirft.
hier der code der fraglichen activity bis zur Problemstelle:
Java
package and07c.lektion2.components;
import java.text.SimpleDateFormat;
import java.util.Date;
import and07.lektion2.db.DbManager;
import and07.lektion2.db.Note;
import and07.lektion2.db.NoteLocation;
import and07.lektion2.db.Project;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class GatherActivity extends ActionBarActivity {
private LocationManager locationManager;
private TextView tvHello, tvOutput;
private EditText etSubject, etNote;
private String newLine = System.getProperty("line.seperator"), name;
private NoteLocation lastLocation;
public static final String PROVIDER = "gps";
private static final String TAG = GatherActivity.class.getSimpleName();
private DbManager dbManager;
private Date timeStamp;
private SimpleDateFormat sdf = new SimpleDateFormat("yy_MM_dd");
private SimpleDateFormat sdfDateTime = new SimpleDateFormat(
"dd.MM.yyyy HH:mm:ss");
private Intent serviceIntent;
private LocationService.LocationServiceBinder binder;
private Project project;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gather);
if (savedInstanceState == null)
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
project = new Project();
timeStamp = new Date();
Log.d(TAG, "load project");
Project temp = dbManager.loadProject(project.getName());
//Project temp = dbManager.loadProject("14_05_12");
Alles anzeigen
Die Klasse DbManager:
Java
package and07.lektion2.db;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DbManager extends SQLiteOpenHelper {
private SQLiteDatabase db;
private static final String TAG = DbManager.class.getSimpleName();
public static final String DB_NAME = "GeoNotes";
public static final int DB_VERSION = 1;
private final String CREATE_PROJECTS = "CREATE TABLE Projects( project_id INTEGER PRIMARY KEY NOT"
+ " NULL, name TEXT NOT NULL, description text);";
private final String CREATE_LOCATIONS = "CREATE TABLE IF NOT EXISTS Locations(time INTEGER"
+ " PRIMARY KEY NOT NULL, project_id INTEGER NOT NULL, latitude REAL NOT NULL, "
+ "longitude REAL NOT NULL, provider TEXT NOT NULL, CONSTRAINT ProjectFK "
+ "FOREIGN KEY(project_id) REFERENCES Projects(project_id) ON DELETE RESTRICT"
+ " ON UPDATE CASCADE);";
private final String CREATE_NOTES = "CREATE TABLE IF NOT EXISTS Notes(time INTEGER PRIMARY KEY"
+ " NOT NULL, time_loc INTEGER NOT NULL, project_id INTEGER NOT NULL, subject TEXT,"
+ " note TEXT NOT NULL, CONSTRAINT LocationsFK FOREIGN KEY(time_loc)REFERENCES Locations(time)"
+ " ON DELETE RESTRICT ON UPDATE CASCADE, CONSTRAINT ProjectFK FOREIGN KEY(project_id)"
+ " REFERENCES Projects(project_id) ON DELETE RESTRICT ON UPDATE CASCADE);";
public DbManager(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("PRAGMA foreign_keys = ON;");
db.execSQL(CREATE_PROJECTS);
db.execSQL(CREATE_LOCATIONS);
db.execSQL(CREATE_NOTES);
}
public void writeInDb(String sqlString) {
if (db == null || db.isOpen())
db = this.getWritableDatabase();
try {
db.execSQL(sqlString);
} catch (SQLException sqle) {
Log.e(TAG, sqle.toString());
}
}
public Project loadProject(String name) {
Log.d(TAG, "loadProject gestartet");
Project project = null;
String query = "SELECT * FROM Projects WHERE name=" + name;
Cursor cursor = queryDb(query);
if (cursor.moveToNext()) {
long projectID = cursor.getLong(0);
String subject = cursor.getString(1);
String description = cursor.getString(2);
project = new Project(projectID, subject, description);
}
cursor.close();
return project;
}
public Cursor queryDb(String sqlString) {
Log.d(TAG, "queryDb gestartet");
if (db == null || !db.isOpen())
db = this.getWritableDatabase();
Cursor cursor = null;
try {
cursor = db.rawQuery(sqlString, null);
} catch (SQLException sqle) {
Log.d(TAG, sqle.toString());
}
return cursor;
}
public void closeDb() {
db.close();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
Alles anzeigen
Die Klasse Project:
Java
package and07.lektion2.db;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;
public class Project implements Parcelable {
private long projectID;
private static final String TAG = Project.class.getSimpleName();
public String description = "";
private SimpleDateFormat sdfProject = new SimpleDateFormat("yy_MM_dd",
Locale.GERMAN);
private String name;
public long getProjectID() {
return projectID;
}
public String getName() {
Log.d(TAG, "name: " + name);
return name;
}
public Project() {
projectID = System.currentTimeMillis();
name = sdfProject.format(new Date(projectID));
}
public String getInsertString() {
return "INSERT INTO PROJECTS VALUES(" + projectID + ", '" + name
+ "', '" + description + "');";
}
public void appendToName(String termToAppend) {
name = sdfProject.format(new Date(projectID)) + "_" + termToAppend;
}
public Project(long projectID, String name, String description) {
this.projectID = projectID;
this.name = name;
this.description = description;
}
public static final Parcelable.Creator<Project> CREATOR = new Creator<Project>() {
@Override
public Project[] newArray(int size) {
return new Project[size];
}
@Override
public Project createFromParcel(Parcel source) {
return new Project(source);
}
};
private Project(Parcel source) {
projectID = source.readLong();
name = source.readString();
description = source.readString();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(projectID);
dest.writeString(name);
dest.writeString(description);
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
}
Alles anzeigen
und des LogCat (ohne zeitangaben):
Code
D/GatherActivity(2220): load project
D/Project(2220): name: 14_05_12
W/dalvikvm(2220): threadid=1: thread exiting with uncaught exception (group=0x40add9f0)
E/AndroidRuntime(2220): FATAL EXCEPTION: main
E/AndroidRuntime(2220): java.lang.RuntimeException: Unable to start activity ComponentInfo{and07c.lektion2.components/and07c.lektion2.components.GatherActivity}: java.lang.NullPointerException
E/AndroidRuntime(2220): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
E/AndroidRuntime(2220): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
E/AndroidRuntime(2220): at android.app.ActivityThread.access$600(ActivityThread.java:123)
E/AndroidRuntime(2220): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
E/AndroidRuntime(2220): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(2220): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(2220): at android.app.ActivityThread.main(ActivityThread.java:4424)
E/AndroidRuntime(2220): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(2220): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(2220): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
E/AndroidRuntime(2220): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
E/AndroidRuntime(2220): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(2220): Caused by: java.lang.NullPointerException
E/AndroidRuntime(2220): at and07c.lektion2.components.GatherActivity.onCreate(GatherActivity.java:59)
E/AndroidRuntime(2220): at android.app.Activity.performCreate(Activity.java:4470)
E/AndroidRuntime(2220): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
E/AndroidRuntime(2220): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
E/AndroidRuntime(2220): ... 11 more
Alles anzeigen
Mich irritiert bei dem problem vor allen die Tatsache, dass nicht einmal der Log Aufruf der Funktion loadProject aufgerufen geht, die Funktion somit scheinbar nicht einmal gestartet wird.