アプリケーションをクラッシュさせる非同期タスクでハンドラーを使用しています。ここにプロジェクトをアップロードしましたhttps://www.zeta-uploader.com/998997295
それは機能しており、問題は asytask 内でハンドラーを使用することだけで、アプリケーションがクラッシュします。
私の質問は、postexecute の json 解析ショーで for ループの値を取得するにはどうすればよいですか?
最初に値を取得してから postexecute に表示する別のアイデアはありますか?
メインコードアプリケーションのクラッシュでこの行を宣言すると、問題が解決します
row4 = getLayoutInflater().inflate(R.layout.row2, null);
public class fifthscreen extends Activity {
String num = null;
long Menu_ID;
String dish_name;
View row4;
int IOConnect = 0;
View row3;
String status;
private static Handler handler;
HorizontalListView listview;
CategoryListAdapter3 cla;
String DescriptionAPI;
TextView txt1, txt2, txt3;
ImageView img1;
String URL, URL2;
String SelectMenuAPI;
static ArrayList<Long> Category_ID = new ArrayList<Long>();
static ArrayList<String> Category_name = new ArrayList<String>();
static ArrayList<String> Category_image = new ArrayList<String>();
public static String allergen2;
private AQuery androidAQuery;
String name;
String description;
String url1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fifthscreen);
handler = new Handler();
img1 = (ImageView) findViewById(R.id.test_button_image);
txt1 = (TextView) findViewById(R.id.menuname);
txt3 = (TextView) findViewById(R.id.description);
androidAQuery = new AQuery(this);
new getDataTask().execute();
}
void clearData() {
Category_ID.clear();
Category_name.clear();
Category_image.clear();
}
public class getDataTask extends AsyncTask<Void, Void, Void> {
getDataTask() {
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
}
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
parseJSONData();
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
if (IOConnect == 0) {
txt1.setText(name);
txt3.setText(description);
androidAQuery.id(img1).image(url1, false, false);
}
}
}
public void parseJSONData() {
SelectMenuAPI = "http://198.57.208.46/~school/api/index/getDishDiscription?dish_name=American%20Cheese%20Burger";
clearData();
URL = SelectMenuAPI;
URL2 = URL.replace(" ", "%20");
try {
HttpClient client = new DefaultHttpClient();
HttpConnectionParams
.setConnectionTimeout(client.getParams(), 15000);
HttpConnectionParams.setSoTimeout(client.getParams(), 15000);
HttpUriRequest request = new HttpGet(URL2);
HttpResponse response = client.execute(request);
InputStream atomInputStream = response.getEntity().getContent();
BufferedReader in = new BufferedReader(new InputStreamReader(
atomInputStream), 8192);
String line;
String str = "";
while ((line = in.readLine()) != null) {
str += line;
}
JSONObject json2 = new JSONObject(str);
status = json2.getString("status");
if (status.equals("1")) {
JSONArray school2 = json2.getJSONArray("data");
for (int i = 0; i < school2.length(); i++) {
name = school2.getJSONObject(0).getString("name");
description = school2.getJSONObject(0).getString(
"description");
url1 = school2.getJSONObject(0).getString("image");
}
JSONObject school3 = json2.getJSONObject("dish_nutrition");
final TableLayout table = (TableLayout) findViewById(R.id.table2);
for (int j = 0; j < school3.length(); j++) {
String s = String.valueOf(j + 1);
row3 = getLayoutInflater().inflate(R.layout.rows, null);
((TextView) row3.findViewById(R.id.localTime))
.setText(school3.getJSONObject(s).getString("qty"));
((TextView) row3.findViewById(R.id.apprentTemp))
.setText(school3.getJSONObject(s).getString("name"));
fifthscreen.getHandler().post(new Runnable() {
public void run() {
table.addView(row3);
}
});
}
final LinearLayout table3 = (LinearLayout) findViewById(R.id.table3);
JSONArray school5 = json2.getJSONArray("dish_ingredient");
for (int i = 0; i < school5.length(); i++) {
row4 = getLayoutInflater().inflate(R.layout.row2, null);
((TextView) row4.findViewById(R.id.name)).setText(school5
.getJSONObject(i).getString("name"));
((TextView) row4.findViewById(R.id.subingredients))
.setText(school5.getJSONObject(i).getString(
"sub_ingredients"));
fifthscreen.getHandler().post(new Runnable() {
public void run() {
table3.addView(row4);
}
});
}
}
else {
JSONArray school2 = json2.getJSONArray("data");
for (int i = 0; i < school2.length(); i++) {
JSONObject object = school2.getJSONObject(i);
Category_ID.add((long) i);
Category_name.add(object.getString("name"));
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
IOConnect = 1;
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static Handler getHandler() {
// TODO Auto-generated method stub
return handler;
}
}