android空指针异常,大体是在onProgressUpdate方法里面 10
packagecom.example.com.shen.bar;importandroid.support.v7.app.ActionBarActivity;import...
package com.example.com.shen.bar;
import android.support.v7.app.ActionBarActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
public class MainActivity extends ActionBarActivity {
ProgressBar bar;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bar = (ProgressBar) this.findViewById(R.id.progress_horizontal);
button = (Button) this.findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new MyTask().execute();
}
});
}
class MyTask extends AsyncTask<Void, Integer, Void> {
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
Log.i("-->update", "-->update");
bar.setProgress(values[0]);
Log.i("-->update1", "-->update1");
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
int i = 1;
while (i <= 100) {
publishProgress(i);
i++;
try {
Thread.sleep(1000);
} catch (Exception e) {
// TODO: handle exception
}
}
return null;
}
}
} 展开
import android.support.v7.app.ActionBarActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
public class MainActivity extends ActionBarActivity {
ProgressBar bar;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bar = (ProgressBar) this.findViewById(R.id.progress_horizontal);
button = (Button) this.findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new MyTask().execute();
}
});
}
class MyTask extends AsyncTask<Void, Integer, Void> {
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
Log.i("-->update", "-->update");
bar.setProgress(values[0]);
Log.i("-->update1", "-->update1");
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
int i = 1;
while (i <= 100) {
publishProgress(i);
i++;
try {
Thread.sleep(1000);
} catch (Exception e) {
// TODO: handle exception
}
}
return null;
}
}
} 展开
3个回答
展开全部
int current = 0;
@Override
protected Void doInBackground(Void... params) {
do {
Log.d("handleMessage","id:"+Thread.currentThread().getId()+" name:"+Thread.currentThread().getName());
current+=10;
try {
publishProgress(current); //这里的参数类型是 AsyncTask<Void, Integer, Void>中的Integer决定的,在onProgressUpdate中可以得到这个值去更新UI主线程,这里是异步线程
Thread.sleep(1000);
if(mProgressBar.getProgress() >= 100){
break;
}
} catch (Exception e) {
}
} while (mProgressBar.getProgress() <= 100);
return null;
}
这样写试试?
@Override
protected Void doInBackground(Void... params) {
do {
Log.d("handleMessage","id:"+Thread.currentThread().getId()+" name:"+Thread.currentThread().getName());
current+=10;
try {
publishProgress(current); //这里的参数类型是 AsyncTask<Void, Integer, Void>中的Integer决定的,在onProgressUpdate中可以得到这个值去更新UI主线程,这里是异步线程
Thread.sleep(1000);
if(mProgressBar.getProgress() >= 100){
break;
}
} catch (Exception e) {
}
} while (mProgressBar.getProgress() <= 100);
return null;
}
这样写试试?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在onProgressUpdate加上findViewById试试
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询