我正在尝试使用其他数据(用户当前的 SSID)更新我的数据库,但是在尝试保存数据库时 - 整个应用程序强制关闭。
我知道保存数据时会导致 NullPointer 异常 - 但我不知道我做错了什么导致它。
日志猫:
04-04 21:24:08.351: E/AndroidRuntime(10745): FATAL EXCEPTION: AsyncTask #2
04-04 21:24:08.351: E/AndroidRuntime(10745): java.lang.RuntimeException: An error occured while executing doInBackground()
04-04 21:24:08.351: E/AndroidRuntime(10745): at android.os.AsyncTask$3.done(AsyncTask.java:299)
04-04 21:24:08.351: E/AndroidRuntime(10745): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
04-04 21:24:08.351: E/AndroidRuntime(10745): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
04-04 21:24:08.351: E/AndroidRuntime(10745): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
04-04 21:24:08.351: E/AndroidRuntime(10745): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-04 21:24:08.351: E/AndroidRuntime(10745): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
04-04 21:24:08.351: E/AndroidRuntime(10745): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
04-04 21:24:08.351: E/AndroidRuntime(10745): at java.lang.Thread.run(Thread.java:856)
04-04 21:24:08.351: E/AndroidRuntime(10745): Caused by: java.lang.NullPointerException
04-04 21:24:08.351: E/AndroidRuntime(10745): at com.app.gamedemo.AddEditDevice.saveContact(AddEditDevice.java:117)
04-04 21:24:08.351: E/AndroidRuntime(10745): at com.app.gamedemo.AddEditDevice.access$1(AddEditDevice.java:111)
04-04 21:24:08.351: E/AndroidRuntime(10745): at com.app.gamedemo.AddEditDevice$1$1.doInBackground(AddEditDevice.java:83)
04-04 21:24:08.351: E/AndroidRuntime(10745): at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-04 21:24:08.351: E/AndroidRuntime(10745): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
04-04 21:24:08.351: E/AndroidRuntime(10745): ... 4 more
来源:(更新了 SCF 的建议)
ADDEDITDEVICE.java
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.ViewGroup;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import java.text.DecimalFormat;
public class AddEditDevice extends Activity {
private long rowID;
private EditText nameEt;
private EditText capEt;
private EditText codeEt;
private TimePicker timeEt;
private TextView ssidTextView;
// @Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.add_country);
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
String ssidString = info.getSSID();
//TextView ssidTextView = (TextView) findViewById(R.id.wifiSSID);
ssidTextView = (TextView) findViewById(R.id.wifiSSID);
ssidTextView.setText(ssidString);
nameEt = (EditText) findViewById(R.id.nameEdit);
capEt = (EditText) findViewById(R.id.capEdit);
codeEt = (EditText) findViewById(R.id.codeEdit);
timeEt = (TimePicker) findViewById(R.id.timeEdit);
Bundle extras = getIntent().getExtras();
if (extras != null)
{
rowID = extras.getLong("row_id");
nameEt.setText(extras.getString("name"));
capEt.setText(extras.getString("cap"));
codeEt.setText(extras.getString("code"));
String time = extras.getString("time");
String[] parts = time.split(":");
timeEt.setCurrentHour(Integer.valueOf(parts[0]));
timeEt.setCurrentMinute(Integer.valueOf(parts[1]));
timeEt.setIs24HourView(false);
}
Button saveButton =(Button) findViewById(R.id.saveBtn);
saveButton.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
if (capEt.getText().length() != 0)
{
AsyncTask<Object, Object, Object> saveContactTask =
new AsyncTask<Object, Object, Object>()
{
@Override
protected Object doInBackground(Object... params)
{
saveContact();
return null;
}
@Override
protected void onPostExecute(Object result)
{
finish();
}
};
saveContactTask.execute((Object[]) null);
}
else
{
AlertDialog.Builder alert = new AlertDialog.Builder(AddEditDevice.this);
alert.setTitle(R.string.errorTitle);
alert.setMessage(R.string.errorMessage);
alert.setPositiveButton(R.string.errorButton, null);
alert.show();
}
}
});}
private void saveContact()
{
DatabaseConnector dbConnector = new DatabaseConnector(this);
if (getIntent().getExtras() == null)
{
dbConnector.insertContact(nameEt.getText().toString(),
capEt.getText().toString(),
timeEt.getCurrentHour().toString() + ":"
+ timeEt.getCurrentMinute().toString(),
codeEt.getText().toString(),
ssidTextView.getText().toString());
}
else
{
dbConnector.updateContact(rowID,
nameEt.getText().toString(),
capEt.getText().toString(),
timeEt.getCurrentHour().toString() + ":"
+ timeEt.getCurrentMinute().toString(),
codeEt.getText().toString(),
ssidTextView.getText().toString());
}
}
}
来源:
数据库连接器.java
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
public class DatabaseConnector {
private static final String DB_NAME = "WorldCountries";
private SQLiteDatabase database;
private DatabaseOpenHelper dbOpenHelper;
public DatabaseConnector(Context context) {
dbOpenHelper = new DatabaseOpenHelper(context, DB_NAME, null, 1);
}
public void open() throws SQLException
{
//open database in reading/writing mode
database = dbOpenHelper.getWritableDatabase();
}
public void close()
{
if (database != null)
database.close();
}
public void insertContact(String name, String cap, String code, String time, String ssid)
{
ContentValues newCon = new ContentValues();
newCon.put("name", name);
newCon.put("cap", cap);
newCon.put("time", time);
newCon.put("code", code);
newCon.put("ssid", ssid);
open();
database.insert("country", null, newCon);
close();
}
public void updateContact(long id, String name, String cap, String code, String time, String ssid)
{
ContentValues editCon = new ContentValues();
editCon.put("name", name);
editCon.put("cap", cap);
editCon.put("time", time);
editCon.put("code", code);
editCon.put("ssid", ssid);
open();
database.update("country", editCon, "_id=" + id, null);
close();
}
public Cursor getAllContacts()
{
return database.query("country", new String[] {"_id", "name"},
null, null, null, null, "name");
}
public Cursor getOneContact(long id)
{
return database.query("country", null, "_id=" + id, null, null, null, null);
}
public void deleteContact(long id)
{
open();
database.delete("country", "_id=" + id, null);
close();
}
}