Ich muss doch noch einmal kurz nerven...
Ich habe eine neue Tabelle erstellt, in die Spiele eingefügt werden sollen.
Wenn ich aber auf "Spiel speichern" klicke, dann kriege ich eine "java.lang.nullpointerexception".
Project - clean und Eclipse neu starten habe ich schon versucht...bringt nichts...
Hier der entsprechende Code:
AddGame.java
package manager.scg;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Add_Game extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addgame);
// gamedatasource = new GameDataSource(this);
Button prev = (Button) findViewById(R.id.prev_addgame);
prev.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}
private GameDataSource gamedatasource;
List<EntryGame> GameList = new ArrayList<EntryGame>();
public void SaveGameClick (View view) {
String hometeam = null;
String guestteam = null;
String place = null;
String referee = null;
String date = null;
String time = null;
EditText edithometeam = (EditText)findViewById(R.id.edithometeam);
EditText editguestteam = (EditText)findViewById(R.id.editguestteam);
EditText editplace = (EditText)findViewById(R.id.editplace);
EditText editreferee = (EditText)findViewById(R.id.editreferee);
EditText editdate = (EditText)findViewById(R.id.editdate);
EditText edittime = (EditText)findViewById(R.id.edittime);
if (edithometeam.getEditableText().toString().length() == 0) {
return;
}
if (editguestteam.getEditableText().toString().length() == 0) {
return;
}
if (editplace.getEditableText().toString().length() == 0) {
return;
}
if (editreferee.getEditableText().toString().length() == 0) {
return;
}
if (editdate.getEditableText().toString().length() == 0) {
return;
}
if (edittime.getEditableText().toString().length() == 0) {
return;
}
hometeam = edithometeam.getEditableText().toString();
guestteam = editguestteam.getEditableText().toString();
place = editplace.getEditableText().toString();
referee = editreferee.getEditableText().toString();
date = editdate.getEditableText().toString();
time = edittime.getEditableText().toString();
try {
gamedatasource.open();
gamedatasource.createEntryGame(hometeam, guestteam, place, referee, date, time);
gamedatasource.close();
Toast.makeText(this, "Spiel hinzugefügt.", Toast.LENGTH_LONG).show();
edithometeam.setText("");
editguestteam.setText("");
editplace.setText("");
editreferee.setText("");
editdate.setText("");
edittime.setText("");
}
catch (Exception ex) {
Toast.makeText(this,ex.toString(), Toast.LENGTH_LONG).show();
}
}
}
Alles anzeigen
EntryGame.java
package manager.scg;
public class EntryGame{
private long _id;
private String hometeam;
private String guestteam;
private String place;
private String referee;
private String date;
private String time;
public void set_id(long _id) {
this._id = _id;
}
public long get_id() {
return _id;
}
public void setHometeam(String hometeam) {
this.hometeam = hometeam;
}
public String getHometeam() {
return hometeam;
}
public void setGuestteam(String guestteam) {
this.guestteam = guestteam;
}
public String getGuestteam() {
return guestteam;
}
public void setPlace(String place) {
this.place = place;
}
public String getPlace() {
return place;
}
public void setReferee(String referee) {
this.referee = referee;
}
public String getReferee() {
return referee;
}
public void setDate(String date) {
this.date = date;
}
public String getDate() {
return date;
}
public void setTime(String time) {
this.time = time;
}
public String getTime() {
return time;
}
public String toString() {
return String.format("%s %s", hometeam, guestteam);
}
}
Alles anzeigen
GameDataSource.java
package manager.scg;
import java.util.ArrayList;
import java.util.List;
import manager.scg.MySQLiteHelper;
import manager.scg.EntryGame;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
public class GameDataSource{
private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
private String[] allColumns = {"_id", "HOMETEAM", "GUESTTEAM", "PLACE", "REFEREE",
"DATE", "TIME" };
public GameDataSource(Context context) {
dbHelper = new MySQLiteHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public EntryGame createEntryGame(String hometeam, String guestteam, String place,
String referee, String date, String time) {
ContentValues values = new ContentValues();
values.put("HOMETEAM", hometeam);
values.put("GUESTTEAM", guestteam);
values.put("PLACE", place);
values.put("REFEREE", referee);
values.put("DATE", date);
values.put("TIME", time);
long insertId = database.insert("GAME", null, values);
Cursor cursor = database.query("GAME", allColumns,
"_id = " + insertId, null, null, null, null, null);
if (cursor.getCount() == 0) {
Log.w("GameDataSource.createEntryGame()","keine Daten gefunden, ID=" + insertId);
return null;
} else {
Log.v("GameDataSource.createEntryGame()","Daten gefunden, ID=" + insertId);
}
cursor.moveToFirst();
return cursorToEntryGame(cursor);
}
protected List<EntryGame> getAllEntriesGame() {
List<EntryGame> EntriesListGame = new ArrayList<EntryGame>();
EntriesListGame = new ArrayList<EntryGame>();
Cursor cursor = database.query("GAME", allColumns, null, null, null,
null, null);
cursor.moveToFirst();
if (cursor.getCount() == 0)
return EntriesListGame;
while (cursor.isAfterLast() == false) {
EntryGame entry = cursorToEntryGame(cursor);
EntriesListGame.add(entry);
cursor.moveToNext();
}
cursor.close();
return EntriesListGame;
}
private EntryGame cursorToEntryGame(Cursor cursor) {
EntryGame entry = new EntryGame();
entry.set_id(cursor.getLong(0));
entry.setHometeam(cursor.getString(1));
entry.setGuestteam(cursor.getString(2));
entry.setPlace(cursor.getString(3));
entry.setReferee(cursor.getString(4));
entry.setDate(cursor.getString(5));
entry.setTime(cursor.getString(6));
return entry;
}
}
Alles anzeigen
MySQLiteHelper.java
package manager.scg;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class MySQLiteHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "scgmanager.db";
private static final int DATABASE_VERSION = 8;
private static final String TABLE_CREATE_PLAYER = ""
+ "create table PLAYER("
+ "_id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "NAME TEXT, "
+ "LASTNAME TEXT, "
+ "EMAIL TEXT, "
+ "CELL INTEGER,"
+ "PASSNUMBER INTEGER)";
private static final String TABLE_CREATE_GAME = ""
+ "create table GAME("
+ "_id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "HOMETEAM TEXT, "
+ "GUESTTEAM TEXT, "
+ "PLACE TEXT, "
+ "REFEREE TEXT,"
+ "DATE TEXT,"
+ "TIME TEXT )";
public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(TABLE_CREATE_PLAYER);
database.execSQL(TABLE_CREATE_GAME);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(MySQLiteHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS PLAYER");
db.execSQL("DROP TABLE IF EXISTS GAME");
onCreate(db);
}
}
Alles anzeigen
Schedule
package manager.scg;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class Schedule extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.schedule);
gamedatasource = new GameDataSource(this);
Schedule.this.ListGame(null);
Button prev = (Button) findViewById(R.id.prev_schedule);
prev.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
Button addgame = (Button) findViewById(R.id.addgame);
addgame.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Add_Game.class);
startActivityForResult(myIntent, 0);
}
});
}
private GameDataSource gamedatasource;
List<EntryGame> GameList = new ArrayList<EntryGame>();
public void ListGame (View view) {
setContentView(R.layout.schedule);
GameList.clear();
try {
gamedatasource.open();
GameList = gamedatasource.getAllEntriesGame();
gamedatasource.close();
}
catch (Exception ex) {
Toast.makeText(this, ex.toString(), Toast.LENGTH_SHORT).show();
}
ArrayAdapter<EntryGame> adapterVerlauf = new ArrayAdapter<EntryGame>(Schedule.this, android.R.layout.simple_list_item_1, GameList);
ListView lGame = (ListView) findViewById(R.id.listViewGame);
lGame.setAdapter(adapterVerlauf);
/* lGame.setTextFilterEnabled(true);
lGame.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent myIntent = new Intent(getApplicationContext(), Game.class);
myIntent.putExtra("GameList", (ArrayList<EntryGame>) GameList );
startActivityForResult(myIntent, 0);
}
}); */
}
}
Alles anzeigen