2013-04-24 85 views
-1

我在android中使用JSON爲客戶端服務器通信做了一個代碼。我正在使用WAMP服務器,並且PHP文件中沒有錯誤。當我運行我的應用程序模擬器時顯示「不幸的是,myapp已停止」。請給我建議我的想法。我試圖解決,但我不能。 我的Java文件:模擬器不幸拋出,myapp已停止消息

package com.example.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.NameValuePair; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
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.Bundle; 
import android.util.Log; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends ListActivity { 

JSONArray jArray; 
String result = null; 
InputStream is = null; 
StringBuilder sb=null; 
TextView tv; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
//http post 
try{ 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost("http://10.0.2.2/android_connect/read1.php"); 
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
    HttpResponse response = httpclient.execute(httppost); 
    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{ 
     jArray = new JSONArray(result); 
     JSONObject json_data=null; 
     for(int i=0;i<jArray.length();i++){ 
      json_data = jArray.getJSONObject(i); 
      String ct_name = json_data.getString("User_ID"); 
      tv = (TextView) findViewById(R.id.textView1); 

      tv.setText(ct_name); 

     } 
     } 
     catch(JSONException e1){ 
      Toast.makeText(getBaseContext(), "No City Found" ,Toast.LENGTH_LONG).show(); 
     } catch (ParseException e1) { 
      e1.printStackTrace(); 
    } 
} 
} 

和PHP文件:

<?php 
mysql_connect("localhost","root",""); 
mysql_select_db("androidhive"); 
$sql=mysql_query("select User_ID from opass"); 
while($row=mysql_fetch_assoc($sql)) 
$output[]=$row; 
print(json_encode($output)); 
mysql_close(); 
?> 

logcat的錯誤:

04-21 22:43:33.502: E/AndroidRuntime(399): FATAL EXCEPTION: main 
04-21 22:43:33.502: E/AndroidRuntime(399): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.http/com.example.http.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' 
04-21 22:43:33.502: E/AndroidRuntime(399): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
04-21 22:43:33.502: E/AndroidRuntime(399): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
04-21 22:43:33.502: E/AndroidRuntime(399): at android.app.ActivityThread.access$600(ActivityThread.java:141) 
04-21 22:43:33.502: E/AndroidRuntime(399): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
04-21 22:43:33.502: E/AndroidRuntime(399): at android.os.Handler.dispatchMessage(Handler.java:99) 
04-21 22:43:33.502: E/AndroidRuntime(399): at android.os.Looper.loop(Looper.java:137) 
04-21 22:43:33.502: E/AndroidRuntime(399): at android.app.ActivityThread.main(ActivityThread.java:5041) 
04-21 22:43:33.502: E/AndroidRuntime(399): at java.lang.reflect.Method.invokeNative(Native Method) 
04-21 22:43:33.502: E/AndroidRuntime(399): at java.lang.reflect.Method.invoke(Method.java:511) 
04-21 22:43:33.502: E/AndroidRuntime(399): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
04-21 22:43:33.502: E/AndroidRuntime(399): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
04-21 22:43:33.502: E/AndroidRuntime(399): at dalvik.system.NativeStart.main(Native Method) 
04-21 22:43:33.502: E/AndroidRuntime(399): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' 
04-21 22:43:33.502: E/AndroidRuntime(399): at android.app.ListActivity.onContentChanged(ListActivity.java:243) 
04-21 22:43:33.502: E/AndroidRuntime(399): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:273) 
04-21 22:43:33.502: E/AndroidRuntime(399): at android.app.Activity.setContentView(Activity.java:1881) 
04-21 22:43:33.502: E/AndroidRuntime(399): at com.example.http.MainActivity.onCreate(MainActivity.java:40) 
04-21 22:43:33.502: E/AndroidRuntime(399): at android.app.Activity.performCreate(Activity.java:5104) 
04-21 22:43:33.502: E/AndroidRuntime(399): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
04-21 22:43:33.502: E/AndroidRuntime(399): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 

回答

1

您的內容必須有一個ListViewid屬性爲「android.R。 id.list'

您的R.layout.activity_main沒有listview元素。

PS:你不應該使用在主線程網絡請求閱讀AsyncTask

2

當延長ListActivity,並提供自定義佈局,你必須要在以下標識設爲您的ListView在XML中 - 在這種遇到你的activity_main

android:id="@android:id/list" 

順便說一下,這個例外本身就足以說明問題。

0

從錯誤消息:

04-21 22:43:33.502: E/AndroidRuntime(399): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.http/com.example.http.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' 

你 「activity_main.xml中」 可能沒有屬性ID爲 「android.R.id.list」。 因爲您的活動擴展了「ListActivity」,所以您應該包括它。

相關問題