What is wrong with my code the progress dialog?
The app works without ProgressDialog.
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new ConnexionFTP(this.getApplicationContext()).execute();
}
}
class ConnexionFTP extends AsyncTask<String, Void, String[]> {
public String modified;
public String time;
public static FTPClient mFTPClient = null;
Context context;
private ProgressDialog pdia;
public ConnexionFTP(Context applicationContext) {
// TODO Auto-generated constructor stub
this.context = applicationContext;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pdia = new ProgressDialog(this.context);
pdia.setMessage("Loading...");
pdia.show();
}
@Override
protected String[] doInBackground(String... params) {
// TODO Auto-generated method stub
mFTPClient = new FTPClient();
try {
mFTPClient.connect("192.168.1.27", 21);
if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {
mFTPClient.login("imane", "aze");
mFTPClient.enterLocalPassiveMode();
String filePath = "imane.sqlite";
time = mFTPClient.getModificationTime(filePath);
File file = new File("/data/data/com.ibm.fr.ftpclientimane/files/MAJ.txt");
if (file.exists()) {
try (BufferedReader buff = new BufferedReader(new FileReader("/data/data/com.ibm.fr.ftpclientimane/files/MAJ.txt"))) {
String line;
line = buff.readLine();
modified = line;
}
} else {
File myFile = new File("/data/data/com.ibm.fr.ftpclientimane/files/MAJ.txt"); //on déclare notre futur fichier
File myDir = new File("/data/data/com.ibm.fr.ftpclientimane/files"); //pour créer le repertoire dans lequel on va mettre notre fichier
Boolean success = true;
if (!myDir.exists()) {
success = myDir.mkdir(); //On crée le répertoire (s'il n'existe pas!!)
}
if (success) {
String data = "valeur lancement négatif";
FileOutputStream output = new FileOutputStream(myFile, true); //le true est pour écrire en fin de fichier, et non l'écraser
output.write(data.getBytes());
modified = data;
}
}
}
int comparaison = time.compareTo(modified);
if (comparaison < 0) {
try {
FileOutputStream output = context.openFileOutput("imane.sqlite",
Context.MODE_PRIVATE);
Log.d("monerreur", "output");
// récupération du fichier sur le serveur
mFTPClient.retrieveFile("imane.sqlite", output);
output.close();
mFTPClient.logout();
mFTPClient.disconnect();
FileWriter fw = new FileWriter("/data/data/com.ibm.fr.ftpclientimane/files/MAJ.txt");
fw.write(time);
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
} else {
mFTPClient.logout();
mFTPClient.disconnect();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(final String[] results) {
super.onPostExecute(results);
pdia.dismiss();
}
}
Logcat in android studio:
06-24 17:48:53.449 3333-3333/com.ibm.fr.ftpclientimane E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ibm.fr.ftpclientimane, PID: 3333
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ibm.fr.ftpclientimane/com.ibm.fr.ftpclientimane.MainActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRootImpl.setView(ViewRootImpl.java:566)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:272) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:298)
at com.ibm.fr.ftpclientimane.ConnexionFTP.onPreExecute(MainActivity.java:49)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
at android.os.AsyncTask.execute(AsyncTask.java:535)
at com.ibm.fr.ftpclientimane.MainActivity.onCreate(MainActivity.java:24)
at android.app.Activity.performCreate(Activity.java:5937)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)