Android 在任务上使用时处理程序崩溃
我正在asyntask中使用处理程序,这使我的应用程序崩溃。我已经在这里上传了我的项目 它正在工作,唯一的问题是在asytask中使用处理程序会导致应用程序崩溃。Android 在任务上使用时处理程序崩溃,android,android-asynctask,Android,Android Asynctask,我正在asyntask中使用处理程序,这使我的应用程序崩溃。我已经在这里上传了我的项目 它正在工作,唯一的问题是在asytask中使用处理程序会导致应用程序崩溃。 我的问题是如何在postexecute上的json解析显示中获得for循环at的值 是否有其他方法可以先获取值,然后在执行后显示 如果我在主代码应用程序崩溃时声明这一行,请修复我的问题 row4 = getLayoutInflater().inflate(R.layout.row2, null); pub
我的问题是如何在postexecute上的json解析显示中获得for循环at的值 是否有其他方法可以先获取值,然后在执行后显示 如果我在主代码应用程序崩溃时声明这一行,请修复我的问题
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;
}
row4=getLayoutFlater()。充气(R.layout.row2,null);
公共类第五屏幕扩展活动{
字符串num=null;
长菜单标识;
串菜名;
查看第4行;
int-IOConnect=0;
查看第3行;
字符串状态;
私有静态处理程序;
水平视图列表视图;
3-共轭亚基;
字符串描述符API;
TextView txt1、txt2、txt3;
ImageView img1;
字符串URL,URL2;
字符串SelectMenuAPI;
静态ArrayList Category_ID=新ArrayList();
静态ArrayList Category_name=新ArrayList();
静态ArrayList Category_image=新ArrayList();
公共静态字符串2;
私用水渠;
字符串名;
字符串描述;
字符串url1;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局第五屏幕);
handler=新的handler();
img1=(ImageView)findViewById(R.id.test\u按钮\u图像);
txt1=(TextView)findViewById(R.id.menuname);
txt3=(TextView)findViewById(R.id.description);
AndroidQuery=新水渠(本);
新建getDataTask().execute();
}
void clearData(){
类别_ID.clear();
类别名称。清除();
Category_image.clear();
}
公共类getDataTask扩展了AsyncTask{
getDataTask(){
}
@凌驾
受保护的void onPreExecute(){
//TODO自动生成的方法存根
}
@凌驾
受保护的Void doInBackground(Void…arg0){
//TODO自动生成的方法存根
parseJSONData();
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
//TODO自动生成的方法存根
如果(IOConnect==0){
txt1.setText(名称);
txt3.setText(说明);
id(img1).image(url1,false,false);
}
}
}
公共void parseJSONData(){
选择菜单UAPI=”http://198.57.208.46/~school/api/index/getdishscription?dish_name=American%20奶酪%20汉堡”;
clearData();
URL=选择菜单UAPI;
URL2=URL.replace(“,“%20”);
试一试{
HttpClient=new DefaultHttpClient();
HttpConnectionParams
.setConnectionTimeout(client.getParams(),15000);
HttpConnectionParams.setSoTimeout(client.getParams(),15000);
HttpUriRequest请求=新的HttpGet(URL2);
HttpResponse response=client.execute(请求);
InputStream atomInputStream=response.getEntity().getContent();
BufferedReader in=新的BufferedReader(新的InputStreamReader(
atomInputStream),8192);
弦线;
字符串str=“”;
而((line=in.readLine())!=null){
str+=直线;
}
JSONObject json2=新的JSONObject(str);
status=json2.getString(“status”);
如果(状态等于(“1”)){
JSONArray school2=json2.getJSONArray(“数据”);
for(int i=0;i<2.length();i++){
name=school2.getJSONObject(0.getString(“name”);
description=school2.getJSONObject(0).getString(
“说明”);
url1=school2.getJSONObject(0.getString(“图像”);
}
JSONObject school3=json2.getJSONObject(“菜肴营养”);
最终的TableLayout表格=(TableLayout)findViewById(R.id.table2);
对于(int j=0;j<3.length();j++){
字符串s=String.valueOf(j+1);
row3=GetLayoutFlater()。充气(R.layout.rows,null);
((TextView)第3行findViewById(R.id.localTime))
.setText(school3.getJSONObject.getString(“数量”);
((文本视图)第3行findViewById(R.id.apprentTemp))
.setText(school3.getJSONObject.getString(“名称”);
fifthscreen.getHandler().post(新的Runnable()命令){
公开募捐{
表.addView(第3行);
}
});
}
最终线性布局表3=(线性布局)findViewById(R.id.table3);
JSONArray学校5=json2.getJSONArray(“菜肴配料”);
对于(int i=0;i<5.length();i++){
row4=GetLayoutFlater()。充气(R.layout.row2,null);
((TextView)row4.findviewbyd(R.id.name)).setText(school5
.getJSONObject(i).getString(“名称”);
((文本视图)第4行findViewById(R.id.SubComponents))
.setText(school5.getJSONObject(i).getString(
“sub_成分”);
fifthscreen.getHandler().post(新的Runnable()命令){
公开募捐{
表3.addView(第4行);
}
});
}
}
否则{
JSONArray school2=json2.getJSONArray(“数据”);
for(int i=0;i<2.length();i++){
JSONObject object=school2.getJSONObject(i);
类别识别号添加((长)i);
Category_name.add(object.getString(“name”));
}
}
}捕获(格式错误){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
IOConnect=1;
e、 printStackTrace();
}捕获(JSONException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
私人静电
row4 = getLayoutInflater().inflate(R.layout.row2, null);
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
return parseJSONData();
}
public class getDataTask extends AsyncTask<Void, Void, String> {
getDataTask() {
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
}
@Override
protected String doInBackground(Void... arg0) {
// TODO Auto-generated method stub
return parseJSONData();
}
@Override
protected void onPostExecute(String result) {
//result is the json string you go back from the server parse it in here now
}
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"));
public class MainActivity extends Activity {
String _response;
// ArrayList<String> nam = new ArrayList<String>();
// ArrayList<String> desc = new ArrayList<String>();
// ArrayList<String> ur = new ArrayList<String>();
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv= (TextView) findViewById(R.id.textView1);
new TheTask().execute();
}
@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;
}
public class TheTask extends AsyncTask<Void, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(Void... arg0) {
try {
String query = URLEncoder.encode("American%20Cheese%20Burger", "utf-8");
String url = "http://198.57.208.46/~school/api/index/getDishDiscription?dish_name=American%20Cheese%20Burger";
Log.i("url",""+url);
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
HttpEntity resEntity = response.getEntity();
_response=EntityUtils.toString(resEntity);
// JSONObject json = new JSONObject(_response);
// String status = json.getString("status");
// if (status.equals("1")) {
// JSONArray school2 = json.getJSONArray("data");
// for (int i = 0; i < school2.length(); i++) {
// String name = school2.getJSONObject(0).getString("name");
// nam.add(name);
// String description = school2.getJSONObject(0).getString(
// "description");
// desc.add(description);
// String url1 = school2.getJSONObject(0).getString("image");
// ur.add(url1);
//
// }
// }
// JSONObject school3 = json.getJSONObject("dish_nutrition");
// JSONObject jb2 = school3.getJSONObject("1");
// String name1 =jb2.getString("name");
// String qty =jb2.getString("qty");
// JSONObject jb3 = school3.getJSONObject("2");
// String name2 =jb3.getString("name");
// String qty2 =jb3.getString("qty");
// JSONArray jr = json.getJSONArray("dish_ingredient");
// for(int i=0;i<jr.length();i++)
// {
// JSONObject jb = (JSONObject) jr.get(i);
// String n = jb.getString("name");
// String sub= jb.getString("sub_ingredients");
// }
}catch(Exception e)
{
e.printStackTrace();
}
return _response;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
tv.setText(result);
}
}
}
<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:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="17dp"
android:text="TextView" />
</RelativeLayout>
dish_nutrition": {
"1": {
"name": "Cholesterol ",
"qty": "2"
},
"2": {
"name": "Chol (mg)",
"qty": "3"
}
JSONObject school3 = json2.getJSONObject("dish_nutrition");
for (int j = 0; j < school3.length(); j++) { // school3 is not a json array