程序闪退,不会用inflater
package com.lexieluv.sleepasynctask;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.widget.ProgressBar;
import android.widget.TextView;
public class Asynctasks extends AsyncTask<String,Integer,Boolean> {
Context context;
private TextView tv;
private ProgressBar pb;
private int i = 1;
LayoutInflater inflater = LayoutInflater.from(context);
@SuppressLint("ResourceType")
//初始化操作
@Override
protected void onPreExecute(){
super.onPreExecute();
tv = (TextView) inflater.inflate(R.id.tv,null);
pb = (ProgressBar) inflater.inflate(R.id.pb,null);
tv.setText("开始第"+i+"个异步任务");
}
//将耗时任务在子线程中完成
@Override
protected Boolean doInBackground(String... strings) {
while(i <= 4){
if(isCancelled()){
break;
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
i++;
publishProgress((i-1)*100/4);
}
return false;
}
//对UI线程进行操作
@Override
protected void onProgressUpdate(Integer... values){
super.onProgressUpdate(values);
if(isCancelled()){
return;
}
if(values != null&values.length > 0){
pb.setProgress(values[0]);
}
if(i < 5){
tv.setText("开始第"+i+"个异步任务");
}else {
tv.setText("已完成");
}
}
//提醒关闭进度条
@Override
protected void onPostExecute(Boolean bboolean){
super.onPostExecute(bboolean);
}
@Override
protected void onCancelled(){
super.onCancelled();
}
}
package com.lexieluv.sleepasynctask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private TextView tv;
private Button btn_1,btn_2;
// private ProgressBar pb;
Asynctasks task;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
setListener();
}
private void initView() {
tv = findViewById(R.id.tv);
btn_1 = findViewById(R.id.btn_1);
btn_2 = findViewById(R.id.btn_2);
// pb = findViewById(R.id.pb);
task = new Asynctasks();
}
private void setListener() {
btn_1.setOnClickListener(this);
btn_2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_1:
task.execute("开始下载");
break;
case R.id.btn_2:
tv.setText("已暂停");
boolean b = task.cancel(true);
if(b){
Toast.makeText(MainActivity.this,"终止成功!",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this,"终止失败!",Toast.LENGTH_SHORT).show();
}
break;
}
}
}
0
收起
正在回答
3回答

1) new的task要放在监听器中,因为一个Asynctasks对象只能执行一次,不能重复execute()。
2)你要new Asynctasks()的时候把那个类中要用到的两个控件,直接传递过去多方便呀。并且你在那个类中无法获得控件,而且你想传Context context时,它也应该在onCreate()之后实例化呀,要不这个Activity都没初始化成功呢哪里来的Context。所以你直接在MainActivity中找到这两个控件,传给Asynctasks就行了。
3)所以在Asynctasks的构造方法你要修改一下,接受这两个控件。

使用它就可以了。祝:学习愉快
LexieMIZUKI
2019-04-25 16:04:07
package com.lexieluv.sleepasynctask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private TextView tv;
private Button btn_1,btn_2;
// private ProgressBar pb;
public Asynctasks task = new Asynctasks(MainActivity.this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
setListener();
}
private void initView() {
tv = findViewById(R.id.tv);
btn_1 = findViewById(R.id.btn_1);
btn_2 = findViewById(R.id.btn_2);
// pb = findViewById(R.id.pb);
}
private void setListener() {
btn_1.setOnClickListener(this);
btn_2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_1:
task.execute("开始下载");
break;
case R.id.btn_2:
tv.setText("已暂停");
boolean b = task.cancel(true);
if(b){
Toast.makeText(MainActivity.this,"终止成功!",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this,"终止失败!",Toast.LENGTH_SHORT).show();
}
break;
}
}
}
package com.lexieluv.sleepasynctask;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.widget.ProgressBar;
import android.widget.TextView;
public class Asynctasks extends AsyncTask<String,Integer,Boolean> {
Context context;
private TextView tv;
private ProgressBar pb;
private int i = 1;
public Asynctasks(Context context){
this.context = context;
}
LayoutInflater inflater = LayoutInflater.from(context);
@SuppressLint("ResourceType")
//初始化操作
@Override
protected void onPreExecute(){
super.onPreExecute();
tv = (TextView) inflater.inflate(R.id.tv,null);
pb = (ProgressBar) inflater.inflate(R.id.pb,null);
tv.setText("开始第"+i+"个异步任务");
}
//将耗时任务在子线程中完成
@Override
protected Boolean doInBackground(String... strings) {
while(i <= 4){
if(isCancelled()){
break;
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
i++;
publishProgress((i-1)*100/4);
}
return false;
}
//对UI线程进行操作
@Override
protected void onProgressUpdate(Integer... values){
super.onProgressUpdate(values);
if(isCancelled()){
return;
}
if(values != null&values.length > 0){
pb.setProgress(values[0]);
}
if(i < 5){
tv.setText("开始第"+i+"个异步任务");
}else {
tv.setText("已完成");
}
}
//提醒关闭进度条
@Override
protected void onPostExecute(Boolean bboolean){
super.onPostExecute(bboolean);
}
@Override
protected void onCancelled(){
super.onCancelled();
}
}
Android网络操作与数据存储2018版
- 参与学习 人
- 提交作业 307 份
- 解答问题 1613 个
本专题是联网及数据处理的必备技能。课程从网络基础知识到线程间协同工作、异步下载处理。介绍了Android内外部文件存储、轻量级数据库SQLite的使用。利用屏幕适配、状态保持、百度地图解决实际问题。
了解课程

恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星