Java Android进度对话框未关闭
我想在页面布局中显示进度对话框。我在下面的代码中实现了。“进度”对话框未关闭,它将继续运行。当我单击上一页中的图像时,它将导航到下一个布局,我希望此布局在从服务器下载所有数据之前显示“进度”对话框,并在当前布局的列表中显示它。“进度”对话框显示,列表显示在后台,但“进度”对话框继续运行,并且不会关闭。我不知道我错在哪里。请帮忙Java Android进度对话框未关闭,java,android,progressdialog,Java,Android,Progressdialog,我想在页面布局中显示进度对话框。我在下面的代码中实现了。“进度”对话框未关闭,它将继续运行。当我单击上一页中的图像时,它将导航到下一个布局,我希望此布局在从服务器下载所有数据之前显示“进度”对话框,并在当前布局的列表中显示它。“进度”对话框显示,列表显示在后台,但“进度”对话框继续运行,并且不会关闭。我不知道我错在哪里。请帮忙 ProgressDialog pg; String[] ar; public void onCreate(Bundle savedInstanceState) {
ProgressDialog pg;
String[] ar;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.filename);
pg=ProgressDialog.show(this, "ABC", "Downloading .....",true);
Thread dt= new Thread(new Runnable()
{
public void run()
{
try
{
String addr=Util.url;
URL url = new URL(urlname);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(5000);
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader r = new BufferedReader(new InputStreamReader(in));
String x = "";
String total = "";
int i=0;
ArrayList<String> content = new ArrayList();
while((x = r.readLine()) != null)
{
content.add(x);
}
in.close();
r.close();
ar= content.toArray(new String[content.size()]);
}
catch(Exception e1){
handler.sendEmptyMessage(0);
}
}
});
dt.start();
try{
dt.join();
}catch(Exception e){
handler.sendEmptyMessage(0);
}
try{
if(ar[0].toString().trim()!="")
{
android.view.Display display1 = ((android.view.WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
TableLayout tbl1 = (TableLayout)findViewById(R.id.tableLayout2);
TableRow newRow1 = (TableRow) new TableRow(this);
TextView txt=(TextView) new TextView(this);
txt.setText("No");
txt.setGravity(Gravity.LEFT);
txt.setTextColor(Color.RED);
txt.setTextSize(18);
TextView txt1=(TextView) new TextView(this);
txt1.setText("NAME");
txt1.setTextColor(Color.RED);
txt1.setTextSize(18);
txt1.setGravity(3);
TextView txt2=(TextView) new TextView(this);
txt2.setText("DATE");
txt2.setTextColor(Color.RED);
txt2.setTextSize(18);
txt.setGravity(3);
TextView txt3=(TextView) new TextView(this);
txt3.setText("VALUE");
txt3.setTextColor(Color.RED);
txt3.setTextSize(18);
txt3.setGravity(Gravity.RIGHT);
txt.setWidth((int)(display1.getWidth()/4));
txt1.setWidth((int)(display1.getWidth()/4));
txt3.setWidth((int)(display1.getWidth()/4));
txt2.setWidth((int)(display1.getWidth()/4));
newRow1.addView(txt2);
newRow1.addView(txt);
newRow1.addView(txt1);
newRow1.addView(txt3);
tbl1.addView(newRow1);
for(int t=0;t<(ar.length);t++)
{
android.view.Display display = ((android.view.WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
TableLayout tbl = (TableLayout)findViewById(R.id.tableLayout1);
TableRow newRow = (TableRow) new TableRow(this);
newRow.setClickable(true);
TextView tx=(TextView) new TextView(this);
String temp=ar[t].toString();
tx.setText(temp);
tx.setTextColor(Color.WHITE);
tx.setGravity(Gravity.LEFT);
tx.setTextSize(15);
t=t+1;
TextView tx1=new TextView(this);
tx1.setText(ar[t].toString());
tx1.setGravity(Gravity.LEFT);
tx1.setTextColor(Color.WHITE);
tx1.setTextSize(15);
t=t+1;
TextView tx2=new TextView(this);
tx2.setText(ar[t].toString());
tx2.setGravity(Gravity.LEFT);
tx2.setTextColor(Color.WHITE);
tx2.setTextSize(15);
t=t+1;
TextView tx3=new TextView(this);
tx3.setText(ar[t].toString());
tx3.setGravity(Gravity.RIGHT);
tx3.setTextColor(Color.WHITE);
tx3.setTextSize(15);
tx3.setWidth((int)(display.getWidth()/4));
tx.setWidth((int)(display.getWidth()/4));
tx1.setWidth((int)(display.getWidth()/4));
tx2.setWidth((int)(display.getWidth()/4));
newRow.addView(tx);
newRow.addView(tx2);
newRow.addView(tx1);
newRow.addView(tx3);
newRow.setId(t);
tbl.addView(newRow);
}
}
}
catch(Exception e){
pg.dismiss();
handler.sendEmptyMessage(0);
}
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
Toast.makeText(this,"Network not available!.... ", Toast.LENGTH_LONG).show();
}
};
ProgressDialog-pg;
字符串[]ar;
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.filename);
pg=ProgressDialog.show(这是“ABC”,“下载…”,true);
线程dt=新线程(新可运行()
{
公开募捐
{
尝试
{
字符串addr=Util.url;
URL=新URL(URL名称);
HttpURLConnection urlConnection=(HttpURLConnection)url.openConnection();
urlConnection.setConnectTimeout(5000);
InputStream in=new BufferedInputStream(urlConnection.getInputStream());
BufferedReader r=新的BufferedReader(新的InputStreamReader(in));
字符串x=“”;
字符串总数=”;
int i=0;
ArrayList内容=新建ArrayList();
而((x=r.readLine())!=null)
{
内容.添加(x);
}
in.close();
r、 close();
ar=content.toArray(新字符串[content.size()]);
}
捕获(异常e1){
handler.sendEmptyMessage(0);
}
}
});
dt.start();
试一试{
dt.join();
}捕获(例外e){
handler.sendEmptyMessage(0);
}
试试{
如果(ar[0].toString().trim()!=“”)
{
android.view.Display display1=((android.view.WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
TableLayout tbl1=(TableLayout)findViewById(R.id.tableLayout2);
TableRow newRow1=(TableRow)新TableRow(this);
TextView txt=(TextView)新建TextView(此);
txt.setText(“否”);
txt.setGravity(Gravity.LEFT);
txt.setTextColor(Color.RED);
txt.setTextSize(18);
TextView txt1=(TextView)新建TextView(此);
txt1.setText(“名称”);
txt1.setTextColor(Color.RED);
txt1.setTextSize(18);
txt1.设置重力(3);
TextView txt2=(TextView)新建TextView(此);
txt2.setText(“日期”);
txt2.setTextColor(Color.RED);
txt2.setTextSize(18);
设置重力(3);
TextView txt3=(TextView)新建TextView(此);
txt3.setText(“值”);
txt3.setTextColor(Color.RED);
txt3.setTextSize(18);
txt3.setGravity(Gravity.RIGHT);
setWidth((int)(display1.getWidth()/4));
setWidth((int)(display1.getWidth()/4));
setWidth((int)(display1.getWidth()/4));
setWidth((int)(display1.getWidth()/4));
newRow1.addView(txt2);
newRow1.addView(txt);
newRow1.addView(txt1);
newRow1.addView(txt3);
tbl1.addView(newRow1);
对于(int t=0;t将这一行添加到您想要关闭对话框的位置
if(pg.isShowing())pg.dismiss();
将这一行添加到您想要关闭对话框的位置
if(pg.isShowing())pg.dismiss();
您正在关闭Try子句异常的ProgressDialog
catch(Exception e){
pg.dismiss();
handler.sendEmptyMessage(0);
}
这就是为什么它毫无例外地不会关闭的原因您正在关闭关于Try子句异常的ProgressDialog
catch(Exception e){
pg.dismiss();
handler.sendEmptyMessage(0);
}
这就是为什么毫无例外这不会关闭的原因。我已经检查了你的代码,只有当你的程序抛出异常时,你的进度对话框才会退出。把它放在catch块的外面(catch块之后)
以下代码
catch(Exception e){
pg.dismiss();
handler.sendEmptyMessage(0);
}
应改为
catch(Exception e){
handler.sendEmptyMessage(0);
}finally{
pg.dismiss();
}
如果上述方法不起作用,请尝试将finally块移到线程的run方法内部,如下所示
Thread dt= new Thread(new Runnable()
{
public void run()
{
try
{
String addr=Util.url;
URL url = new URL(urlname);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(5000);
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader r = new BufferedReader(new InputStreamReader(in));
String x = "";
String total = "";
int i=0;
ArrayList<String> content = new ArrayList();
while((x = r.readLine()) != null)
{
content.add(x);
}
in.close();
r.close();
ar= content.toArray(new String[content.size()]);
}
catch(Exception e1){
handler.sendEmptyMessage(0);
}finally{
pg.dismiss();
}
}
});
Thread dt=新线程(new Runnable()
{
公开募捐
{
尝试
{
字符串addr=Util.url;
URL=新URL(URL名称);
HttpURLConnection urlConnection=(HttpURLConnection)url.openConnection();
urlConnection.setConnectTimeout(5000);
InputStream in=new BufferedInputStream(urlConnection.getInputStream());
BufferedReader r=新的BufferedReader(新的InputStreamReader(in));
字符串x=“”;
字符串总数=”;
int i=0;
ArrayList内容=新建ArrayList();
而((x=r.readLine())!=null)
{
内容.添加(x);
}
in.close();
r、 close();
ar=content.toArray(新字符串[content.size()]);
}
捕获(异常e1){
handler.sendEmptyMessage(0);
}最后{
第()页;
}
}
});
我已经看完了你的代码,当且仅当你的程序抛出异常时,你的进度对话框才会退出。把它放在catch块的外面(在catch块之后)
以下代码
catch(Exception e){
pg.dismiss();
handler.sendEmptyMessage(0);
}
应改为
catch(Exception e){
handler.sendEmptyMessage(0);
}finally{
pg.dismiss();
}
如果上述方法不起作用,请尝试将finally块移到线程的run方法内部,如下所示
Thread dt= new Thread(new Runnable()
{
public void run()
{
try
{
String addr=Util.url;
URL url = new URL(urlname);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(5000);
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader r = new BufferedReader(new InputStreamReader(in));
String x = "";
String total = "";
int i=0;
ArrayList<String> content = new ArrayList();
while((x = r.readLine()) != null)
{
content.add(x);
}
in.close();
r.close();
ar= content.toArray(new String[content.size()]);
}
catch(Exception e1){
handler.sendEmptyMessage(0);
}finally{
pg.dismiss();
}
}
});
Thread dt=新线程(new Runnable()
{
公开募捐
Thread thread = new Thread() {
public void run () {
try
{
pg.show();
//long running task
}
catch(){
}
handler.post(new Runnable() {
@Override
public void run() {
//code for Update UI after the long running task
// dismiss the progress dialog on UI thread
pg.dismiss();
}
});
}
};
thread.start();