当我点击按钮时,我的应用程序为什么会崩溃,并且如何修复它?我需要在点击按钮时向我的 SQLite 数据库中插入一行。它包含两个值,这应该很容易。一个是使用 simpledateformatter 格式化的日期。另一个只是来自 ArrayList 的位置。我的代码中没有红线,但是当我按下按钮以插入行时,我的应用程序崩溃了。下面是三个 Java 文件之间交互的代码。我有一个预感问题出在 Model.java 中,但也可能不止这些。如果需要更多代码,请询问。
MainActivity.java
MainActivity.java
public class MainActivity extends Activity {
final String TAG = "*** DEBUG ***";
public static final String SMOKIN_DATA_FILE = "smokin.dat";
public static final int EDIT_ACTIVITY = 1;
public static Model model = null;
public static MySmokinDatabase mySmokinDatabase;
public static Cursor cursor;
public static SimpleCursorAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
restoreModel();
if (mySmokinDatabase == null)
mySmokinDatabase = new MySmokinDatabase(this);
refreshView();
}
@Override
protected void onResume() {
super.onResume();
if (model == null)
restoreModel();
refreshView();
}
protected void onPause() {
super.onPause();
saveModel();
}
private void refreshView() {
Spinner spinner = (Spinner) findViewById(R.id.location_spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, model.getLocationsArray());
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
SimpleDateFormat sdf = new SimpleDateFormat("E, MMM dd");
TextView dateText = (TextView) findViewById(R.id.date);
TextView countText = (TextView) findViewById(R.id.count);
TextView daysText = (TextView) findViewById(R.id.days);
TextView totalText = (TextView) findViewById(R.id.total);
TextView aveText = (TextView) findViewById(R.id.ave);
GregorianCalendar now = new GregorianCalendar();
dateText.setText(sdf.format(now.getTime()));
//get today's count from data in the SQLite table - count entries with today's date
countText.setText("" + "");
//mySmokinDatabase.getTodaysCount());
// Table data
daysText.setText("" + String.format("%10d", model.getDays()));
totalText.setText("" + "get total count from data in SQLite table - count total rows");
if (model.getDays() > 0)
aveText.setText("calc average from SQLite and model info");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == EDIT_ACTIVITY) {
saveModel();
}
}
public void smokedHandler(View view) {
Spinner spinner = (Spinner) findViewById(R.id.location_spinner);
String s = spinner.getSelectedItem().toString();
String d = model.getDates();
mySmokinDatabase.insertSmokinValues(new Model(s,d));
cursor = mySmokinDatabase.getAllSmokinCursor();
adapter.changeCursor(cursor);
refreshView();
}
public void restoreModel() {
// Restore from disk, or start with an empty model
try {
ObjectInputStream ois = new ObjectInputStream(
openFileInput(SMOKIN_DATA_FILE));
model = (Model) ois.readObject();
ois.close();
}
catch (Exception e){
Log.v("*** DEBUG ***", "Error writing to file: " + e);
model = new Model();
}
}
public void saveModel() {
Log.v("*** AJ ***", "In onPause");
try {
ObjectOutputStream oos = new ObjectOutputStream(
openFileOutput(SMOKIN_DATA_FILE, Context.MODE_PRIVATE));
oos.writeObject(model);
oos.close();
}
catch (Exception e){
Log.v("*** MatchIt ***", "Error writing to file: " + e);
}
}
DB文件
public long insertSmokinValues(Model model) {
ContentValues newSmokinValues = new ContentValues();
newSmokinValues.put(KEY_DATE, model.getDates());
newSmokinValues.put(KEY_LOCATION, model.getLocations());
SQLiteDatabase db = smokinDBOpenHelper.getWritableDatabase();
return db.insert(SmokinDBOpenHelper.INCIDENTS_TABLE, null, newSmokinValues);
}
public Cursor getAllSmokinCursor() {
SQLiteDatabase db = smokinDBOpenHelper.getWritableDatabase();
return db.query(SmokinDBOpenHelper.INCIDENTS_TABLE, new String[]
{KEY_ID, KEY_LOCATION, KEY_DATE}, null, null, null, null, null);
}
模型文件
private ArrayList<String> locations = new ArrayList<String>();
public String [] defaultLocations = {"Home", "Work", "Commuting", "School", "Bar",
"Restaurant", "Social Gathering", "Other"};
public Model(GregorianCalendar date){
startDate = date;
for (String s : this.defaultLocations)
locations.add(s);
}
public Model(String s, String d) {
place = s;
dates = d;
}
public String getDates() {
GregorianCalendar gc = new GregorianCalendar();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
dates = sdf.format(gc.getTime());
return dates;
}
public String getLocations() {
for (String s : this.locations)
place = s;
return place;
}
如所请求,以下是日志记录:
03-18 15:52:42.397: W/dalvikvm(676): threadid=1: thread exiting with uncaught
exception
(group=0x40a13300)
03-18 15:52:42.457: E/AndroidRuntime(676): FATAL EXCEPTION: main
03-18 15:52:42.457: E/AndroidRuntime(676): java.lang.IllegalStateException: Could not
execute method of the activity
03-18 15:52:42.457: E/AndroidRuntime(676): at
android.view.View$1.onClick(View.java:3591)
03-18 15:52:42.457: E/AndroidRuntime(676): at
android.view.View.performClick(View.java:4084)
03-18 15:52:42.457: E/AndroidRuntime(676): at
android.view.View$PerformClick.run(View.java:16966)
03-18 15:52:42.457: E/AndroidRuntime(676): at
android.os.Handler.handleCallback(Handler.java:615)
03-18 15:52:42.457: E/AndroidRuntime(676): at
android.os.Handler.dispatchMessage(Handler.java:92)
03-18 15:52:42.457: E/AndroidRuntime(676): at android.os.Looper.loop(Looper.java:137)
03-18 15:52:42.457: E/AndroidRuntime(676): at
android.app.ActivityThread.main(ActivityThread.java:4745)
03-18 15:52:42.457: E/AndroidRuntime(676): at
java.lang.reflect.Method.invokeNative(Native Method)
03-18 15:52:42.457: E/AndroidRuntime(676): at
java.lang.reflect.Method.invoke(Method.java:511)
03-18 15:52:42.457: E/AndroidRuntime(676): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-18 15:52:42.457: E/AndroidRuntime(676): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-18 15:52:42.457: E/AndroidRuntime(676): at dalvik.system.NativeStart.main(Native
Method)
03-18 15:52:42.457: E/AndroidRuntime(676): Caused by:
java.lang.reflect.InvocationTargetException
03-18 15:52:42.457: E/AndroidRuntime(676): at
java.lang.reflect.Method.invokeNative(Native Method)
03-18 15:52:42.457: E/AndroidRuntime(676): at
java.lang.reflect.Method.invoke(Method.java:511)
03-18 15:52:42.457: E/AndroidRuntime(676): at
android.view.View$1.onClick(View.java:3586)
03-18 15:52:42.457: E/AndroidRuntime(676): ... 11 more
03-18 15:52:42.457: E/AndroidRuntime(676): Caused by: java.lang.NullPointerException
03-18 15:52:42.457: E/AndroidRuntime(676): at
com.example.smokin4ThomasSullivan.MainActivity.smokedHandler(MainActivity.java:119)
03-18 15:52:42.457: E/AndroidRuntime(676): ... 14 more
第119行:adapter.changeCursor(cursor); 所涉及的方法在我的数据库文件中发布
一如既往地感谢您的帮助!