2016-03-04 103 views
-4

我們,我在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我,因爲我不知道問題在哪裏!

+0

投票前的評論會有幫助! – Bhargav

+1

哪一行是DisplayMessageActivity.java:89 – Jens

+0

它需要我「dbAdapter.open();」 – dvdLucka

回答

1

你需要學習如何閱讀的Java堆棧跟蹤

Nullpointers是最簡單的錯誤,通常修復。

Caused by: java.lang.NullPointerException 
    at com.example.davide.prova.DisplayMessageActivity$WifiScanReceiver.onReceive(DisplayMessageActivity.java:89) 

你總是想尋找你的第一個原因的錯誤(由) 這無關與Android或數據庫。

看看你的文件的第89行。該行中的某些內容爲空,並且您可以調用該方法。理解爲什麼它是空的/什麼時候會導致修復異常。

+0

在89行我打電話dbAdapter.open(),但數據庫isn 't null – dvdLucka

+0

dbAdapter本身是否爲NULL?我看到DBAdapter有一個構造函數,它接受上下文,但是我沒有看到任何dbAdapter對象的實例化。 – BryanT