Reputation: 1364
I want to show ProgressDialog
in OnClick()
method in my Activity
. I am doing like the following, but there an error. What can be the fix?
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)
bSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (etTo.getText().toString().compareTo("") == 0)
Toast.makeText(getApplicationContext(),
"no sender specified", Toast.LENGTH_SHORT).show();
else {
ProgressDialog progress = new ProgressDialog(getApplicationContext());
progress.setTitle("Sending Mail");
progress.setMessage("Please Wait...");
progress.show();
int flag = sendMail(etTo.getText().toString(), etCc.getText()
.toString(), etBcc.getText().toString(), etSubject
.getText().toString(), etMessage.getText()
.toString(), etSign.getText().toString(), attachlst);
// To dismiss the dialog
progress.dismiss();
}
}
});
Upvotes: 0
Views: 157
Reputation: 914
user activity context in place of application context
wrong :ProgressDialog progress = new ProgressDialog(getApplicationContext());
correct :ProgressDialog progress = new ProgressDialog(ActvityName.this);
Upvotes: 0
Reputation: 23638
Just remove the progress.dismiss();
in your else part, because as you show()
your dialog and after that directly you are dismissing it ,So as your dialog will be shown at the same time it will be dismissed and you won't get idea of it.
So i would suggest you to use the AsyncTask
to show and remove your dialog as below:
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();
}
}
Upvotes: 1
Reputation: 1675
try this...
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>
Upvotes: 0
Reputation: 3703
At ProgressDialog initialization replace
ProgressDialog progress = new ProgressDialog(getApplicationContext());
to :
ProgressDialog progress = new ProgressDialog(YourActivity.this);
Upvotes: 0
Reputation: 18933
Change this from
ProgressDialog progress = new ProgressDialog(getApplicationContext());
to
ProgressDialog progress = new ProgressDialog(YourActivityName.this);
because getApplicationContext()
is a Context
of your Application
while YourActivityName.this
is a Context
of your particular class or Activity
.
Upvotes: 0