我有一個方法,我用來接收一些信息。我向這個方法發送一個字符串(服務器方向)和兩個包含用戶和密碼的字符串,並且我收到一個包含所有信息的String []。然後,我使用String []值來訪問ArrayAdapter,但是我在logcat中顯示錯誤,我不知道爲什麼會出現此錯誤...在預覽版本中,我使用自己的值,但沒有「String []值」的作品完美,但現在我不知道它爲什麼不起作用。ListView錯誤android
String[] values;
@SuppressLint("InlinedApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE);
setContentView(R.layout.activity_select_environment);
myBundle = getIntent().getExtras();
user = myBundle.getString("user");
password = myBundle.getString("password");
values = request("http://myIp/XXX/post.php?request=1", user, password);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, android.R.id.text1, values);
}
另一種方法是在下:
public String[] request(String requesturl, String user, String password)
{
String result[] = new String[100];
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(requesturl);
String results = "";
//Varibles POST
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("user", user));
nameValuePairs.add(new BasicNameValuePair("password", password));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
response.getAllHeaders();
response.getEntity();
result[0] = "";
if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
results = EntityUtils.toString(response.getEntity());
if(!results.equals("-1"))
{
JSONObject jsonResponse = new JSONObject(results);
JSONArray jsonMainNode = jsonResponse.optJSONArray("edificios");
int lengthJsonArr =jsonMainNode.length();
for(int i = 0; i < lengthJsonArr; i++)
{
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
result[i] = jsonChildNode.optString("nombre");
}
}
}
return result;
}
catch(Exception ex)
{
result[0] = "error";
return result;
}
}
以下是錯誤的logcat的。
03-31 09:02:22.920: W/dalvikvm(2016): threadid=1: thread exiting with uncaught exception (group=0xb3b0ab90)
03-31 09:02:22.950: E/AndroidRuntime(2016): FATAL EXCEPTION: main
03-31 09:02:22.950: E/AndroidRuntime(2016): Process: com.example.en4dis, PID: 2016
03-31 09:02:22.950: E/AndroidRuntime(2016): java.lang.NullPointerException
03-31 09:02:22.950: E/AndroidRuntime(2016): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
03-31 09:02:22.950: E/AndroidRuntime(2016): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
03-31 09:02:22.950: E/AndroidRuntime(2016): at android.widget.AbsListView.obtainView(AbsListView.java:2240)
03-31 09:02:22.950: E/AndroidRuntime(2016): at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
03-31 09:02:22.950: E/AndroidRuntime(2016): at android.widget.ListView.onMeasure(ListView.java:1175)
03-31 09:02:22.950: E/AndroidRuntime(2016): at android.view.View.measure(View.java:16458)
03-31 09:02:22.950: E/AndroidRuntime(2016): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:689)
03-31 09:02:22.950: E/AndroidRuntime(2016): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:473)
03-31 09:02:22.950: E/AndroidRuntime(2016): at android.view.View.measure(View.java:16458)
03-31 09:02:22.950: E/AndroidRuntime(2016): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-31 09:02:22.950: E/AndroidRuntime(2016): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-31 09:02:22.950: E/AndroidRuntime(2016): at android.view.View.measure(View.java:16458)
03-31 09:02:22.950: E/AndroidRuntime(2016): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-31 09:02:22.950: E/AndroidRuntime(2016): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
03-31 09:02:22.950: E/AndroidRuntime(2016): at android.view.View.measure(View.java:16458)
03-31 09:02:22.950: E/AndroidRuntime(2016): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
03-31 09:02:22.950: E/AndroidRuntime(2016): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-31 09:02:22.950: E/AndroidRuntime(2016): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2289)
03-31 09:02:22.950: E/AndroidRuntime(2016): at android.view.View.measure(View.java:16458)
03-31 09:02:22.950: E/AndroidRuntime(2016): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1914)
03-31 09:02:22.950: E/AndroidRuntime(2016): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1111)
03-31 09:02:22.950: E/AndroidRuntime(2016): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1293)
03-31 09:02:22.950: E/AndroidRuntime(2016): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
03-31 09:02:22.950: E/AndroidRuntime(2016): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5582)
03-31 09:02:22.950: E/AndroidRuntime(2016): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
03-31 09:02:22.950: E/AndroidRuntime(2016): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
03-31 09:02:22.950: E/AndroidRuntime(2016): at android.view.Choreographer.doFrame(Choreographer.java:532)
03-31 09:02:22.950: E/AndroidRuntime(2016): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
03-31 09:02:22.950: E/AndroidRuntime(2016): at android.os.Handler.handleCallback(Handler.java:733)
03-31 09:02:22.950: E/AndroidRuntime(2016): at android.os.Handler.dispatchMessage(Handler.java:95)
03-31 09:02:22.950: E/AndroidRuntime(2016): at android.os.Looper.loop(Looper.java:137)
03-31 09:02:22.950: E/AndroidRuntime(2016): at android.app.ActivityThread.main(ActivityThread.java:4998)
03-31 09:02:22.950: E/AndroidRuntime(2016): at java.lang.reflect.Method.invokeNative(Native Method)
03-31 09:02:22.950: E/AndroidRuntime(2016): at java.lang.reflect.Method.invoke(Method.java:515)
03-31 09:02:22.950: E/AndroidRuntime(2016): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-31 09:02:22.950: E/AndroidRuntime(2016): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-31 09:02:22.950: E/AndroidRuntime(2016): at dalvik.system.NativeStart.main(Native Method)
它找不到xml佈局文件。可能是您用來創建適配器視圖的那些(請參閱構造函數調用)(android.R.layout.simple_expandable_list_item_1,android.R.id.text1)。你導入android.R嗎?因爲你確實參考了它。請記住,android.r不包括你的自定義xml文件(R.layout.activity_select_environment) – Bram
我試着再做一次,編寫它,發現R和所有這一切,它找到所有正確的,但是當我編譯我有同樣的錯誤。當我將它與預定義的值一起使用時,它可能很重要,但它現在有這個錯誤? – Imrik
如果我使用例如:values = new String [] {「Edificio1」,「Edificio2」,「Edificio3」};那麼listview加載正確...出了什麼問題? – Imrik