2016-07-05 195 views
1

問題:這隻發生在某些頁面上。當我按回應用程序停止。我得到這個錯誤。我無法弄清楚確切的問題。我是android新手。這發生在許多頁面。任何幫助將不勝感激。每當我按下後退按鈕時,應用程序停止

logcat的錯誤:

InputEventSender:Exception dispatching finished signal.E/MessageQueue-JNI:Exception in MessageQueue callback:handleReceiveCallbackE/MessageQueue-JNI:java.lang.NullPointerException: Attempt to invoke virtual method 'void android.database.sqlite.SQLiteDatabase.close()' on a null object reference at twik.in.DBHelper.close(DBHelper.java:102)at twik.in.ActivityMenuDetail.onBackPressed(ActivityMenuDetail.java:285)at android.app.Activity.onKeyUp(Activity.java:2477)at android.view.KeyEvent.dispatch(KeyEvent.java:2664)at android.app.Activity.dispatchKeyEvent(Activity.java:2730) 

在誤差這兩個類被提及,所以我在這裏張貼的代碼

public class ActivityMenuDetail extends Activity { 

ImageView imgPreview; 
TextView txtText, txtSubText; 
WebView txtDescription; 
Button btnAdd; 
ScrollView sclDetail; 
ProgressBar prgLoading; 
TextView txtAlert; 

// declare dbhelper object 
static DBHelper dbhelper; 

// declare ImageLoader object 
ImageLoader imageLoader; 

// declare variables to store menu data 
String Menu_image, Menu_name, Menu_serve, Menu_description; 
double Menu_price; 
int Menu_quantity; 
long Menu_ID; 
String MenuDetailAPI; 
int IOConnect = 0; 

// create price format 
DecimalFormat formatData = new DecimalFormat("#.##"); 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.menu_detail); 

    ActionBar bar = getActionBar(); 
    bar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.header))); 
    bar.setTitle("Service Centre Detail"); 
    bar.setDisplayHomeAsUpEnabled(true); 
    bar.setHomeButtonEnabled(true); 

    imgPreview = (ImageView) findViewById(R.id.imgPreview); 
    txtText = (TextView) findViewById(R.id.txtText); 
    txtSubText = (TextView) findViewById(R.id.txtSubText); 
    txtDescription = (WebView) findViewById(R.id.txtDescription); 
    btnAdd = (Button) findViewById(R.id.btnAdd); 
    //btnShare = (Button) findViewById(R.id.btnShare); 
    sclDetail = (ScrollView) findViewById(R.id.sclDetail); 
    prgLoading = (ProgressBar) findViewById(R.id.prgLoading); 
    txtAlert = (TextView) findViewById(R.id.txtAlert); 

    // get screen device width and height 
    DisplayMetrics dm = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(dm); 
    int wPix = dm.widthPixels; 
    int hPix = wPix/2 + 50; 

    // change menu image width and height 
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(wPix, hPix); 
    imgPreview.setLayoutParams(lp); 

    imageLoader = new ImageLoader(ActivityMenuDetail.this); 
    dbhelper = new DBHelper(this); 

    // get menu id that sent from previous page 
    Intent iGet = getIntent(); 
    Menu_ID = iGet.getLongExtra("menu_id", 0); 

    // Menu detail API url 
    MenuDetailAPI = Constant.MenuDetailAPI+"?accesskey="+Constant.AccessKey+"&menu_id="+Menu_ID; 

    // call asynctask class to request data from server 
    new getDataTask().execute();  

    // event listener to handle add button when clicked 
    btnAdd.setOnClickListener(new OnClickListener() { 

     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 
      // show input dialog 
      addtoCart(); 
     } 
    }); 



} 

@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_detail, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    switch (item.getItemId()) { 
    case R.id.cart: 
     // refresh action 
     Intent iMyOrder = new Intent(ActivityMenuDetail.this, ActivityCart.class); 
     startActivity(iMyOrder); 
     overridePendingTransition (R.anim.open_next, R.anim.close_next); 
     return true; 

    case android.R.id.home: 
     // app icon in action bar clicked; go home 
     this.finish(); 
     overridePendingTransition(R.anim.open_main, R.anim.close_next); 
     return true; 

    default: 
     return super.onOptionsItemSelected(item); 
    } 

} 

// method to show number of order form 

void addtoCart(){ 
    // open database first 
    try{ 
     dbhelper.openDataBase(); 
    }catch(SQLException sqle){ 
     throw sqle; 
    } 

    if(dbhelper.isDataExist(Menu_ID)){ 
     dbhelper.updateData(Menu_ID, 1, (Menu_price)); 
    }else{ 
     dbhelper.addData(Menu_ID, Menu_name, 1, (Menu_price)); 
    } 

    startActivity(new Intent(ActivityMenuDetail.this,ActivityCart.class)); 

}; 

// asynctask class to handle parsing json in background 
public class getDataTask extends AsyncTask<Void, Void, Void>{ 

    // show progressbar first 
    getDataTask(){ 
     if(!prgLoading.isShown()){ 
      prgLoading.setVisibility(0); 
      txtAlert.setVisibility(8); 
     } 
    } 

    @Override 
    protected Void doInBackground(Void... arg0) { 
     // TODO Auto-generated method stub 
     // parse json data from server in background 
     parseJSONData(); 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     // TODO Auto-generated method stub 
     // when finish parsing, hide progressbar 
     prgLoading.setVisibility(8); 
     // if internet connection and data available show data 
     // otherwise, show alert text 
     if((Menu_name != null) && IOConnect == 0){ 
      sclDetail.setVisibility(0); 

      imageLoader.DisplayImage(Constant.AdminPageURL+Menu_image, imgPreview); 

      txtText.setText(Menu_name); 
      txtSubText.setText("Price : " +Menu_price+" "+ActivityMenuList.Currency+"\n"+"Status : "+Menu_serve+"\n"+"Empty Slots : "+Menu_quantity); 
      txtDescription.loadDataWithBaseURL("", Menu_description, "text/html", "UTF-8", ""); 
      txtDescription.setBackgroundColor(Color.parseColor("#e7e7e7")); 
     }else{ 
      txtAlert.setVisibility(0); 
     } 
    } 
} 

// method to parse json data from server 
public void parseJSONData(){ 

    try { 
     // request data from menu detail API 
     HttpClient client = new DefaultHttpClient(); 
     HttpConnectionParams.setConnectionTimeout(client.getParams(), 15000); 
     HttpConnectionParams.setSoTimeout(client.getParams(), 15000); 
     HttpUriRequest request = new HttpGet(MenuDetailAPI); 
     HttpResponse response = client.execute(request); 
     InputStream atomInputStream = response.getEntity().getContent(); 


     BufferedReader in = new BufferedReader(new InputStreamReader(atomInputStream)); 

     String line; 
     String str = ""; 
     while ((line = in.readLine()) != null){ 
      str += line; 
     } 

     // parse json data and store into tax and currency variables 
     JSONObject json = new JSONObject(str); 
     JSONArray data = json.getJSONArray("data"); // this is the "items: [ ] part 

     for (int i = 0; i < data.length(); i++) { 
      JSONObject object = data.getJSONObject(i); 

      JSONObject menu = object.getJSONObject("Menu_detail"); 

      Menu_image = menu.getString("Menu_image"); 
      Menu_name = menu.getString("Menu_name"); 
      Menu_price = Double.valueOf(formatData.format(menu.getDouble("Price"))); 
      Menu_serve = menu.getString("Serve_for"); 
      Menu_description = menu.getString("Description"); 
      Menu_quantity = menu.getInt("Quantity"); 

     } 


    } catch (MalformedURLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     IOConnect = 1; 
     e.printStackTrace(); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 


// close database before back to previous page 
@Override 
public void onBackPressed() { 
    // TODO Auto-generated method stub 
    super.onBackPressed(); 
    dbhelper.close(); 
    finish(); 
    overridePendingTransition(R.anim.open_main, R.anim.close_next); 
} 


@Override 
protected void onDestroy() { 
    // TODO Auto-generated method stub 
    //imageLoader.clearCache(); 
    super.onDestroy(); 
} 


@Override 
public void onConfigurationChanged(final Configuration newConfig) 
{ 
    // Ignore orientation change to keep activity from restarting 
    super.onConfigurationChanged(newConfig); 
} 
} 

這裏

public class DBHelper extends SQLiteOpenHelper{ 

String DB_PATH; 
private final static String DB_NAME = "db_order"; 
public final static int DB_VERSION = 1; 
public static SQLiteDatabase db; 

private final Context context; 

private final String TABLE_NAME = "tbl_order"; 
private final String ID = "id"; 
private final String MENU_NAME = "Menu_name"; 
private final String QUANTITY = "Quantity"; 
private final String TOTAL_PRICE = "Total_price"; 


public DBHelper(Context context) { 

    super(context, DB_NAME, null, DB_VERSION); 
    this.context = context; 

    DB_PATH = Constant.DBPath; 
} 

public void createDataBase() throws IOException{ 

    boolean dbExist = checkDataBase(); 
    SQLiteDatabase db_Read = null; 


    if(dbExist){ 
     //do nothing - database already exist 

    }else{ 
     db_Read = this.getReadableDatabase(); 
     db_Read.close(); 

     try { 
      copyDataBase(); 
     } catch (IOException e) { 
      throw new Error("Error copying database"); 
     } 
    } 

} 



private boolean checkDataBase(){ 

    File dbFile = new File(DB_PATH + DB_NAME); 

    return dbFile.exists(); 

} 


private void copyDataBase() throws IOException{ 

    InputStream myInput = context.getAssets().open(DB_NAME); 

    String outFileName = DB_PATH + DB_NAME; 

    OutputStream myOutput = new FileOutputStream(outFileName); 

    byte[] buffer = new byte[1024]; 
    int length; 
    while ((length = myInput.read(buffer))>0){ 
     myOutput.write(buffer, 0, length); 
    } 

    myOutput.flush(); 
    myOutput.close(); 
    myInput.close(); 

} 

public void openDataBase() throws SQLException{ 
    String myPath = DB_PATH + DB_NAME; 
    db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE); 
} 

@Override 
public void close() { 
    db.close(); 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 

} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 

} 

/** this code is used to get all data from database */ 
public ArrayList<ArrayList<Object>> getAllData(){ 
    ArrayList<ArrayList<Object>> dataArrays = new ArrayList<ArrayList<Object>>(); 

    Cursor cursor = null; 

     try{ 
      cursor = db.query(
        TABLE_NAME, 
        new String[]{ID, MENU_NAME, QUANTITY, TOTAL_PRICE}, 
        null,null, null, null, null); 
      cursor.moveToFirst(); 

      if (!cursor.isAfterLast()){ 
       do{ 
        ArrayList<Object> dataList = new ArrayList<Object>(); 

        dataList.add(cursor.getLong(0)); 
        dataList.add(cursor.getString(1)); 
        dataList.add(cursor.getString(2)); 
        dataList.add(cursor.getString(3)); 

        dataArrays.add(dataList); 
       } 

       while (cursor.moveToNext()); 
      } 
      cursor.close(); 
     }catch (SQLException e){ 
      Log.e("DB Error", e.toString()); 
      e.printStackTrace(); 
     } 

    return dataArrays; 
} 

/** this code is used to get all data from database */ 
public boolean isDataExist(long id){ 
    boolean exist = false; 

    Cursor cursor = null; 

     try{ 
      cursor = db.query(
        TABLE_NAME, 
        new String[]{ID}, 
        ID +"="+id, 
        null, null, null, null); 
      if(cursor.getCount() > 0){ 
       exist = true; 
      } 

      cursor.close(); 
     }catch (SQLException e){ 
      Log.e("DB Error", e.toString()); 
      e.printStackTrace(); 
     } 

    return exist; 
} 

/** this code is used to get all data from database */ 
public boolean isPreviousDataExist(){ 
    boolean exist = false; 

    Cursor cursor = null; 

     try{ 
      cursor = db.query(
        TABLE_NAME, 
        new String[]{ID}, 
        null,null, null, null, null); 
      if(cursor.getCount() > 0){ 
       exist = true; 
      } 

      cursor.close(); 
     }catch (SQLException e){ 
      Log.e("DB Error", e.toString()); 
      e.printStackTrace(); 
     } 

    return exist; 
} 

public void addData(long id, String menu_name, int quantity, double total_price){ 
    // this is a key value pair holder used by android's SQLite functions 
    ContentValues values = new ContentValues(); 
    values.put(ID, id); 
    values.put(MENU_NAME, menu_name); 
    values.put(QUANTITY, quantity); 
    values.put(TOTAL_PRICE, total_price); 

    // ask the database object to insert the new data 
    try{db.insert(TABLE_NAME, null, values);} 
    catch(Exception e) 
    { 
     Log.e("DB ERROR", e.toString()); 
     e.printStackTrace(); 
    } 
} 

public void deleteData(long id){ 
    // ask the database manager to delete the row of given id 
    try {db.delete(TABLE_NAME, ID + "=" + id, null);} 
    catch (Exception e) 
    { 
     Log.e("DB ERROR", e.toString()); 
     e.printStackTrace(); 
    } 
} 

public void deleteAllData(){ 
    // ask the database manager to delete the row of given id 
    try {db.delete(TABLE_NAME, null, null);} 
    catch (Exception e) 
    { 
     Log.e("DB ERROR", e.toString()); 
     e.printStackTrace(); 
    } 
} 

public void updateData(long id, int quantity, double total_price){ 
    // this is a key value pair holder used by android's SQLite functions 
    ContentValues values = new ContentValues(); 
    values.put(QUANTITY, quantity); 
    values.put(TOTAL_PRICE, total_price); 

    // ask the database object to update the database row of given rowID 
    try {db.update(TABLE_NAME, values, ID + "=" + id, null);} 
    catch (Exception e) 
    { 
     Log.e("DB Error", e.toString()); 
     e.printStackTrace(); 
    } 
}} 
+0

只是改變這個如果(dbhelper!= NULL){dbhelper.close();} – Nisarg

+0

刪除'super.onBackPressed();''從onBackPressed()'方法 –

+0

刪除,但仍然崩潰。 –

回答

0

從您提供的錯誤跟蹤,似乎在DBHelper「DB」的實例爲空。因此,在您嘗試通過調用dbHelper.close()關閉數據庫時,您的onBackPressed()會拋出NullPointerException。添加空檢查將有所幫助。

//關閉數據庫之前返回上一頁 @覆蓋

public void onBackPressed() { 
// TODO Auto-generated method stub 
super.onBackPressed(); 
dbhelper.close(); 
finish(); 
overridePendingTransition(R.anim.open_main, R.anim.close_next); 
} 

上述功能最終會調用下面的功能:

@Override 
public void close() { 
db.close(); 
} 

添加空校驗爲 「DB」

@Override 
public void close() { 
if (db != null) { 
    db.close(); 
} 
} 

快速瀏覽一下你的代碼,看起來你正在打開數據庫僅限於「添加到購物車」之類的操作。

void addtoCart(){ 
// open database first 
try{ 
    dbhelper.openDataBase(); 
}catch(SQLException sqle){ 
    throw sqle; 
} 

if(dbhelper.isDataExist(Menu_ID)){ 
    dbhelper.updateData(Menu_ID, 1, (Menu_price)); 
}else{ 
    dbhelper.addData(Menu_ID, Menu_name, 1, (Menu_price)); 
} 

startActivity(new Intent(ActivityMenuDetail.this,ActivityCart.class)); 

}; 

如果您嘗試關閉屏幕而未執行此操作,則DB對象將不可用。

+0

您很重要。當我在應用程序中打開我的購物車頁面後,它永遠不會崩潰。但如果我不打開它並在其他地方導航它崩潰。如何解決它。新手在這裏。它是一個痛點。你發現了確切的行爲。 PLZ建議soln –

+0

這很簡單。在關閉數據庫之前添加空檢查。我在上面的snipper中也提到了相同的內容。在close()方法中,在DBHelper類本身中添加一個檢查。環繞「db.close()」用if語句檢查db null。 – Keya

+0

當我添加上面的代碼@override變成紅色並且說它不覆蓋來自上層的方法。如果你發表一個精心製作的方法去做你的建議,那將是非常棒的。以上所有的soln都還沒有工作。 –

1

的debhelper類logcat的說,你的dbhelper預成型時對象爲空close()

@Override 
public void onBackPressed() { 
    // TODO Auto-generated method stub 
    super.onBackPressed(); 
    if (dbhelper != null) 
     dbhelper.close(); 
    finish(); 
    overridePendingTransition(R.anim.open_main, R.anim.close_next); 
} 

這應該解決這個問題

+0

沒有解脫。仍在發生。應用程序使用相同的logcat停止工作 –

+0

?請在一分鐘內添加新的logcat –

+0

發佈新日誌。工作室在低頭撞擊。 –

0

根據logcat你的dbhelper爲空,所以它給出了一個NULL POINTER EXCEPTION。

@Override 
public void onBackPressed() { 
    // TODO Auto-generated method stub 
    super.onBackPressed(); 
    dbhelper.close(); 
    finish(); 
    overridePendingTransition(R.anim.open_main, R.anim.close_next); 
} 

在上述方法中,super.onBackPressed();正在關閉數據庫中,這不是一個好的做法之前首先被調用。

將其更改爲,

@Override 
    public void onBackPressed() { 
     if (dbhelper != null) 
      dbhelper.close(); 
     finish(); 
     overridePendingTransition(R.anim.open_main, R.anim.close_next); 
     super.onBackPressed(); 
    } 

而且改變,

public void close() { 
db.close(); 
} 

public void close() { 
if(db!=null) 
db.close(); 
} 
在Dbhelper.class

+0

將其更改爲您的建議。仍然崩潰。 –

+0

logcat現在說什麼? –

+0

嘗試刪除'super.onBackPressed();' –

0

這是因爲它可以讓空對象引用的dbhelper an d也不需要使用finish();當onBackPressed()被稱爲活動自動完成。

if (dbhelper != null) 
      dbhelper.close(); 
     finish(); 
     overridePendingTransition(R.anim.open_main, R.anim.close_next); 
     super.onBackPressed(); 
相關問題