2012-07-10 165 views
0

我試圖啓動一個按鈕的onclick活動,但似乎無法讓它工作。 按下按鈕時,應用程序會凍結。開始一個新的活動OnClick(Android)

這是我使用的代碼:我要開始

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

,並且活動:

從按鈕:

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

在當前活動

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

      } 
     }); 



    } 
} 

有誰可以告訴我在做什麼錯了?

+1

從執行提供的logcat的! – theAlse 2012-07-10 08:35:05

+1

您是否已將新活動添加到清單文件? – Saiesh 2012-07-10 08:36:19

+0

應用程序凍結,因爲按鈕工作正常,問題出現在listviewer活動中。 – Rakshi 2012-07-10 08:38:09

回答

2

可能是因爲您正在嘗試在活動列表查看器的MainUI線程上獲取Web請求(網絡相關操作)。

從這一行,

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

使用AsyncTask這個Web請求,然後再試一次..

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