为什么单击我的应用程序会崩溃button
,如何解决?
单击按钮时,row
我需要在其中插入一个。SQLite
database
它包含两个值,这应该很容易。一个是date
, 使用simpledateformatter
. 另一个只是一个位置ArrayList
。我的代码中没有红线,但是当我按下该insert
行的按钮时,我的应用程序崩溃了。下面是来自Java
相互交互的 3 个文件的代码。我有一种预感,问题出在我的 Model.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);
}
}
数据库文件
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); 所涉及的方法发布在我的数据库文件中
一如既往地感谢任何帮助!