这是我的问题, 我在我的代码中添加了一些log.i,即使在最后,它们都打印在logCat中,这意味着它会一直到最后,但是有些错误会继续... ...
感谢您的帮助!
日志:
08-02 13:14:11.030: I/Process(1817): Sending signal. PID: 1817 SIG: 9
08-02 13:15:36.670: D/dalvikvm(1854): GC_FOR_ALLOC freed 36K, 4% free 6347K/6595K, paused 60ms
08-02 13:15:36.790: I/dalvikvm-heap(1854): Grow heap (frag case) to 12.983MB for 7056016-byte allocation
08-02 13:15:36.880: D/dalvikvm(1854): GC_FOR_ALLOC freed <1K, 3% free 13237K/13511K, paused 60ms
08-02 13:15:37.020: D/dalvikvm(1854): GC_CONCURRENT freed <1K, 3% free 13237K/13511K, paused 4ms+3ms
08-02 13:15:38.940: D/dalvikvm(1854): GC_CONCURRENT freed 3K, 1% free 15246K/15367K, paused 3ms+3ms
08-02 13:15:41.521: D/dalvikvm(1854): GC_FOR_ALLOC freed 28K, 1% free 15555K/15687K, paused 59ms
08-02 13:15:41.551: I/dalvikvm-heap(1854): Grow heap (frag case) to 17.630MB for 2499884-byte allocation
08-02 13:15:41.701: D/dalvikvm(1854): GC_FOR_ALLOC freed <1K, 2% free 17996K/18183K, paused 59ms
08-02 13:15:41.851: D/dalvikvm(1854): GC_CONCURRENT freed <1K, 2% free 17996K/18183K, paused 6ms+3ms
08-02 13:15:42.712: I/log_tag(1854): ID_ville: 3, Nom_ville: Lyon
08-02 13:15:42.712: I/log_tag(1854): ID_ville: 10, Nom_ville: Lille
08-02 13:15:42.712: I/test tableau(1854): Lyon
08-02 13:15:42.720: I/test 2 tableau(1854): Lille
08-02 13:15:42.750: D/AndroidRuntime(1854): Shutting down VM
08-02 13:15:42.750: W/dalvikvm(1854): threadid=1: thread exiting with uncaught exception (group=0x40014760)
08-02 13:15:42.781: E/AndroidRuntime(1854): FATAL EXCEPTION: main
08-02 13:15:42.781: E/AndroidRuntime(1854): java.lang.NullPointerException
08-02 13:15:42.781: E/AndroidRuntime(1854): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:398)
08-02 13:15:42.781: E/AndroidRuntime(1854): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:366)
08-02 13:15:42.781: E/AndroidRuntime(1854): at android.widget.AbsListView.obtainView(AbsListView.java:1949)
08-02 13:15:42.781: E/AndroidRuntime(1854): at android.widget.ListView.measureHeightOfChildren(ListView.java:1228)
08-02 13:15:42.781: E/AndroidRuntime(1854): at android.widget.ListView.onMeasure(ListView.java:1139)
08-02 13:15:42.781: E/AndroidRuntime(1854): at android.view.View.measure(View.java:10828)
08-02 13:15:42.781: E/AndroidRuntime(1854): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581)
08-02 13:15:42.781: E/AndroidRuntime(1854): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365)
08-02 13:15:42.781: E/AndroidRuntime(1854): at android.view.View.measure(View.java:10828)
08-02 13:15:42.781: E/AndroidRuntime(1854): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
08-02 13:15:42.781: E/AndroidRuntime(1854): at android.widget.FrameLayout.onMeasure(FrameLayout.java:267)
08-02 13:15:42.781: E/AndroidRuntime(1854): at android.view.View.measure(View.java:10828)
08-02 13:15:42.781: E/AndroidRuntime(1854): at android.widget.LinearLayout.measureVertical(LinearLayout.java:764)
08-02 13:15:42.781: E/AndroidRuntime(1854): at android.widget.LinearLayout.onMeasure(LinearLayout.java:519)
08-02 13:15:42.781: E/AndroidRuntime(1854): at android.view.View.measure(View.java:10828)
08-02 13:15:42.781: E/AndroidRuntime(1854): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
08-02 13:15:42.781: E/AndroidRuntime(1854): at android.widget.FrameLayout.onMeasure(FrameLayout.java:267)
08-02 13:15:42.781: E/AndroidRuntime(1854): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:1889)
08-02 13:15:42.781: E/AndroidRuntime(1854): at android.view.View.measure(View.java:10828)
08-02 13:15:42.781: E/AndroidRuntime(1854): at android.view.ViewRoot.performTraversals(ViewRoot.java:938)
08-02 13:15:42.781: E/AndroidRuntime(1854): at android.view.ViewRoot.handleMessage(ViewRoot.java:2040)
08-02 13:15:42.781: E/AndroidRuntime(1854): at android.os.Handler.dispatchMessage(Handler.java:99)
08-02 13:15:42.781: E/AndroidRuntime(1854): at android.os.Looper.loop(Looper.java:132)
08-02 13:15:42.781: E/AndroidRuntime(1854): at android.app.ActivityThread.main(ActivityThread.java:4123)
08-02 13:15:42.781: E/AndroidRuntime(1854): at java.lang.reflect.Method.invokeNative(Native Method)
08-02 13:15:42.781: E/AndroidRuntime(1854): at java.lang.reflect.Method.invoke(Method.java:491)
08-02 13:15:42.781: E/AndroidRuntime(1854): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
08-02 13:15:42.781: E/AndroidRuntime(1854): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
08-02 13:15:42.781: E/AndroidRuntime(1854): at dalvik.system.NativeStart.main(Native Method)
08-02 13:16:04.090: I/Process(1854): Sending signal. PID: 1854 SIG: 9
代码:
import android.os.Bundle;
import android.app.Activity;
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.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainClient extends Activity {
TextView txt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_client);
String[] listeStrings = new String[999];
listeStrings = getServerData(strURL);
ListView lvListe = (ListView)findViewById(R.id.ClientList);
Log.i("test tableau", listeStrings[0]);
lvListe.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,listeStrings));
Log.i("test 2 tableau", listeStrings[1]);
}
public static final String strURL = "http://www.kogitus.com/test/ville.php";
private String[] getServerData(String returnString) {
InputStream is = null;
String result = "";
String[] listeStrings = new String[999];
// Envoyer la requête au script PHP.
// Script PHP : $sql=mysql_query("select * from tblVille where Nom_ville like '".$_REQUEST['ville']."%'");
// $_REQUEST['ville'] sera remplacé par L dans notre exemple.
// Ce qui veut dire que la requête enverra les villes commençant par la lettre L
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("ville","L"));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(strURL);
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());
}
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
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());
}
try{
JSONArray jArray = new JSONArray(result);
returnString = "";
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
// Affichage ID_ville et Nom_ville dans le LogCat
Log.i("log_tag","ID_ville: "+json_data.getInt("ID_ville")+
", Nom_ville: "+json_data.getString("Nom_ville"));
listeStrings[i] = json_data.getString("Nom_ville");
// Résultats de la requête
//if (returnString == "")
//returnString += json_data.getString("Nom_ville");
//else
//returnString += "\n" + jArray.getJSONObject(i);
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data " + e.toString());
}
return listeStrings;
}
}
和他的XML文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/wallpaper" >
<ImageView
android:id="@+id/BottomBar"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:scaleType="fitXY"
android:src="@drawable/bottombar" />
<ImageView
android:id="@+id/MenuClient"
android:layout_width="453dp"
android:layout_height="620dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scaleType="fitXY"
android:src="@drawable/menuclient" />
<ImageView
android:id="@+id/RightBar"
android:layout_width="160dp"
android:layout_height="620dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:scaleType="fitXY"
android:src="@drawable/rightbar" />
<Button
android:id="@+id/BoutonBills"
style="@android:style/Animation.Activity"
android:layout_width="152dp"
android:layout_height="129dp"
android:layout_marginTop="480dp"
android:layout_alignParentRight="true"
android:background="@drawable/bills" />
<Button
android:id="@+id/BoutonProviders"
style="@android:style/Animation.Activity"
android:layout_width="152dp"
android:layout_height="129dp"
android:layout_marginTop="360dp"
android:layout_alignParentRight="true"
android:background="@drawable/providers" />
<Button
android:id="@+id/BoutonPieces"
style="@android:style/Animation.Activity"
android:layout_width="152dp"
android:layout_height="129dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="120dp"
android:background="@drawable/pieces" />
<Button
android:id="@+id/BoutonTrucks"
style="@android:style/Animation.Activity"
android:layout_width="152dp"
android:layout_height="129dp"
android:layout_alignParentRight="true"
android:layout_marginTop="240dp"
android:background="@drawable/trucks" />
<ListView
android:id="@+id/ClientList"
android:layout_width="453dp"
android:layout_height="600dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
答案 0 :(得分:1)
你的代码中有一个java.lang.NullPointerException。确保您不访问任何未初始化的对象。
意思是:
Object myObj;
myObj.doSomething();
将引发java.lang.NullPointerException
您需要此订单:
Object myObj;
myObj = new Object();
myObj.doSomething();
没有代码,我无法分辨这种情况发生在哪里
答案 1 :(得分:0)
您可以像这样声明列表数组:
String[] listeStrings = new String[999];
然后检索服务器数据,如下所示:
listeStrings = getServerData(strURL);
我的问题是:您确定在检索服务器数据后,列表中只包含999个元素吗?
我假设getServerData()
返回的元素少于999.在这种情况下,当您将listeString
传递给ArrayAdapter
时,ArrayAdapter将尝试逐个获取所有元素数组中的记录,有时会到达元素为空的位置,从而产生NullPointerException
。
解决方案是为您的字符串数组分配尽可能多的位置,因为JSON数组会返回许多元素
但是,我建议使用ArrayList
代替String[]
数组,它们会以动态方式分配。