每按一次按钮,应用程序停止

时间:2016-07-05 09:11:06

标签: android

问题:这只发生在某些页面上。当我按回app停止。我收到这个错误。我无法弄清楚确切的问题。我是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);
}
}

和debhelper课程在这里

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();
    }
}}

4 个答案:

答案 0 :(得分:1)

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);
}

那应该解决问题

答案 1 :(得分:0)

从您提供的错误跟踪中,似乎&#34; db&#34; DBHelper中的实例为null。因此,当您尝试通过调用dbHelper.close()来关闭db时,在onBackPressed()中,它会抛出NullPointerException。添加空检查将有所帮助。

//在返回上一页之前关闭数据库 @override

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();
}

为&#34; db&#34;

添加空检查
@Override
public void close() {
if (db != null) {
    db.close();
}
}

快速浏览一下代码,似乎只是在&#34;添加到购物车&#34;等操作上打开数据库。

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对象将无法使用。

答案 2 :(得分:0)

根据logcat,你的dbhelper是null,所以它给出了一个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中

答案 3 :(得分:0)

这是因为它可能会获得dbhelper的空对象引用,并且它也不需要使用finish();当onBackPressed()调用活动自动完成时。

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