http 서버에서 json
피드를로드하는 동안 ProgressDialog
을 표시하려고합니다. doInBackground
및 onPostExecute
에 NullPointerException
이 있습니다. AsyncTask
을 오해하지 않은 경우 onPostExecute
에 최종 코드가 실행됩니다. 내 잘못은 어디 있니? 여기 내 활동 코드입니다 :NullPointerException을 던지는 AsyncTask의 ProgressDialog?
public class ResultInput extends Activity {
int count = 0;
String mainUrl;
String[] product_pict_part;
InputStream is = null;
private ProgressDialog pd;
String TAG = "meinDebug";
ParseEan parse_code;
String readProductNameEan;
ImageView productPic;
String productName;
Drawable drawable;
TextView product_name;
TextView text_level;
int productLevel;
private Context context;
// private MyProgressDialog dialog;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
new ProgressTask(ResultInput.this).execute();
}
private InputStream fetch(String urlString) throws MalformedURLException,
IOException {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(urlString);
HttpResponse response = httpClient.execute(request);
return response.getEntity().getContent();
}
private class ProgressTask extends AsyncTask<String, Void, Boolean> {
private ProgressDialog dialog;
private Activity activity;
// private List<Message> messages;
public ProgressTask(Activity activity) {
this.activity = activity;
context = activity;
dialog = new ProgressDialog(context);
}
/** progress dialog to show user that the backup is processing. */
/** application context. */
protected void onPreExecute() {
this.dialog.setMessage("Progress start");
this.dialog.show();
}
@Override
protected void onPostExecute(final Boolean success) {
pd.dismiss();
productPic.setImageDrawable(drawable);
Log.i(TAG, "Picture URL " + mainUrl);
product_name.setText(productName);
if (productLevel == 1) {
text_level.setText("helal");
} else if (productLevel == 2) {
text_level.setText("mittel");
} else if (productLevel == 3) {
text_level.setText("haram");
} else if (productLevel == 4) {
text_level.setText("in analyse");
}
if (dialog.isShowing()) {
dialog.dismiss();
}
if (success) {
Toast.makeText(context, "OK", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "Error", Toast.LENGTH_LONG).show();
}
}
protected Boolean doInBackground(final String... args) {
try {
mainUrl = "http://nur-efsan.de/websHalalCheck/public/image/index/dir/";
product_name = (TextView) findViewById(R.id.produkt_name);
text_level = (TextView) findViewById(R.id.text_level);
productPic = (ImageView) findViewById(R.id.productpic);
productName = parse_code.parseJSONPost("data", "name",
readProductNameEan);
String productPicture = parse_code.parseJSONPost("data",
"image", readProductNameEan);
productLevel = Integer.parseInt(parse_code.parseJSONPost(
"data", "levelId", readProductNameEan));
product_pict_part = productPicture.split("/");
mainUrl = mainUrl + product_pict_part[0] + "/name/"
+ product_pict_part[1];
try {
is = fetch(mainUrl);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
drawable = Drawable.createFromStream(is, "src");
return true;
} catch (Exception e) {
Log.e("tag", "error", e);
return false;
}
}
}
}
http://samir-mangroliya.blogspot.in/p/android-customized-listview.html AsyncTask를 함께 wrok하는 방법을 참조 –
'execute()'태스크 전에'show()'대화 상자를,'doInBackground()'메소드는'onProgressUpdate()'메소드에 대한 통지와'progressDialog'에 대한 커스텀 증분 진행을 보내야합니다. 원하는 경우 여기에 내 작업'progressDialog'을 게시 할 수 있습니다. – Sajmon