首先,我是Android设计的新手,所以请帮帮我。 我正在尝试配置两个活动,第一个在列表中显示Trips,第二个用于填写用户正式定义Trip。但是,当我单击第二个活动上的创建按钮时,我得到了NullPointerException的失败交付结果,为什么会有任何想法?
这是我的MainActivity,onClick添加它执行的startActivityForResult();
public class MainActivity extends ListActivity {
private static final int TRIP_CREATE=100;
private TripsData datasource;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
datasource=new TripsData(this);
datasource.open();
List<Trip> values=datasource.getAllTrips();
ArrayAdapter<Trip> adapter=new ArrayAdapter<Trip>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
// Will be called via the onClick attribute
// of the buttons in main.xml
public void onClick(View view) {
@SuppressWarnings("unchecked")
ArrayAdapter<Trip> adapter=(ArrayAdapter<Trip>) getListAdapter();
Trip trip=null;
switch (view.getId()) {
case R.id.add:
//Starting a new Intent
Intent nextScreen=new Intent(getApplicationContext(), TripCreateActivity.class);
// starting new activity
startActivityForResult(nextScreen, TRIP_CREATE);
break;
case R.id.delete:
if (getListAdapter().getCount()> 0) {
trip=(Trip) getListAdapter().getItem(0);
datasource.deleteTrip(trip);
adapter.remove(trip);
}
break;
}
}
@Override
protected void onResume() {
datasource.open();
super.onResume();
}
@Override
protected void onPause() {
datasource.close();
super.onPause();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
Bundle extras=data.getExtras();
ArrayAdapter<Trip> adapter=(ArrayAdapter<Trip>) getListAdapter();
if (resultCode==Activity.RESULT_OK) {
if(requestCode==TRIP_CREATE){
//String result=data.getStringExtra("name");
Trip trip=datasource.createTrip(data.getStringExtra("name"),
data.getStringExtra("type"));
adapter.add(trip);
}
if (resultCode==RESULT_CANCELED) {
//Write your code if there's no result
}
adapter.notifyDataSetChanged();
}
}
}
这是填写旅行信息的活动,我不认为这个问题在这里:
public class TripCreateActivity extends Activity {
protected static final int TRIP_CREATE=100;
private TripsData datasource;
EditText inputName;
EditText inputType;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_trip_create);
datasource=new TripsData(this);
datasource.open();
inputName=(EditText) findViewById(R.id.name);
inputType=(EditText) findViewById(R.id.type);
Button Finish=(Button) findViewById(R.id.finish);
Finish.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//Starting a new Intent
Intent nextScreen=new Intent(getApplicationContext(), MainActivity.class);
//Intent nextScreen=new Intent();
//Sending data to another Activity
nextScreen.putExtra("name", inputName.getText().toString());
nextScreen.putExtra("type", inputType.getText().toString());
setResult(Activity.RESULT_OK, nextScreen);
Toast.makeText(getApplicationContext(), "Create button: "+inputName.getText().toString(), Toast.LENGTH_LONG).show();
finish();
//startActivity(nextScreen);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.trip_create, menu);
return true;
}
}
这是TripsData代码,我是数据库的新手,所以我认为错误就在这里:
public class TripsData {
private SQLiteDatabase database;
private TripsDB dbHelper;
private String[] allTrips = { TripsDB.TRIP_COLUMN_ID,
TripsDB.TRIP_COLUMN_TYPE};
public TripsData(Context context){
dbHelper = new TripsDB(context);
}
public void open() throws SQLException{
database = dbHelper.getWritableDatabase();
}
public void close(){
dbHelper.close();
}
public Trip createTrip(String name, String type){
ContentValues values = new ContentValues();
values.put(TripsDB.TRIP_COLUMN_NAME, name);
values.put(TripsDB.TRIP_COLUMN_TYPE, type);
long insertId = database.insert(TripsDB.TABLE_TRIPS, null, values);
Cursor cursor = database.query(TripsDB.TABLE_TRIPS,
allTrips, TripsDB.TRIP_COLUMN_ID + " = " + insertId, null, null, null, null);
cursor.moveToFirst();
Trip newTrip = cursorToTrip(cursor);
cursor.close();
return newTrip;
}
public void deleteTrip(Trip trip){
long id = trip.getId();
System.out.println("Project deleted with id: " + id);
database.delete(TripsDB.TABLE_TRIPS, TripsDB.TRIP_COLUMN_ID
+ " = " + id, null);
}
public List<Trip> getAllTrips(){
List<Trip> trips = new ArrayList<Trip>();
Cursor cursor = database.query(TripsDB.TABLE_TRIPS,
allTrips, null, null, null, null, null);
cursor.moveToFirst();
while(!cursor.isAfterLast()){
Trip trip = cursorToTrip(cursor);
trips.add(trip);
cursor.moveToNext();
}
cursor.close();
return trips;
}
private Trip cursorToTrip(Cursor cursor){
Trip trip = new Trip();
trip.setId(cursor.getLong(0));
trip.setName(cursor.getString(1));
return trip;
}
logcat的:
02-22 12:20:37.251: I/name =(5947): Hard coded Name
02-22 12:20:37.251: I/type=(5947): Hard code Type
02-22 12:20:37.251: I/SqliteDatabaseCpp(5947): sqlite returned: error code = 1, msg = table trips has no column named name, db=/data/data/com.example.festivent/databases/trips.db
02-22 12:20:37.272: E/SQLiteDatabase(5947): Error inserting name=Hard coded Name
02-22 12:20:37.272: E/SQLiteDatabase(5947): android.database.sqlite.SQLiteException: table trips has no column named name: , while compiling: INSERT INTO trips(name) VALUES (?)
02-22 12:20:37.272: E/SQLiteDatabase(5947): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
02-22 12:20:37.272: E/SQLiteDatabase(5947): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
02-22 12:20:37.272: E/SQLiteDatabase(5947): at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
02-22 12:20:37.272: E/SQLiteDatabase(5947): at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
02-22 12:20:37.272: E/SQLiteDatabase(5947): at android.database.sqlite.SQLiteStatement.acquireAndLock(SQLiteStatement.java:260)
02-22 12:20:37.272: E/SQLiteDatabase(5947): at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:112)
02-22 12:20:37.272: E/SQLiteDatabase(5947): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1718)
02-22 12:20:37.272: E/SQLiteDatabase(5947): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1591)
02-22 12:20:37.272: E/SQLiteDatabase(5947): at com.example.merptivity.TripsData.createTrip(TripsData.java:37)
02-22 12:20:37.272: E/SQLiteDatabase(5947): at com.example.merptivity.MainActivity.onCreate(MainActivity.java:38)
02-22 12:20:37.272: E/SQLiteDatabase(5947): at android.app.Activity.performCreate(Activity.java:4465)
02-22 12:20:37.272: E/SQLiteDatabase(5947): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-22 12:20:37.272: E/SQLiteDatabase(5947): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
02-22 12:20:37.272: E/SQLiteDatabase(5947): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
02-22 12:20:37.272: E/SQLiteDatabase(5947): at android.app.ActivityThread.access$600(ActivityThread.java:122)
02-22 12:20:37.272: E/SQLiteDatabase(5947): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
02-22 12:20:37.272: E/SQLiteDatabase(5947): at android.os.Handler.dispatchMessage(Handler.java:99)
02-22 12:20:37.272: E/SQLiteDatabase(5947): at android.os.Looper.loop(Looper.java:137)
02-22 12:20:37.272: E/SQLiteDatabase(5947): at android.app.ActivityThread.main(ActivityThread.java:4340)
02-22 12:20:37.272: E/SQLiteDatabase(5947): at java.lang.reflect.Method.invokeNative(Native Method)
02-22 12:20:37.272: E/SQLiteDatabase(5947): at java.lang.reflect.Method.invoke(Method.java:511)
02-22 12:20:37.272: E/SQLiteDatabase(5947): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-22 12:20:37.272: E/SQLiteDatabase(5947): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-22 12:20:37.272: E/SQLiteDatabase(5947): at dalvik.system.NativeStart.main(Native Method)
02-22 12:20:37.272: D/AndroidRuntime(5947): Shutting down VM
02-22 12:20:37.272: W/dalvikvm(5947): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
02-22 12:20:37.292: E/AndroidRuntime(5947): FATAL EXCEPTION: main
02-22 12:20:37.292: E/AndroidRuntime(5947): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.festivent/com.example.merptivity.MainActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
02-22 12:20:37.292: E/AndroidRuntime(5947): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
02-22 12:20:37.292: E/AndroidRuntime(5947): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
02-22 12:20:37.292: E/AndroidRuntime(5947): at android.app.ActivityThread.access$600(ActivityThread.java:122)
02-22 12:20:37.292: E/AndroidRuntime(5947): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
02-22 12:20:37.292: E/AndroidRuntime(5947): at android.os.Handler.dispatchMessage(Handler.java:99)
02-22 12:20:37.292: E/AndroidRuntime(5947): at android.os.Looper.loop(Looper.java:137)
02-22 12:20:37.292: E/AndroidRuntime(5947): at android.app.ActivityThread.main(ActivityThread.java:4340)
02-22 12:20:37.292: E/AndroidRuntime(5947): at java.lang.reflect.Method.invokeNative(Native Method)
02-22 12:20:37.292: E/AndroidRuntime(5947): at java.lang.reflect.Method.invoke(Method.java:511)
02-22 12:20:37.292: E/AndroidRuntime(5947): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-22 12:20:37.292: E/AndroidRuntime(5947): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-22 12:20:37.292: E/AndroidRuntime(5947): at dalvik.system.NativeStart.main(Native Method)
02-22 12:20:37.292: E/AndroidRuntime(5947): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
02-22 12:20:37.292: E/AndroidRuntime(5947): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:434)
02-22 12:20:37.292: E/AndroidRuntime(5947): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
02-22 12:20:37.292: E/AndroidRuntime(5947): at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:74)
02-22 12:20:37.292: E/AndroidRuntime(5947): at com.example.merptivity.TripsData.cursorToTrip(TripsData.java:71)
02-22 12:20:37.292: E/AndroidRuntime(5947): at com.example.merptivity.TripsData.createTrip(TripsData.java:41)
02-22 12:20:37.292: E/AndroidRuntime(5947): at com.example.merptivity.MainActivity.onCreate(MainActivity.java:38)
02-22 12:20:37.292: E/AndroidRuntime(5947): at android.app.Activity.performCreate(Activity.java:4465)
02-22 12:20:37.292: E/AndroidRuntime(5947): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-22 12:20:37.292: E/AndroidRuntime(5947): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
02-22 12:20:37.292: E/AndroidRuntime(5947): ... 11 more
修改 这是我的数据表:
public static abstract class TRIP implements BaseColumns{
public static final String TABLE_NAME = "trips";
public static final String COLUMN_TITLE = "title";
public static final String COLUMN_TYPE = "_type";
public static final String COLUMN_COST = "cost";
public static final String COLUMN_START = "start";
public static final String COLUMN_END = "end";
public static final String SQL_CREATE = "create table " + TABLE_NAME
+ "(" + TRIP._ID + " integer primary key autoincrement, "
+ COLUMN_TITLE + " text, "
+ COLUMN_TYPE
+ " text);";
}