2012-05-22 56 views
1

我很新的Android編程,所以任何幫助將是偉大的:) 我試圖得到一些虛擬數據遠程,解析JSON響應並顯示它在一個列表視圖。Android Asynctask HTTP在ListView中不顯示結果?

這是XML佈局文件我試圖與該列表活動使用方法: -

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
    <ListView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/list" 
    /> 
</LinearLayout> 

這是我的http搜索活動: -

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.ListActivity; 
import android.net.ParseException; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.Toast; 

public class searchResultActivity extends ListActivity { 

    ListView listview; 
    String result = null; 
    InputStream is = null; 
    StringBuilder sb = null; 
    String url; 
    String fd_name; 
    ArrayList<String> listItems; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     /*setContentView(R.layout.test); 
     listview = (ListView)findViewById(R.id.list);*/ 
     url = "http://pjchambers.comuf.com/index.php"; 
     new getData().execute(); 
    } 

    public class getData extends AsyncTask<String, Void, ArrayList<String>> { 

     protected ArrayList<String> doInBackground(String...urls) { 
      // TODO Auto-generated method stub 
      listItems = new ArrayList<String>(); 

      try { 
       // Set of http statements setting up the connections to the php 
       // and database 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpGet httpget = new HttpGet(url); 
       HttpResponse response = httpclient.execute(httpget); 
       HttpEntity entity = response.getEntity(); 
       is = entity.getContent(); 
      } catch (Exception e) { 
       Log.e("log_tag", "Error in http connection" + e.toString()); 
      } 

      // convert response to string 
      try { 
       BufferedReader reader = new BufferedReader(
         new InputStreamReader(is, "iso-8859-1"), 8); 
       sb = new StringBuilder(); 
       sb.append(reader.readLine() + "\n"); 
       String line = "0"; 
       while ((line = reader.readLine()) != null) { 
        sb.append(line + "\n"); 
       } 
       is.close(); 
       result = sb.toString(); 
      } catch (Exception e) { 
       Log.e("log_tag", "Error converting result " + e.toString()); 
      } 

      // paring data 
      try { 
       JSONArray jArray = new JSONArray(result); 
       JSONObject json_data = null; 
       for (int i = 0; i < jArray.length(); i++) { 
        json_data = jArray.getJSONObject(i); 
        fd_name = json_data.getString("Name"); 
        listItems.add(fd_name); 
       } 
      } catch (JSONException e1) { 
       Toast.makeText(getBaseContext(), "No Results Found", 
         Toast.LENGTH_LONG).show(); 
      } catch (ParseException e1) { 
       e1.printStackTrace(); 
      } 

      Log.d("debug", "- " + listItems.toString()); 
      return listItems; 
     } 

     protected void doPostExecute() { 
      ArrayAdapter<String> adapter = new ArrayAdapter<String>(
        searchResultActivity.this, android.R.layout.simple_list_item_1, R.id.list, listItems); 
      setListAdapter(adapter); 
     } 

    } 

} 

我花了幾個小時試圖不同的方法和方法,使用一些不同的在線教程,但它們都以相同的結果結束。由於它的應用程序有效,但顯示屏幕顯示;但是,如果我在onCreate方法中聲明佈局應用程序崩潰。任何幫助或建議將必須意識到,一套新的眼睛可能會注意到的問題(一個或多個)直線距離

這是logcat的後,我註釋掉的兩個語句

05-22 12:12:20.234: D/debug(631): - [The Arena, Empire] 
05-22 12:12:42.004: I/dalvikvm(691): threadid=3: reacting to signal 3 
05-22 12:12:42.244: I/dalvikvm(691): Wrote stack traces to '/data/anr/traces.txt' 
05-22 12:12:42.404: D/gralloc_goldfish(691): Emulator without GPU emulation detected. 
05-22 12:12:48.415: I/dalvikvm(691): threadid=3: reacting to signal 3 
05-22 12:12:48.424: I/dalvikvm(691): Wrote stack traces to '/data/anr/traces.txt' 
05-22 12:12:50.595: D/AndroidRuntime(691): Shutting down VM 
05-22 12:12:50.595: W/dalvikvm(691): threadid=1: thread exiting with uncaught exception (group=0x409c01f8) 
05-22 12:12:50.614: E/AndroidRuntime(691): FATAL EXCEPTION: main 
05-22 12:12:50.614: E/AndroidRuntime(691): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.project.barcrawl/com.project.barcrawl.searchResultActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' 
05-22 12:12:50.614: E/AndroidRuntime(691): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 
05-22 12:12:50.614: E/AndroidRuntime(691): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
05-22 12:12:50.614: E/AndroidRuntime(691): at android.app.ActivityThread.access$600(ActivityThread.java:123) 
05-22 12:12:50.614: E/AndroidRuntime(691): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
05-22 12:12:50.614: E/AndroidRuntime(691): at android.os.Handler.dispatchMessage(Handler.java:99) 
05-22 12:12:50.614: E/AndroidRuntime(691): at android.os.Looper.loop(Looper.java:137) 
05-22 12:12:50.614: E/AndroidRuntime(691): at android.app.ActivityThread.main(ActivityThread.java:4424) 
05-22 12:12:50.614: E/AndroidRuntime(691): at java.lang.reflect.Method.invokeNative(Native Method) 
05-22 12:12:50.614: E/AndroidRuntime(691): at java.lang.reflect.Method.invoke(Method.java:511) 
05-22 12:12:50.614: E/AndroidRuntime(691): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
05-22 12:12:50.614: E/AndroidRuntime(691): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
05-22 12:12:50.614: E/AndroidRuntime(691): at dalvik.system.NativeStart.main(Native Method) 
05-22 12:12:50.614: E/AndroidRuntime(691): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' 
05-22 12:12:50.614: E/AndroidRuntime(691): at android.app.ListActivity.onContentChanged(ListActivity.java:243) 
05-22 12:12:50.614: E/AndroidRuntime(691): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:254) 
05-22 12:12:50.614: E/AndroidRuntime(691): at android.app.Activity.setContentView(Activity.java:1835) 
05-22 12:12:50.614: E/AndroidRuntime(691): at com.project.barcrawl.searchResultActivity.onCreate(searchResultActivity.java:41) 
05-22 12:12:50.614: E/AndroidRuntime(691): at android.app.Activity.performCreate(Activity.java:4465) 
05-22 12:12:50.614: E/AndroidRuntime(691): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 
05-22 12:12:50.614: E/AndroidRuntime(691): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 
05-22 12:12:50.614: E/AndroidRuntime(691): ... 11 more 
05-22 12:12:51.004: I/dalvikvm(691): threadid=3: reacting to signal 3 
05-22 12:12:51.024: I/dalvikvm(691): Wrote stack traces to '/data/anr/traces.txt' 
+0

你是什麼意思「如果我聲明佈局」?你的意思是2條評論線?你能詳細說明你的實際問題和疑問嗎?並添加錯誤日誌。謝謝! – Joe

+0

您需要首先取消註釋這兩行才能使其工作。基於logcat的 –

+0

究竟是哪裏的問題?你能連接到互聯網嗎?你解析成功了嗎? – Hesam

回答

2

你在XML ID應id="@android:id/list"那麼它應該工作。在您的代碼中,將創建一個新ID作爲「您的」R.class的一部分,與android.R.id.list不同。因此無法找到列表視圖,因此拋出異常。

另外,構造函數ArrayAdapter的第三個參數應該是要填充數據的文本資源的ID。你通過了R.id.list這是錯誤的。對於android.R.id.simple_list_item_1定義的資源,如果我沒有弄錯,應該是android.R.id.text1

+0

謝謝我已經改變了xml,但仍然沒有運氣,我知道它的東西愚蠢我只是不知道它是什麼。至少它不會拋出異常 – PJChamb

+0

我更新了我的答案,並在上面的代碼中看到了另一個問題。如果它仍然沒有幫助,那麼更新您的原始帖子是一個好主意,以便可以看到現在做了什麼。 – Stephan