Android ProgressDialog 不能与 AsyncTask 协同工作
Android ProgressDialog not working together with AsyncTask
我正在尝试 运行 另一个线程上的函数。不幸的是,它只是继续前进而没有解雇。如果 运行 函数中没有 Try catch,它就可以工作。我认为它可能是我在函数内部的 AsyncTask。
我也发现了这个错误
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
这是我的代码
btnSubmit.setOnClickListener(new View.OnClickListener() {
InputStream is = null;
@Override
public void onClick(View v) {
pDialog = ProgressDialog.show(AvvikelseActivity.this, "Registrerar", "Vänta");
//Start the function here
//start a new thread to process job
new Thread(new Runnable() {
@Override
public void run() {
//heavy job here
//send message to main thread
try {
Bitmap immage = null;
//preapare the image
immage = BitmapFactory.decodeFile(imgurl);
final int maxSize = 960;
int outWidth;
int outHeight;
int inWidth = immage.getWidth();
int inHeight = immage.getHeight();
if (inWidth > inHeight) {
outWidth = maxSize;
outHeight = (inHeight * maxSize) / inWidth;
} else {
outHeight = maxSize;
outWidth = (inWidth * maxSize) / inHeight;
}
Bitmap resizedBitmap = Bitmap.createScaledBitmap(immage, outWidth, outHeight, false);
//initiate string imagedata, which will be the string for the actual image
String imagedata = null;
//encode the image
imagedata = encodeTobase64(immage);
//Setting nameValuePairs
final List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
//adding the string variables
nameValuePairs.add(new BasicNameValuePair("reporter", reporter));
nameValuePairs.add(new BasicNameValuePair("department", department));
nameValuePairs.add(new BasicNameValuePair("errortype", errortype));
nameValuePairs.add(new BasicNameValuePair("title", title));
nameValuePairs.add(new BasicNameValuePair("description", description));
nameValuePairs.add(new BasicNameValuePair("percaution", percaution));
nameValuePairs.add(new BasicNameValuePair("imgurl", imgurl));
nameValuePairs.add(new BasicNameValuePair("dataset", dataset));
nameValuePairs.add(new BasicNameValuePair("image", imagedata));
nameValuePairs.add(new BasicNameValuePair("phoneid", android_id));
nameValuePairs.add(new BasicNameValuePair("typ", TAG_TYP));
//Task to upload the information in background, on another thread
new AsyncTask<ApiConnector, Long, Boolean>() {
@Override
protected Boolean doInBackground(ApiConnector... apiConnectors) {
return apiConnectors[0].uploadImageToserver(nameValuePairs);
}
}.execute(new ApiConnector());
//tell the user that it is registred
Toast.makeText(getBaseContext(), getString(R.string.registred), Toast.LENGTH_LONG).show();
pDialog.dismiss();
//change window to main
Intent mainIntent = new Intent(getBaseContext(), MainActivity.class);
startActivity(mainIntent);
} catch (Exception e) {
Log.d("MainActivity", e.toString());
}
}
}).start();
} //Here ends onClick(View v)
});
你需要用到这个
runOnUiThread(new Runnable() {
@Override
public void run() {
pDialog.dismiss();
}
});
而不是普通的pDialog.dismiss();
对 Ui 的任何更新都必须从 Ui 线程或主线程进行。
不需要有不同的线程。 AsyncTask
已经在不同的线程上工作。您还需要关闭对话框并在 AsyncTask
完成后启动新的 activity。
试试这个
btnSubmit.setOnClickListener(new View.OnClickListener() {
InputStream is = null;
@Override
public void onClick(View v) {
pDialog = ProgressDialog.show(AvvikelseActivity.this, "Registrerar", "Vänta");
try {
Bitmap immage = null;
//preapare the image
immage = BitmapFactory.decodeFile(imgurl);
final int maxSize = 960;
int outWidth;
int outHeight;
int inWidth = immage.getWidth();
int inHeight = immage.getHeight();
if (inWidth > inHeight) {
outWidth = maxSize;
outHeight = (inHeight * maxSize) / inWidth;
} else {
outHeight = maxSize;
outWidth = (inWidth * maxSize) / inHeight;
}
Bitmap resizedBitmap = Bitmap.createScaledBitmap(immage, outWidth, outHeight, false);
//initiate string imagedata, which will be the string for the actual image
String imagedata = null;
//encode the image
imagedata = encodeTobase64(immage);
//Setting nameValuePairs
final List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
//adding the string variables
nameValuePairs.add(new BasicNameValuePair("reporter", reporter));
nameValuePairs.add(new BasicNameValuePair("department", department));
nameValuePairs.add(new BasicNameValuePair("errortype", errortype));
nameValuePairs.add(new BasicNameValuePair("title", title));
nameValuePairs.add(new BasicNameValuePair("description", description));
nameValuePairs.add(new BasicNameValuePair("percaution", percaution));
nameValuePairs.add(new BasicNameValuePair("imgurl", imgurl));
nameValuePairs.add(new BasicNameValuePair("dataset", dataset));
nameValuePairs.add(new BasicNameValuePair("image", imagedata));
nameValuePairs.add(new BasicNameValuePair("phoneid", android_id));
nameValuePairs.add(new BasicNameValuePair("typ", TAG_TYP));
//Task to upload the information in background, on another thread
new AsyncTask<ApiConnector, Long, Boolean>() {
@Override
protected Boolean doInBackground(ApiConnector... apiConnectors) {
return apiConnectors[0].uploadImageToserver(nameValuePairs);
}
@Override
protected void onPostExecute(Boolean result) {
//tell the user that it is registred
Toast.makeText(getBaseContext(), getString(R.string.registred), Toast.LENGTH_LONG).show();
pDialog.dismiss();
//change window to main
Intent mainIntent = new Intent(getBaseContext(), MainActivity.class);
startActivity(mainIntent);
}
}.execute(new ApiConnector());
} catch (Exception e) {
Log.d("MainActivity", e.toString());
}
}
} //Here ends onClick(View v)
});
但如果您想使用新线程,只需将其添加到您的 AsyncTask
并从外部删除此方法中的代码。
@Override
protected void onPostExecute(Boolean result) {
//tell the user that it is registred
Toast.makeText(getBaseContext(), getString(R.string.registred), Toast.LENGTH_LONG).show();
pDialog.dismiss();
//change window to main
Intent mainIntent = new Intent(getBaseContext(), MainActivity.class);
startActivity(mainIntent);
}
我正在尝试 运行 另一个线程上的函数。不幸的是,它只是继续前进而没有解雇。如果 运行 函数中没有 Try catch,它就可以工作。我认为它可能是我在函数内部的 AsyncTask。
我也发现了这个错误
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
这是我的代码
btnSubmit.setOnClickListener(new View.OnClickListener() {
InputStream is = null;
@Override
public void onClick(View v) {
pDialog = ProgressDialog.show(AvvikelseActivity.this, "Registrerar", "Vänta");
//Start the function here
//start a new thread to process job
new Thread(new Runnable() {
@Override
public void run() {
//heavy job here
//send message to main thread
try {
Bitmap immage = null;
//preapare the image
immage = BitmapFactory.decodeFile(imgurl);
final int maxSize = 960;
int outWidth;
int outHeight;
int inWidth = immage.getWidth();
int inHeight = immage.getHeight();
if (inWidth > inHeight) {
outWidth = maxSize;
outHeight = (inHeight * maxSize) / inWidth;
} else {
outHeight = maxSize;
outWidth = (inWidth * maxSize) / inHeight;
}
Bitmap resizedBitmap = Bitmap.createScaledBitmap(immage, outWidth, outHeight, false);
//initiate string imagedata, which will be the string for the actual image
String imagedata = null;
//encode the image
imagedata = encodeTobase64(immage);
//Setting nameValuePairs
final List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
//adding the string variables
nameValuePairs.add(new BasicNameValuePair("reporter", reporter));
nameValuePairs.add(new BasicNameValuePair("department", department));
nameValuePairs.add(new BasicNameValuePair("errortype", errortype));
nameValuePairs.add(new BasicNameValuePair("title", title));
nameValuePairs.add(new BasicNameValuePair("description", description));
nameValuePairs.add(new BasicNameValuePair("percaution", percaution));
nameValuePairs.add(new BasicNameValuePair("imgurl", imgurl));
nameValuePairs.add(new BasicNameValuePair("dataset", dataset));
nameValuePairs.add(new BasicNameValuePair("image", imagedata));
nameValuePairs.add(new BasicNameValuePair("phoneid", android_id));
nameValuePairs.add(new BasicNameValuePair("typ", TAG_TYP));
//Task to upload the information in background, on another thread
new AsyncTask<ApiConnector, Long, Boolean>() {
@Override
protected Boolean doInBackground(ApiConnector... apiConnectors) {
return apiConnectors[0].uploadImageToserver(nameValuePairs);
}
}.execute(new ApiConnector());
//tell the user that it is registred
Toast.makeText(getBaseContext(), getString(R.string.registred), Toast.LENGTH_LONG).show();
pDialog.dismiss();
//change window to main
Intent mainIntent = new Intent(getBaseContext(), MainActivity.class);
startActivity(mainIntent);
} catch (Exception e) {
Log.d("MainActivity", e.toString());
}
}
}).start();
} //Here ends onClick(View v)
});
你需要用到这个
runOnUiThread(new Runnable() {
@Override
public void run() {
pDialog.dismiss();
}
});
而不是普通的pDialog.dismiss();
对 Ui 的任何更新都必须从 Ui 线程或主线程进行。
不需要有不同的线程。 AsyncTask
已经在不同的线程上工作。您还需要关闭对话框并在 AsyncTask
完成后启动新的 activity。
试试这个
btnSubmit.setOnClickListener(new View.OnClickListener() {
InputStream is = null;
@Override
public void onClick(View v) {
pDialog = ProgressDialog.show(AvvikelseActivity.this, "Registrerar", "Vänta");
try {
Bitmap immage = null;
//preapare the image
immage = BitmapFactory.decodeFile(imgurl);
final int maxSize = 960;
int outWidth;
int outHeight;
int inWidth = immage.getWidth();
int inHeight = immage.getHeight();
if (inWidth > inHeight) {
outWidth = maxSize;
outHeight = (inHeight * maxSize) / inWidth;
} else {
outHeight = maxSize;
outWidth = (inWidth * maxSize) / inHeight;
}
Bitmap resizedBitmap = Bitmap.createScaledBitmap(immage, outWidth, outHeight, false);
//initiate string imagedata, which will be the string for the actual image
String imagedata = null;
//encode the image
imagedata = encodeTobase64(immage);
//Setting nameValuePairs
final List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
//adding the string variables
nameValuePairs.add(new BasicNameValuePair("reporter", reporter));
nameValuePairs.add(new BasicNameValuePair("department", department));
nameValuePairs.add(new BasicNameValuePair("errortype", errortype));
nameValuePairs.add(new BasicNameValuePair("title", title));
nameValuePairs.add(new BasicNameValuePair("description", description));
nameValuePairs.add(new BasicNameValuePair("percaution", percaution));
nameValuePairs.add(new BasicNameValuePair("imgurl", imgurl));
nameValuePairs.add(new BasicNameValuePair("dataset", dataset));
nameValuePairs.add(new BasicNameValuePair("image", imagedata));
nameValuePairs.add(new BasicNameValuePair("phoneid", android_id));
nameValuePairs.add(new BasicNameValuePair("typ", TAG_TYP));
//Task to upload the information in background, on another thread
new AsyncTask<ApiConnector, Long, Boolean>() {
@Override
protected Boolean doInBackground(ApiConnector... apiConnectors) {
return apiConnectors[0].uploadImageToserver(nameValuePairs);
}
@Override
protected void onPostExecute(Boolean result) {
//tell the user that it is registred
Toast.makeText(getBaseContext(), getString(R.string.registred), Toast.LENGTH_LONG).show();
pDialog.dismiss();
//change window to main
Intent mainIntent = new Intent(getBaseContext(), MainActivity.class);
startActivity(mainIntent);
}
}.execute(new ApiConnector());
} catch (Exception e) {
Log.d("MainActivity", e.toString());
}
}
} //Here ends onClick(View v)
});
但如果您想使用新线程,只需将其添加到您的 AsyncTask
并从外部删除此方法中的代码。
@Override
protected void onPostExecute(Boolean result) {
//tell the user that it is registred
Toast.makeText(getBaseContext(), getString(R.string.registred), Toast.LENGTH_LONG).show();
pDialog.dismiss();
//change window to main
Intent mainIntent = new Intent(getBaseContext(), MainActivity.class);
startActivity(mainIntent);
}