0

我正在尝试通过单击按钮开始一项活动,但似乎无法使其正常工作。按下按钮时应用程序冻结。

这是我使用的代码:

从按钮:

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="ExecJson"
    android:text="Button" />

在当前活动中:

           public void ExecJson (View view) {           
            Intent intent = new Intent(this, Listviewer.class); 
            startActivity(intent);
        }

我要开始的活动:

package com.pxr.tutorial.json;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.pxr.tutorial.xmltest.R;

@SuppressWarnings("unused")
public class Listviewer extends ListActivity {


    /** Called when the activity is first created. */

     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);


        setContentView(R.layout.listplaceholder);


        ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();


        JSONObject json = JSONfunctions.getJSONfromURL("http://oranjelan.nl/deelnemers.txt");

        try{

            JSONArray  deelnemers = json.getJSONArray("deelnemers");

            for(int i=0;i<deelnemers.length();i++){                     
                HashMap<String, String> map = new HashMap<String, String>();    
                JSONObject e = deelnemers.getJSONObject(i);

                map.put("id",  String.valueOf(i));
                map.put("name", "Alias: " +  e.getString("alias"));
                map.put("city", "Woonplaats: " +  e.getString("woonplaats"));
                map.put("sex", "Geslacht: " +  e.getString("geslacht"));
                mylist.add(map);            
            }   



        }catch(JSONException e)        {
             Log.e("log_tag", "Error parsing data "+e.toString());
        }

        ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main, 
                        new String[] { "name", "city", "sex" }, 
                        new int[] { R.id.item_title, R.id.item_subtitle2, R.id.item_subtitle });

        setListAdapter(adapter);

        final ListView lv = getListView();
        lv.setTextFilterEnabled(true);  
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
                @SuppressWarnings("unchecked")
                HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);                   
                Toast.makeText(Listviewer.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show(); 

            }
        });



    }
}

谁能告诉我我做错了什么?

4

2 回答 2

2

可能是因为您试图在 Activity Listviewer 的 MainUI 线程上获取 Web 请求(网络相关操作)。

从这一行开始,

JSONObject json = JSONfunctions.getJSONfromURL("http://oranjelan.nl/deelnemers.txt");

对此 Web 请求使用AsyncTask 并重试..

于 2012-07-10T08:37:39.117 回答
0

启动新应用时应用程序冻结的原因Activity是因为您正在 UI 线程上运行与网络相关的操作。AnAsyncTask将很容易实现,并且可能还会摆脱您的问题。

例子:

public class AsyncLoading extends AsyncTask<String, Void, Boolean> {

    // Attributes
    private Context mContext;
    private ListView mListView;
    private List<Map<String, String>> mListData;

    public AsyncLoading(Context c, ListView lv) {
        mContext = c;
        mListView = lv;
    }

    @Override
    protected Boolean doInBackground(String... params) {
        try {
            JSONObject json = JSONfunctions.getJSONfromURL("http://oranjelan.nl/deelnemers.txt");
            JSONArray  deelnemers = json.getJSONArray("deelnemers");
            for(int i=0;i<deelnemers.length();i++){                  

                HashMap<String, String> map = new HashMap<String, String>();    
                JSONObject e = deelnemers.getJSONObject(i);

                map.put("id",  String.valueOf(i));
                map.put("name", "Alias: " +  e.getString("alias"));
                map.put("city", "Woonplaats: " +  e.getString("woonplaats"));
                map.put("sex", "Geslacht: " +  e.getString("geslacht"));

                mListData.add(map);            
            }   
            return true;

        } catch(JSONException e) {
            Log.e("log_tag", "Error parsing data "+e.toString());
            return false;
        }
    }

    @Override
    protected void onPostExecute(Boolean result) {
        if( result ) {

            mListView.setAdapter(

                new SimpleAdapter(
                    mContext,
                    mListData,
                    R.layout.main,
                    new String[] { "name", "city", "sex" },
                    new int[] { R.id.item_title, R.id.item_subtitle2, R.id.item_subtitle }
                )

            );
        } else {
            Toast.makeText(mContext, "No data found.", Toast.LENGTH_SHORT).show();
        }
    }
}
于 2012-07-10T08:37:59.200 回答