简体   繁体   中英

the application has stopped, when i tried to clear all data

i tried to clear all data from the table inside my database, it was successfull, but the only problems is, everytime i pressed the clear button, it does clear all data but after that the application shows it has stopped

当我单击清除按钮时

which part did i go wrong? below is my coding from my database:

public boolean deleteRow(long id) {

        SQLiteDatabase db = helper.getWritableDatabase();
        String where = data.UID + "=" + id;
        return db.delete(data.TABLE_NAME, where, null) != 0;
    }

    public void DeleteAll(){

        Cursor c = readEntry();
        long id = c.getColumnIndexOrThrow(data.UID);
        if (c.moveToFirst()) {
            do {
                deleteRow(c.getLong((int) id));
            } while (c.moveToNext());
        }
        c.close();
        return;
    }

and code from the other java:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.showbmi);
        initializeApp();
    }
    private void initializeApp(){

        weightinputid = (EditText) findViewById(R.id.weightid);
        heightinputid = (EditText) findViewById(R.id.heightid);
        buttonBMI = (Button) findViewById(R.id.buttonBMI);
        BMIfinal= (TextView) findViewById(R.id.BMIfinal);
        BMIStatus = (TextView) findViewById(R.id.BMIstatus);
        save = (Button) findViewById(R.id.button);
        detail = (Button)findViewById(R.id.button1);
        bb = (Button)findViewById(R.id.bb);
        table_layout = (TableLayout) findViewById(R.id.tableLayout1);

        data  = new fitnessdatabase(this);
        BuildTable();
        bb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                data.DeleteAll();
                new MyAsync().execute();
            }
        });



private class MyAsync extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {

            super.onPreExecute();

            table_layout.removeAllViews();

            PD = new ProgressDialog(Showbmi.this);
            PD.setTitle("Please Wait..");
            PD.setMessage("Loading...");
            PD.setCancelable(false);
            PD.show();
        }

        @Override
        protected Void doInBackground(Void... params) {

            String weight = weightinputid.getText().toString();
            String bmi = BMIfinal.getText().toString();
            String status = BMIStatus.getText().toString();
            String date1 = mDateText.getText().toString();

            // inserting data
            data.open();
        //    sqlcon.insertData(firstname, lastname);
            long id = data.insertData(weight, bmi, status, date1);
            BuildTable();
            return null;
        }

        }

the error from the logcat as shown below:

java.util.concurrent.FutureTask.setException(FutureTask.java:219)
            at java.util.concurrent.FutureTask.run(FutureTask.java:239)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
            at java.lang.Thread.run(Thread.java:856)
     Caused by: java.lang.NullPointerException
            at com.example.adiehabs.fitnessku.Showbmi$MyAsync.doInBackground(Showbmi.java:188)
            at com.example.adiehabs.fitnessku.Showbmi$MyAsync.doInBackground(Showbmi.java:169)
            at android.os.AsyncTask$2.call(AsyncTask.java:287)
            at java.util.concurrent.FutureTask.run(FutureTask.java:234)

When you try data.Open() it hits an uninitialised variable as you've initalised it in a private void. Change it to public or put all that code into your created bundle. As the UI controls will also be null.

edit your updated with logcat confirms this. In the do in background.

Caused by: java.lang.NullPointerException at com.example.adiehabs.fitnessku.Showbmi$MyAsync.doInBackground(Showbmi.java:188)

private void initializeApp(){

if (c.moveToFirst()) {
        do {
            deleteRow(c.getLong((int) id));
        } while (c.moveToNext());
    }

I would use: Move the cursor to first and only move it if it's not gone past the last item.

  c.moveToFirst(); 
       while(!c.isAfterLast()) {
            deleteRow(c.getLong((int) id));
           c.moveToNext();
        } 
    }

The other thing:

Have you declared these variables in your main scope table_layout , PD ?
The app not responding is often from a nullpointer exception.

Delete all table rows

public boolean deleteTable(){

    return mSQLiteDatabase.delete(DataBaseHelper.TABLE, null, null) > 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM