我收到以下错误:
06-09 06:56:24.675: E/AndroidRuntime(1629): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gauvion.simpleworkouttracker/com.myapp.MainActivity}: java.lang.NullPointerException
Logcat说它的原因是:
06-09 06:56:24.675: E/AndroidRuntime(1629): Caused by: java.lang.NullPointerException
06-09 06:56:24.675: E/AndroidRuntime(1629): at com.myapp.MainActivity.onCreate(MainActivity.java:26)
以下是MainActivity的代码:
package com.myapp;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends ListActivity {
private DayDataSource datasource;
private SimpleCursorAdapter dataAdapter;
private Cursor cursor;
private boolean isEditing = false;
private Toast toast_deleted;
private String[] columns = new String[] { MySQLiteHelper.COLUMN_NAME, MySQLiteHelper.COLUMN_DAY };
private int[] to;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cursor = datasource.fetchAllDays();
startManagingCursor(cursor);
to = new int[] { R.id.listitem_day_name, R.id.listitem_day_day };
dataAdapter = new SimpleCursorAdapter(this, R.layout.listitem_day, cursor, columns, to, 0);
setListAdapter(dataAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
以下是DayDataSource的代码:
package com.myapp;
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 DayDataSource {
private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
public DayDataSource(Context context) {
dbHelper = new MySQLiteHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public void createDay(String name, String day) {
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.COLUMN_NAME, name);
values.put(MySQLiteHelper.COLUMN_DAY, day);
database.insert(MySQLiteHelper.TABLE_DAYS, null, values);
}
public void updateDay(long id, String name, String day) {
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.COLUMN_NAME, name);
values.put(MySQLiteHelper.COLUMN_DAY, day);
database.update(MySQLiteHelper.TABLE_DAYS, values, MySQLiteHelper.COLUMN_ID + " = " + id, null);
}
public void deleteDay(long id) {
database.delete(MySQLiteHelper.TABLE_DAYS, MySQLiteHelper.COLUMN_ID + " = " + id, null);
}
public Cursor fetchAllDays() {
Cursor cursor = database.rawQuery("select " + MySQLiteHelper.COLUMN_ID + ", " + MySQLiteHelper.COLUMN_NAME + ", " + MySQLiteHelper.COLUMN_DAY + " " +
"from " + MySQLiteHelper.TABLE_DAYS + " " +
"order by case " + MySQLiteHelper.COLUMN_DAY + " " +
"when 'Monday' then 0 " +
"when 'Tuesday' then 1 " +
"when 'Wednesday' then 2 " +
"when 'Thursday' then 3 " +
"when 'Friday' then 4 " +
"when 'Saturday' then 5 " +
"when 'Sunday' then 6 " +
"when 'No Specific Day' then 7 " +
"end", null);
if (cursor != null) { cursor.moveToFirst(); }
return cursor;
}
}
非常感谢任何帮助。
答案 0 :(得分:5)
您尝试使用datasource
而不初始化
cursor = datasource.fetchAllDays();
onCreate 中的 datasource
为空