我在字符串数组中搜索一堆注释,因为我想在ListView中使用该字符串数组,但我不知道为什么我的应用程序崩溃...
例如,如果我使用以下字符串数组,它不会崩溃:
String[] values = new String[] { "value1",
"value2",
"value3",
"value4" };
但是当我尝试动态添加
的数据时String[] Comments=new String[20];
显示错误...
protected void onPostExecute(String result) {
super.onPostExecute(result);
JSONObject jsonObject;
try {
jsonObject = new JSONObject(result);
for (int i = 1; i <= jsonObject.length(); i++) {
JSONObject jsonInnerObject = jsonObject
.getJSONObject(Integer.toString(i));
Comment = jsonInnerObject.getString("comment_text");
Comment_owner = jsonInnerObject.getString("comment_owner");
Comments[i] = Comment;
}
} catch (JSONException e) {
e.printStackTrace();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(showStatus.this,
android.R.layout.simple_list_item_1, android.R.id.text1,
Comments);
listView.setAdapter(adapter);
}
我的json数据是:
{
"1": {
"comment_text": "its k...",
"comment_owner": "hello"
},
"2": {
"comment_text": "hello",
"comment_owner": "hello"
},
"3": {
"comment_text": "hmmmm....",
"comment_owner": "hello"
},
"4": {
"comment_text": "really...?",
"comment_owner": "hello"
}
}
log cat
05-26 23:45:31.807: E/AndroidRuntime(16334): FATAL EXCEPTION: main
05-26 23:45:31.807: E/AndroidRuntime(16334): java.lang.NullPointerException
05-26 23:45:31.807: E/AndroidRuntime(16334): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
05-26 23:45:31.807: E/AndroidRuntime(16334): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
05-26 23:45:31.807: E/AndroidRuntime(16334): at android.widget.AbsListView.obtainView(AbsListView.java:2340)
05-26 23:45:31.807: E/AndroidRuntime(16334): at android.widget.ListView.makeAndAddView(ListView.java:1793)
05-26 23:45:31.807: E/AndroidRuntime(16334): at android.widget.ListView.fillDown(ListView.java:678)
05-26 23:45:31.807: E/AndroidRuntime(16334): at android.widget.ListView.fillFromTop(ListView.java:739)
05-26 23:45:31.807: E/AndroidRuntime(16334): at android.widget.ListView.layoutChildren(ListView.java:1638)
05-26 23:45:31.807: E/AndroidRuntime(16334): at android.widget.AbsListView.onLayout(AbsListView.java:2170)
05-26 23:45:31.807: E/AndroidRuntime(16334): at android.view.View.layout(View.java:13846)
05-26 23:45:31.807: E/AndroidRuntime(16334): at android.view.ViewGroup.layout(ViewGroup.java:4466)
05-26 23:45:31.807: E/AndroidRuntime(16334): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
05-26 23:45:31.807: E/AndroidRuntime(16334): at android.view.View.layout(View.java:13846)
05-26 23:45:31.807: E/AndroidRuntime(16334): at android.view.ViewGroup.layout(ViewGroup.java:4466)
05-26 23:45:31.807: E/AndroidRuntime(16334): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
05-26 23:45:31.807: E/AndroidRuntime(16334): at android.view.View.layout(View.java:13846)
05-26 23:45:31.807: E/AndroidRuntime(16334): at android.view.ViewGroup.layout(ViewGroup.java:4466)
05-26 23:45:31.807: E/AndroidRuntime(16334): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
05-26 23:45:31.807: E/AndroidRuntime(16334): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
05-26 23:45:31.807: E/AndroidRuntime(16334): at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
05-26 23:45:31.807: E/AndroidRuntime(16334): at android.view.View.layout(View.java:13846)
05-26 23:45:31.807: E/AndroidRuntime(16334): at android.view.ViewGroup.layout(ViewGroup.java:4466)
05-26 23:45:31.807: E/AndroidRuntime(16334): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
05-26 23:45:31.807: E/AndroidRuntime(16334): at android.view.View.layout(View.java:13846)
05-26 23:45:31.807: E/AndroidRuntime(16334): at android.view.ViewGroup.layout(ViewGroup.java:4466)
05-26 23:45:31.807: E/AndroidRuntime(16334): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2168)
05-26 23:45:31.807: E/AndroidRuntime(16334): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1926)
05-26 23:45:31.807: E/AndroidRuntime(16334): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1140)
05-26 23:45:31.807: E/AndroidRuntime(16334): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4726)
05-26 23:45:31.807: E/AndroidRuntime(16334): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:747)
05-26 23:45:31.807: E/AndroidRuntime(16334): at android.view.Choreographer.doCallbacks(Choreographer.java:567)
05-26 23:45:31.807: E/AndroidRuntime(16334): at android.view.Choreographer.doFrame(Choreographer.java:536)
05-26 23:45:31.807: E/AndroidRuntime(16334): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:733)
05-26 23:45:31.807: E/AndroidRuntime(16334): at android.os.Handler.handleCallback(Handler.java:615)
05-26 23:45:31.807: E/AndroidRuntime(16334): at android.os.Handler.dispatchMessage(Handler.java:92)
05-26 23:45:31.807: E/AndroidRuntime(16334): at android.os.Looper.loop(Looper.java:153)
05-26 23:45:31.807: E/AndroidRuntime(16334): at android.app.ActivityThread.main(ActivityThread.java:5000)
05-26 23:45:31.807: E/AndroidRuntime(16334): at java.lang.reflect.Method.invokeNative(Native Method)
05-26 23:45:31.807: E/AndroidRuntime(16334): at java.lang.reflect.Method.invoke(Method.java:511)
05-26 23:45:31.807: E/AndroidRuntime(16334): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
05-26 23:45:31.807: E/AndroidRuntime(16334): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
05-26 23:45:31.807: E/AndroidRuntime(16334): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
您最好使用ArrayList
代替Array
,因为列表是动态的。
ArrayList<String> list = ArrayList<String>();
JSONObject jsonObject;
try {
jsonObject = new JSONObject(result);
for (int i = 1; i <= jsonObject.length(); i++) {
JSONObject jsonInnerObject = jsonObject
.getJSONObject(Integer.toString(i));
Comment = jsonInnerObject.getString("comment_text");
Comment_owner = jsonInnerObject.getString("comment_owner");
list.add(Comment);
}
} catch (JSONException e) {
e.printStackTrace();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(showStatus.this,
android.R.layout.simple_list_item_1, android.R.id.text1,
list);
listView.setAdapter(adapter);
也遵循java命名约定。你有以帽子开头的变量