异步任务 - 空指针异常

时间:2015-12-23 14:57:18

标签: java android sqlite

Android新手在这里。我正在努力让数据库与我的应用程序一起工作。我正在尝试使用异步任务来执行数据库操作,但是当我尝试将数据添加到数据库时,我得到了nullpointerexception。

下面是我的logcat

12-23 14:50:07.775  21300-21743/com.example.myacer.clubhub E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:299)
            at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:856)
     Caused by: java.lang.NullPointerException
            at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
            at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
            at com.example.myacer.clubhub.Database.BackGroundTask.doInBackground(BackGroundTask.java:53)
            at com.example.myacer.clubhub.Database.BackGroundTask.doInBackground(BackGroundTask.java:18)
            at android.os.AsyncTask$2.call(AsyncTask.java:287)
            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:856)

我的异步课程         package com.example.myacer.clubhub.Database;

    import android.app.Activity;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.AsyncTask;
    import android.widget.ListView;
    import android.widget.Toast;

    import com.example.myacer.clubhub.Manager.ManagerSchedule;
    import com.example.myacer.clubhub.Manager.UpdateSchedule;
    import com.example.myacer.clubhub.R;


public class BackGroundTask extends AsyncTask<String, Fixture, String> {

Context ctx;
FixtureAdapter fixtureAdapter;
Activity activity;
ListView listView;

BackGroundTask(Context ctx) {
    this.ctx = ctx;
    activity = (Activity) ctx;
}

public BackGroundTask(UpdateSchedule updateSchedule) {
}


@Override
protected void onPreExecute() {
    super.onPreExecute();
}

public BackGroundTask(ManagerSchedule managerSchedule) {
}

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

    String method = params[0];
    MySQLiteHelper mySQLiteHelper = new MySQLiteHelper(ctx);
    if (method.equals("add_info")) {
        String date = params[1];
        String time = params[2];
        String opponent = params[3];
        String venue = params[4];

        SQLiteDatabase db = mySQLiteHelper.getWritableDatabase();
        mySQLiteHelper.addFixture(db, date, time, opponent, venue);
        return "one fixture added...";
    } else if (method.equals("get_info")) {

        listView = (ListView) activity.findViewById(R.id.scheduleView);
        SQLiteDatabase sqLiteDatabase = mySQLiteHelper.getReadableDatabase();
        Cursor cursor = mySQLiteHelper.showFixtures(sqLiteDatabase);
        fixtureAdapter = new FixtureAdapter(ctx, R.layout.display_fixture_row);

        String date, time, opponent, venue;

        while (cursor.moveToNext()) {
            date = cursor.getString(cursor.getColumnIndex(FixtureContract.FixtureEntry.DATE));
            time = cursor.getString(cursor.getColumnIndex(FixtureContract.FixtureEntry.TIME));
            opponent = cursor.getString(cursor.getColumnIndex(FixtureContract.FixtureEntry.OPPONENT));
            venue = cursor.getString(cursor.getColumnIndex(FixtureContract.FixtureEntry.VENUE));

            Fixture fixture = new Fixture(date, time, opponent, venue);
            publishProgress(fixture);
        }
        return "get_info";
    }

    return null;
}

@Override
protected void onProgressUpdate(Fixture... values) {

    fixtureAdapter.add(values[0]);
}

@Override
protected void onPostExecute(String result) {
    if (result.equals("get_info")) {
        listView.setAdapter(fixtureAdapter);
    } else {
        Toast.makeText(ctx, result, Toast.LENGTH_SHORT).show();
    }

    super.onPostExecute(result);
}
}

当我点击“添加”按钮时,应用会崩溃按钮

package com.example.myacer.clubhub.Manager;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.database.sqlite.SQLiteDatabase;

import com.example.myacer.clubhub.Database.BackGroundTask;
import com.example.myacer.clubhub.Database.MySQLiteHelper;
import com.example.myacer.clubhub.R;


/**
 * Created by My Acer on 10/12/2015.
 */
public class UpdateSchedule extends AppCompatActivity {

    EditText addDate;
    EditText addTime;
    EditText addOpponent;
    EditText addVenue;
    Button addFixtureButton;

    String date, time, opponent, venue;

    MySQLiteHelper mySQLiteHelper;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_updateschedule);

        mySQLiteHelper = new MySQLiteHelper(this);
        addDate = (EditText) findViewById(R.id.fixtureDateEdit);
        addTime = (EditText) findViewById(R.id.fixtureTimeEdit);
        addOpponent = (EditText) findViewById(R.id.fixtureOpponentEdit);
        addVenue = (EditText) findViewById(R.id.fixtureVenueEdit);
        addFixtureButton = (Button) findViewById(R.id.addFixtureButton);

    }


    private void returnToManagerHome() {
        Intent i = new Intent(UpdateSchedule.this, ManagerActivity.class);
        UpdateSchedule.this.startActivity(i);
    }

    //update schedule
    //write fixture to database


    public void addData(View view) {
        date = addDate.getText().toString();
        time = addTime.getText().toString();
        opponent = addOpponent.getText().toString();
        venue = addVenue.getText().toString();

        BackGroundTask backGroundTask = new BackGroundTask(this);
        backGroundTask.execute("add_info", date, time, opponent, venue);
    }
}

非常感谢任何帮助,谢谢

0 个答案:

没有答案