开始新的活动OnClick(Android)

时间:2012-07-10 08:34:05

标签: android android-activity onclick

我正在尝试按一下按钮启动一个活动,但似乎无法让它工作。 按下按钮时应用会冻结。

这是我使用的代码:

从按钮开始:

<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(); 

            }
        });



    }
}

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

2 个答案:

答案 0 :(得分:2)

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

从这一行开始,

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

对此网络请求使用AsyncTask,然后重试..

答案 1 :(得分:0)

启动新Activity时应用程序冻结的原因是因为您正在UI线程上运行与网络相关的操作。 AsyncTask很容易实现,也可能会解决您的问题。

示例:

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();
        }
    }
}