我們,我在android上做一個練習,點擊一個按鈕後會檢索區域中的所有WIFI名稱。我需要保存在數據庫中的一些信息,但是當我運行該項目,一個NullPointerException停止qhen打開數據庫空指針異常數據庫Android
logcat的
03-04 18:44:16.225 12670-12670/com.example.davide.prova E/AndroidRuntime: FATAL EXCEPTION: main
03-04 18:44:16.225 12670-12670/com.example.davide.prova E/AndroidRuntime: java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.wifi.SCAN_RESULTS flg=0x8000010 } in c[email protected]42465e28
03-04 18:44:16.225 12670-12670/com.example.davide.prova E/AndroidRuntime: at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:765)
03-04 18:44:16.225 12670-12670/com.example.davide.prova E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:615)
03-04 18:44:16.225 12670-12670/com.example.davide.prova E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:92)
03-04 18:44:16.225 12670-12670/com.example.davide.prova E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
03-04 18:44:16.225 12670-12670/com.example.davide.prova E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:4867)
03-04 18:44:16.225 12670-12670/com.example.davide.prova E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
03-04 18:44:16.225 12670-12670/com.example.davide.prova E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
03-04 18:44:16.225 12670-12670/com.example.davide.prova E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
03-04 18:44:16.225 12670-12670/com.example.davide.prova E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
03-04 18:44:16.225 12670-12670/com.example.davide.prova E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
03-04 18:44:16.225 12670-12670/com.example.davide.prova E/AndroidRuntime: Caused by: java.lang.NullPointerException
03-04 18:44:16.225 12670-12670/com.example.davide.prova E/AndroidRuntime: at com.example.davide.prova.DisplayMessageActivity$WifiScanReceiver.onReceive(DisplayMessageActivity.java:89)
03-04 18:44:16.225 12670-12670/com.example.davide.prova E/AndroidRuntime: at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:755)
03-04 18:44:16.225 12670-12670/com.example.davide.prova E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:615)
03-04 18:44:16.225 12670-12670/com.example.davide.prova E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:92)
03-04 18:44:16.225 12670-12670/com.example.davide.prova E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
03-04 18:44:16.225 12670-12670/com.example.davide.prova E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:4867)
03-04 18:44:16.225 12670-12670/com.example.davide.prova E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
03-04 18:44:16.225 12670-12670/com.example.davide.prova E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
03-04 18:44:16.225 12670-12670/com.example.davide.prova E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
03-04 18:44:16.225 12670-12670/com.example.davide.prova E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
03-04 18:44:16.225 12670-12670/com.example.davide.prova E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
這是我聲明類的DB:
public class DBAdapter {
private static final String LOG_TAG = "DatabaseUtil";
/*NOME DEL DATABASE*/
private static final String DB_NAME = "DB_WIFIdiscover";
private static final int DB_VERSION = 1;
/*NOMI TABELLE*/
private static final String DB_TABLE_WIFI_INFO = "Table_stato_wifi";
private static final String DB_TABLE_LOCATION = "Table_dove_sei";
/*DATABASE INFORMAZIONI RELATIVE AL WIFI TROVATO*/
/*DATABASE COORDINATE*/
public static final String COLUMN_LOCATION_ID = "LocationID";
public static final String COLUMN_LATITUDINE = "Longitudine";
public static final String COLUMN_LONGITUDINE = "Latitudine";
private static final String SQL_CREATE_ENTRIES = "create table " //
+ DB_TABLE_LOCATION + " (" //
+ COLUMN_LOCATION_ID + " integer primary key autoincrement, " //
+ COLUMN_LATITUDINE + " DOUBLE NOT NULL, " //
+ COLUMN_LONGITUDINE + " DOUBLE NOT NULL);";
private final Context mCtx;
private FROpenHelper dbHelper;
private SQLiteDatabase mDb;
private String TAG = this.getClass().getCanonicalName();
//private SQLiteDatabase db; //si riferisce al database
//private FROpenHelper dbHelper; //si riferisce all'OpenHelper
private static class FROpenHelper extends SQLiteOpenHelper {
public FROpenHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) throws SQLException {
Log.w(LOG_TAG, "Creating database.");
try {
db.execSQL(SQL_CREATE_ENTRIES);
} catch (SQLException e) {
Log.e(LOG_TAG, e.getMessage());
throw e;
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(LOG_TAG, "Upgrading from version " + oldVersion + " to " + newVersion
+ ", which will destroy all old data");
// Create a new one.
onCreate(db);
}
}
public DBAdapter(Context context) {
this.mCtx = context;
}
public DBAdapter open() throws SQLException {
try {
dbHelper = new FROpenHelper(mCtx);
mDb = dbHelper.getWritableDatabase();
} catch (SQLiteException e) {
Log.e(TAG, e.getMessage());
throw e;
}
return this;
}
/**
* Close the DB.
*/
public void close() {
dbHelper.close();
}
而這正是我所說的開放式方法的類:
public class DisplayMessageActivity extends AppCompatActivity {
ListView lv;
WifiManager wifi;
String wifis[];
WifiScanReceiver wifiReciever;
public DBAdapter dbAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
//
lv = (ListView)findViewById(R.id.listView);
wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
wifiReciever = new WifiScanReceiver();
wifi.startScan();
//layout dell'activity
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
protected void onPause() {
unregisterReceiver(wifiReciever);
super.onPause();
}
protected void onResume() {
registerReceiver(wifiReciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
super.onResume();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
private class WifiScanReceiver extends BroadcastReceiver {
@TargetApi(Build.VERSION_CODES.M)
public void onReceive(Context c, Intent intent) {
List<ScanResult> wifiScanList = wifi.getScanResults();//lista degli hotspot trovati
wifis = new String[wifiScanList.size()];//array di stringhe della lunghezza del numeri di hotspot trovati
//for(int i = 0; i < wifiScanList.size(); i++){
//if(wifiScanList.get(i).is80211mcResponder() == true){//controlla se è un canale 802.11, se lo è lo salva nel database
// wifis[i] = ((wifiScanList.get(i).SSID).toString());
//Inserisco nel DB i valori della rete wireless
//long idpos = dbAdapter.insertDB();
//if(idpos == -1){
//}
dbAdapter.open();
dbAdapter.insertDB();//passa valori della rete
dbAdapter.close();
//}
//SSID nome della rete, BSSID modalità di funzionamento wifi, frequency, level(dbm)
//}
lv.setAdapter(new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,wifis));//reference to an built-in XML layout document that is part of the Android OS
}
}
}
請^ h elp我,因爲我不知道問題在哪裏!
投票前的評論會有幫助! – Bhargav
哪一行是DisplayMessageActivity.java:89 – Jens
它需要我「dbAdapter.open();」 – dvdLucka