从AsyncTask更新视图



我想从android中的mysql数据库中检索数据,并显示在textview 中

它显示错误

E/AndroidRuntime:致命异常:main流程:com.example.elearnersarena.practicemysql,PID:23452java.lang.NullPointerException:试图对null对象引用调用虚拟方法"void android.widget.TextView.setText(java.lang.CharSequence)"网址:com.example.elearnersarenae.practicemysql.BackgroundAsyncTask.onPostExecute(BackgroundAsyncTask.java:110)网址:com.example.elearnersarenae.practicemysql.BackgroundAsyncTask.onPostExecute(BackgroundAsyncTask.java:26)在android.os.AsyncTask.fining(AsyncTask.java:632)在android.os.AsyncTask.access上$600(AsyncTask.java:177)在android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)在android.os.Handler.dispatchMessage(Handler.java:102)在android.os.Looper.loop(Looper.java:135)在android.app.ActivityThread.main(ActivityThreads.java:5349)位于java.lang.reflect.Method.ioke(本机方法)位于java.lang.reflect.Method.ioke(Method.java:372)网址:com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)网址:com.android.internal.os.ZygoteInit.main(ZygoteNit.java:703)

这是后台异步任务代码

public class BackgroundAsyncTask extends AsyncTask<String, Void, String> {
Context ctx;
TextView tv;
BackgroundAsyncTask(Context ctx) {
this.ctx = ctx;
}
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
String insert_url="http://192.168.10.3:81/android_exam_practice/insert.php";
String select_url="http://192.168.10.3:81/android_exam_practice/select.php";
String method=params[0];
if(method.equals("insert"))
{
String name=params[1];
String email=params[2];
try {
URL url=new URL(insert_url);
HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
OutputStream outputStream=httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
String data= URLEncoder.encode("name","UTF-8")+"="+URLEncoder.encode(name,"UTF-8")+"&"+
URLEncoder.encode("email","UTF-8")+"="+URLEncoder.encode(email,"UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
outputStream.close();
InputStream inputStream=httpURLConnection.getInputStream();
inputStream.close();
return "data inserted ";
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
else if(method.equals("select"))
{
//   String Name=params[1];
// String Email=params[2];
try {
URL url=new URL(select_url);
HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("post");
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
//OutputStream outputStream=httpURLConnection.getOutputStream();
//BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
//String data =URLEncoder.encode("");
InputStream inputStream=httpURLConnection.getInputStream();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String response ="";
String line ="";
while((line=bufferedReader.readLine())!=null)
{
response+=line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String aVoid) {
super.onPostExecute(aVoid);
// Toast.makeText(ctx, aVoid, Toast.LENGTH_SHORT).show();
tv.setText(aVoid);
}

}

您的解决方案就在这里

onPostExecute()方法中初始化textview

@Override
protected void onPostExecute(String aVoid) {
super.onPostExecute(aVoid);
// Toast.makeText(ctx, aVoid, Toast.LENGTH_SHORT).show();
tv = findViewById(R.id.textView); 
tv.setText(aVoid);
}
应用程序中的

BackgroundAsyncTask是UI线程以外的线程,因此如果需要更新任何视图,您可以在Activity/Fragment中创建公共静态方法,并使用runOnUiThread()方法将此方法调用为:

runOnUiThread(new Runnable() {
public void run() {
// call your method here.
}
});

使用Handler查看此项以了解有关Handler的的更多信息

从更改BackgroundAsyncTask的构造函数

BackgroundAsyncTask(Context ctx) {
this.ctx = ctx;
}

BackgroundAsyncTask(Context ctx, TextView tv) {
this.ctx = ctx;
this.tv = tv;
}

因此,当您在活动中初始化BackgroundAsyncTask时,而不是:

new BackgroundAsyncTask(this)

写入

new BackgroundAsyncTask(this, tv)

其中tv是要更新的活动的TextView

最新更新