AsyncTask:执行doInBackground()

时间:2017-09-16 18:24:20

标签: java android android-asynctask

我试图创建使用AsyncTask将数据保存到SQLiteDatabase

的简单应用程序

布局:activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context="jvtech.asynctask_practice.MainActivity">

   <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/name"
        android:hint="name"
    />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/college"
        android:hint="college"
    />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/save"
        android:text="save"
    />

</LinearLayout>

主类:MainActivity.java

package jvtech.asynctask_practice;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity
 {
EditText name,college;
String n,c;
Button save;
Context ct;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    name=(EditText)findViewById(R.id.name);
    college=(EditText)findViewById(R.id.college);
    save=(Button)findViewById(R.id.save);
    save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            n=name.getText().toString();
            c=college.getText().toString();
            BackgroundTask backgroundTask=new BackgroundTask(ct);
            backgroundTask.execute("add_item",n,c);
            finish();
        }
    });
}}

TableData.java

package jvtech.asynctask_practice;

import android.provider.BaseColumns;

public final class TableData {
public TableData(){}
public static abstract class TableInfo implements BaseColumns{
    public static final String NAME="name";
    public static final String COLLEGE="college";
    public static final String TABLE_NAME="info_user";
    public static final String DATABASE_NAME="info_reg";
}}

DatabaseOperation.java

package jvtech.asynctask_practice;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseOperations extends SQLiteOpenHelper {

public static final int database_version=1;
public String CREATE_QUERY="CREATE TABLE "+TableData.TableInfo.TABLE_NAME+"("+TableData.TableInfo.NAME+" TEXT,"+TableData.TableInfo.COLLEGE+" TEXT);";

public DatabaseOperations(Context context) {
    super(context, TableData.TableInfo.DATABASE_NAME, null, database_version);
    Log.d("Database Operations","Database created");
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_QUERY);
    Log.d("Database opeartions","Table Created");
}

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

}

public void putInformation(SQLiteDatabase sq,String name,String college)
{
    ContentValues values=new ContentValues();
    values.put(TableData.TableInfo.NAME,name);
    values.put(TableData.TableInfo.COLLEGE,college);
    sq.insert(TableData.TableInfo.TABLE_NAME,null,values);
    Log.d("Database Operations","One Row inserted");
}}

BackgroundTask.java

package jvtech.asynctask_practice;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.AsyncTask;
import android.widget.Toast;

public class BackgroundTask extends AsyncTask<String,String,String> {
Context ct;
BackgroundTask(Context ct)
{
    this.ct=ct;
}
@Override
protected void onPreExecute() {
    super.onPreExecute();
}

@Override
protected String doInBackground(String... params) {
    String method=params[0];
    DatabaseOperations db=new DatabaseOperations(ct);
    if (method.equals("add_item"))
    {
        String n=params[1];
        String c=params[2];
        SQLiteDatabase sq=db.getWritableDatabase();
        db.putInformation(sq,n,c);
    }
    return "one row inserted";
}

@Override
protected void onProgressUpdate(String... values) {
    super.onProgressUpdate(values);
}

@Override
protected void onPostExecute(String s) {
    Toast.makeText(ct,s, Toast.LENGTH_SHORT).show();
}}

错误:

 [use following link to see errors][https://i.stack.imgur.com/7i6Qr.png]

我已经附上了我创建的与项目相关的文件。我希望程序从EditTexts复制数据并使用AsyncTask将其存储在SQLiteDatabse中,但是我收到了一个错误。请帮忙。提前谢谢。

1 个答案:

答案 0 :(得分:0)

您永远不会为ct分配值,因此在您创建null时它是BackgroundTask