我有一个问题: 我想方法exprrasionCalculatePoints在一个胎面计算,当它计算时,必须有进度条显示进度。当计算点数时,我绘制一个图表。但是有两个问题,当我按下屏幕而不是计算点数时,进度条停止 第二个问题是:
graph = new GraphPanel(getApplicationContext());
setContentView(graph);
这两行必须在一个metod计算全部之前执行。 我该如何解决这两个问题?
源代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
final ProgressDialog ringProgressDialog = ProgressDialog.show(this,
"Please wait ...", "Downloading Image ...", true);
setContentView(R.layout.drawsurface);
ringProgressDialog.setCancelable(true);
new Thread(new Runnable() {
@Override
public void run() {
try {
Calculator.expr[0].exprasionCalculatePoints();
} catch (Exception e) {
Log.e(e.getMessage(), e.getMessage());
}
ringProgressDialog.dismiss();
graph = new GraphPanel(getApplicationContext());
setContentView(graph);
}
}).start();
}
}
logcat的
08-11 04:56:37.253: E/AndroidRuntime(2681): FATAL EXCEPTION: Thread-149
08-11 04:56:37.253: E/AndroidRuntime(2681): Process: com.example.mycalc, PID: 2681
08-11 04:56:37.253: E/AndroidRuntime(2681): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
08-11 04:56:37.253: E/AndroidRuntime(2681): at android.os.Handler.<init>(Handler.java:200)
08-11 04:56:37.253: E/AndroidRuntime(2681): at android.os.Handler.<init>(Handler.java:114)
08-11 04:56:37.253: E/AndroidRuntime(2681): at android.view.SurfaceView$1.<init>(SurfaceView.java:118)
08-11 04:56:37.253: E/AndroidRuntime(2681): at android.view.SurfaceView.<init>(SurfaceView.java:118)
08-11 04:56:37.253: E/AndroidRuntime(2681): at com.calculator.grap.GraphPanel.<init>(GraphPanel.java:31)
08-11 04:56:37.253: E/AndroidRuntime(2681): at com.calculator.grap.GraphingTab$1.run(GraphingTab.java:58)
08-11 04:56:37.253: E/AndroidRuntime(2681): at java.lang.Thread.run(Thread.java:841)
答案 0 :(得分:2)
您需要在UI线程上运行它们。尝试这样的事情:
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
final ProgressDialog ringProgressDialog = ProgressDialog.show(this,
"Please wait ...", "Downloading Image ...", true);
setContentView(R.layout.drawsurface);
ringProgressDialog.setCancelable(true);
new Thread(new Runnable() {
@Override
public void run() {
try {
Calculator.expr[0].exprasionCalculatePoints();
} catch (Exception e) {
Log.e(e.getMessage(), e.getMessage());
}
runOnUiThread(display);
}
}).start();
}
Runnable display = new Runnable() {
@Override
public void run () {
ringProgressDialog.dismiss();
graph = new GraphPanel(getApplicationContext());
setContentView(graph);
}
}
答案 1 :(得分:0)
progressBar是一个UI组件。你不能在一个线程内调用它。尝试使用runOnUiThread