SQlite数据库操作和AsyncTask [重复]
Posted
技术标签:
【中文标题】SQlite数据库操作和AsyncTask [重复]【英文标题】:SQlite dataabase operation and AsyncTask [duplicate] 【发布时间】:2017-03-26 03:06:30 【问题描述】:我正在学习 AsyncTask 类,并通过以下示例进行练习。我有一个 UI,它有一个 EditText(用于数据输入)、一个带有 5 个单选按钮的单选组和一个按钮(提交)。这个想法是,当用户选择单选按钮并向 EDITTEXT 输入一个值时。单击按钮后,信息将转到数据库表。数据库表被创建,整个代码工作。但是我想用AsyncTask在后台运行数据库操作功能。应用 AsyncTask 类后出现问题。请帮帮我。
这是没有 AsyncTask 的代码:
package jhapps.com.myspending;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class Child extends AppCompatActivity
private static Button btnsubmit;
private static RadioGroup rgChild;
private static RadioButton rbChild;
private static EditText dollarAmount;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_child);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
);
DataSubmitChild();
private void DataSubmitChild()
btnsubmit=(Button)findViewById(R.id.btnSubmitChild);
btnsubmit.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
rgChild = (RadioGroup) findViewById(R.id.rgChildren);
int selectedRbChild = rgChild.getCheckedRadioButtonId();
rbChild = (RadioButton) findViewById(selectedRbChild);
String rbChildF = rbChild.getText().toString();
dollarAmount = (EditText) findViewById(R.id.amountEnterChild);
String dollarAmountF = dollarAmount.getText().toString();
float dollarAmountFloat = Float.parseFloat(dollarAmountF);
DataBaseHelper dataBaseHelper = new DataBaseHelper(Child.this);
if (dollarAmountFloat != 0 & rbChildF != "")
int itemID;
switch (rbChildF)
case "Daycare":itemID=53;
dataBaseHelper.insertDataSpending(itemID, dollarAmountFloat);
break;
case "Lessons and Activities": itemID=54;
dataBaseHelper.insertDataSpending(itemID, dollarAmountFloat);
break;
case "Allowance":itemID=55;
dataBaseHelper.insertDataSpending(itemID, dollarAmountFloat);
break;
case "School Supplies and Fees" :itemID=56;
dataBaseHelper.insertDataSpending(itemID, dollarAmountFloat);
break;
case "Babysitting":itemID=57;
dataBaseHelper.insertDataSpending(itemID, dollarAmountFloat);
break;
Toast.makeText(Child.this,"Your spending is recorded!",Toast.LENGTH_LONG).show();
dollarAmount.setText("");
rgChild.clearCheck();
else
Toast.makeText(Child.this,"Please choose an Item",Toast.LENGTH_LONG).show();
);
这是 AsyncTask 不起作用的代码:
package jhapps.com.myspending;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class Child extends AppCompatActivity
private static Button btnsubmit;
private static RadioGroup rgChild;
private static RadioButton rbChild;
private EditText dollarAmount;
Context ctx;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_child);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
);
//DataSubmitChild();
rgChild = (RadioGroup) findViewById(R.id.rgChildren);
int selectedRbChild = rgChild.getCheckedRadioButtonId();
rbChild = (RadioButton) findViewById(selectedRbChild);
dollarAmount = (EditText) findViewById(R.id.amountEnterChild);
btnsubmit=(Button)findViewById(R.id.btnSubmitChild);
btnsubmit.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
BackgroundTask backgroundTask=new BackgroundTask();
backgroundTask.execute();
);
dollarAmount.setText("");
rgChild.clearCheck();
private class BackgroundTask extends AsyncTask<Void,Void,Void>
String rbChildF = rbChild.getText().toString();
String dollarAmountF = dollarAmount.getText().toString();
float dollarAmountFloat = Float.parseFloat(dollarAmountF);
@Override
protected Void doInBackground(Void... params)
DataBaseHelper dataBaseHelper = new DataBaseHelper(getBaseContext());
if (dollarAmountFloat != 0 & rbChildF != "")
int itemID;
switch (rbChildF)
case "Daycare":itemID=53;
dataBaseHelper.insertDataSpending(itemID, dollarAmountFloat);
break;
case "Lessons and Activities": itemID=54;
dataBaseHelper.insertDataSpending(itemID, dollarAmountFloat);
break;
case "Allowance":itemID=55;
dataBaseHelper.insertDataSpending(itemID, dollarAmountFloat);
break;
case "School Supplies and Fees" :itemID=56;
dataBaseHelper.insertDataSpending(itemID, dollarAmountFloat);
break;
case "Babysitting":itemID=57;
dataBaseHelper.insertDataSpending(itemID, dollarAmountFloat);
break;
Toast.makeText(Child.this,"Your spending is recorded!",Toast.LENGTH_LONG).show();
else
Toast.makeText(Child.this,"Please choose an Item",Toast.LENGTH_LONG).show();
return null;
这是错误信息:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: jhapps.com.myspending, PID: 27613
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.RadioButton.getText()' on a null object reference
at jhapps.com.myspending.Child$BackgroundTask.<init>(Child.java:62)
at jhapps.com.myspending.Child$BackgroundTask.<init>(Child.java:0)
at jhapps.com.myspending.Child$2.onClick(Child.java:54)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
【问题讨论】:
【参考方案1】:这些视图都是空的……
private class BackgroundTask extends AsyncTask<Void,Void,Void>
// Don't put these here...
String rbChildF = rbChild.getText().toString();
String dollarAmountF = dollarAmount.getText().toString();
float dollarAmountFloat = Float.parseFloat(dollarAmountF);
如果你真的需要这些值,那就做一个构造函数
private class BackgroundTask extends AsyncTask<Void,Void,Void>
String rbChildF, dollarAmountF;
float dollarAmount;
public BackgroundTask(String radio, String dollarAmtF, float dollars)
this.rbChildF = radio;
this.dollarAmountF = dollarAmtF;
this.dollarAmount = dollars;
并将这些值传入
String rbChildF = rbChild.getText().toString();
String dollarAmountF = dollarAmount.getText().toString();
float dollarAmountFloat = Float.parseFloat(dollarAmountF);
new BackgroundTask(rbChildF, dollarAmountF, dollarAmountFloat).execute();
【讨论】:
非常感谢!!这是一个很大的帮助!这对我理解这个概念很有帮助。【参考方案2】:asyctask 是一个独立的脚本块,它不知道您的视图值。 要让 asynctask 知道您的视图 edittext 或 radio 值,您应该将父视图传递给 asyctask 构造函数。比在构造函数主体中使用 findviewbyid 来检索将在 doinbackground 方法中使用的值。
记住。构造函数和 onpostexecute 在主线程上执行。 asyctask 包含在 doinbackground 中发生的事情。
【讨论】:
感谢您的概念。它有助于我理解类和一般的 android。以上是关于SQlite数据库操作和AsyncTask [重复]的主要内容,如果未能解决你的问题,请参考以下文章
Android AsyncTask 和 SQLite 数据库实例
如果我在 AsyncTask 线程中插入数据,如何从 UI 线程访问 SQLite 数据库?
在 AsyncTask doInBackground 中创建 140,000 行 SQLite 数据库需要很多很多分钟
Android PushNotification IntentService 并调用 AsyncTask 函数从服务器数据库获取数据并更新 sqlite 数据库