按钮上的显示进度对话框单击

时间:2014-04-24 05:28:12

标签: android progressdialog

我想在我ProgressDialog的{​​{1}}方法中显示OnClick()。我做的如下,但有一个错误。可以解决什么问题?


Activity

04-24 10:52:46.241: E/AndroidRuntime(21846): FATAL EXCEPTION: main
04-24 10:52:46.241: E/AndroidRuntime(21846): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
04-24 10:52:46.241: E/AndroidRuntime(21846):    at android.view.ViewRoot.setView(ViewRoot.java:561)
04-24 10:52:46.241: E/AndroidRuntime(21846):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
04-24 10:52:46.241: E/AndroidRuntime(21846):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
04-24 10:52:46.241: E/AndroidRuntime(21846):    at android.app.Dialog.show(Dialog.java:265)
04-24 10:52:46.241: E/AndroidRuntime(21846):    at com.wamiq.test_layout.ComposeMessage$4.onClick(ComposeMessage.java:237)
04-24 10:52:46.241: E/AndroidRuntime(21846):    at android.view.View.performClick(View.java:2532)

5 个答案:

答案 0 :(得分:1)

只需删除你的其他部分中的progress.dismiss();,因为当你show()对话时,你直接将其解雇,所以当你的对话框同时出现时它会被解雇你不会明白它。

所以我建议您使用AsyncTask来显示和删除对话框,如下所示:

class progressdialog extends AsyncTask<String, Integer, String> {

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
            ProgressDialog progress = new ProgressDialog(YourActivityname.this);
        progress.setTitle("Sending Mail");
                    progress.setMessage("Please Wait...");
                  progress.show();

    }

    @Override
    protected String doInBackground(String... params) {
          int flag = sendMail(etTo.getText().toString(), etCc.getText()
                .toString(), etBcc.getText().toString(), etSubject
                .getText().toString(), etMessage.getText()
                .toString(), etSign.getText().toString(), attachlst);
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
                 progress.dismiss();
          }
   }

答案 1 :(得分:0)

更改此内容
ProgressDialog progress = new ProgressDialog(getApplicationContext());

ProgressDialog progress = new ProgressDialog(YourActivityName.this);

因为getApplicationContext()Context的{​​{1}}而ApplicationYourActivityName.this的{​​{1}}。

答案 2 :(得分:0)

在ProgressDialog初始化替换

ProgressDialog progress = new ProgressDialog(getApplicationContext());

到:

ProgressDialog progress = new ProgressDialog(YourActivity.this);

答案 3 :(得分:0)

试试这个......

MainActivity.java

package com.example.progressdialogpractice;

import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    private ProgressDialog progressDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button a =(Button)findViewById(R.id.button1);
a.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub

    Intent intent = new Intent();
    intent.setComponent(new ComponentName("com.example.progressdialogpractice", "com.example.progressdialogpractice.SubActivity"));
    startActivity(intent);

}});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

SubActivity.java

package com.example.progressdialogpractice;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;

public class SubActivity extends Activity {
    private ProgressDialog progressDialog;
    private Handler UIhandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
UIhandler = new Handler();
setContentView(R.layout.sub);
new LoginProgressTask().execute(1);

}

class LoginProgressTask extends AsyncTask<Integer, Integer, Boolean> {
 @Override
 protected Boolean doInBackground(Integer...a) {
   try {
     Thread.sleep(4000);  // Do your real work here
   } catch (InterruptedException e) {
     e.printStackTrace();
   }
   return Boolean.TRUE;   // Return your real result here
 }
 @Override
 protected void onPreExecute() {
 UIhandler.post(new Runnable() {
           @Override
           public void run() {
progressDialog = ProgressDialog.show( SubActivity.this,"wait","downloading");
           }
       });
 }
 @Override
 protected void onPostExecute(Boolean result) {
   // result is the value returned from doInBackground

 UIhandler.post(new Runnable() {
           @Override
           public void run() {
     progressDialog.dismiss();
             Toast.makeText(SubActivity.this, "Hello", Toast.LENGTH_SHORT).show();
             }
       });
 }
}   
}

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="58dp"
        android:text="Button" />

</RelativeLayout>

sub.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <TextView
        android:id="@+id/sub"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fuck you dipshit" />



</RelativeLayout>

答案 4 :(得分:0)

用户活动上下文代替应用程序上下文

wrong :ProgressDialog progress = new ProgressDialog(getApplicationContext()); 
 correct  :ProgressDialog progress = new ProgressDialog(ActvityName.this);