gpt4 book ai didi

android - Android应用程式当机。将JSON数据放入Listview

转载 作者:行者123 更新时间:2023-12-03 16:54:45 27 4
gpt4 key购买 nike

我昨天刚刚发布,但是现在我试图将我的数据放到ListView而不是TextView中。调出ListView Activity 后,应用程序崩溃。如果有人对此有任何线索,将不胜感激。

这是 Activity 代码:

   package com.example.Accomplist;

import android.app.Activity;
import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.RemoteException;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

/**
* Created with IntelliJ IDEA.
* User: DESAI_628IL
* Date: 3/1/13
* Time: 7:34 PM
* To change this template use File | Settings | File Templates.
*/
public class MainScreen extends ListActivity{
JSONObject jsonObj= null;

String[] eventArr= new String[10];
int i=0;

// HttpClient client;
// url to make request
String[] urlArr = new String[10];//"http://accomplist.herokuapp.com/api/v1/sharedevent/1/?format=json";


//Following comment block is example of json object from above url
{
/*

{
"date":"2013-01-17T12:48:24.326859",
"event":{
"description":"I want to find work that I love so that I never have to work a day in my life.",
"id":3,
"resource_uri":"/api/v1/event/3/",
"title":{
"id":3,
"listitem":"Get a Job I Enjoy",
"resource_uri":"/api/v1/title/3/"
},
"user":{
"date_joined":"2013-01-17T12:40:17.115755",
"email":"sourabhdesai@gmail.com",
"first_name":"",
"id":3,
"is_active":true,
"is_staff":false,
"is_superuser":false,
"last_login":"2013-02-02T15:51:11.622505",
"last_name":"",
"password":"pbkdf2_sha256$10000$ETuiYJXQjvez$5yHd5IPaQpS0SvlissO/JydwXUHhMYDgadY7uyTyWg0=",
"resource_uri":"/api/v1/user/3/",
"username":"sourabhd"
}
},
"id":2,
"resource_uri":"/api/v1/sharedevent/2/",
"votes":7
}

*/
}
private static final String TAG_EVENT="event"; //A JSON object within the JSON object that will be returned by JSONParse()
private static final String TAG_DESCRIPTION="description"; //A JSON tag within the JSON object EVENT
private static String eventString="Yo";

protected void onCreate(Bundle savedInstanceState) {
for(int b=0; b<urlArr.length; b++) {
urlArr[b]= "http://accomplist.herokuapp.com/api/v1/sharedevent/"+(b+1)+"/?format=json";
}
super.onCreate(savedInstanceState);
setContentView(R.layout.main_screen);
ListView eventsList= getListView();
new JSONParse().execute(urlArr[0],urlArr[1],urlArr[2],urlArr[3],urlArr[4],urlArr[5],urlArr[6],urlArr[7],urlArr[8]);
}
private class JSONParse extends AsyncTask<String, Void, String> {
HttpClient client=new DefaultHttpClient();


@Override
protected String doInBackground(String... jsonurl) {
// StringBuilder url= new StringBuilder(String.valueOf(jsonurl));
HttpUriRequest request= new HttpGet("http://accomplist.herokuapp.com/api/v1/sharedevent/2/?format=json");
HttpResponse r= null;
try {
r = client.execute(request);
} catch (ClientProtocolException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
int status= r.getStatusLine().getStatusCode();
if (status==200){
HttpEntity e=r.getEntity();
String data= null;
try {
data = EntityUtils.toString(e);
} catch (IOException e1) {
e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
try {
jsonObj = new JSONObject(data);
} catch (JSONException e1) {
e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
try {
JSONObject eventJson= jsonObj.getJSONObject(TAG_EVENT);
eventString= eventJson.getString(TAG_DESCRIPTION);
}
catch (JSONException e1) {
eventString="Couldn't Parse Data";
}
return eventString;
}
else{
return eventString;
}
}
protected void onProgressUpdate() {
Toast loadingToast= Toast.makeText(getApplicationContext(), "Loading", Toast.LENGTH_LONG);
loadingToast.show();
}
protected void onPostExecute(String result) {
eventString=result;
eventArr[i]=eventString;
i++;
MainScreen.this.setListAdapter(new ArrayAdapter<String>(MainScreen.this,
android.R.layout.simple_list_item_1,eventArr));

}
}
}

这是LogCat错误消息:
02-26 15:57:21.692: WARN/InputMethodManagerService(324): Got RemoteException sending setActive(false) notification to pid 5900 uid 10048
02-26 15:57:31.964: WARN/dalvikvm(5918): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
02-26 15:57:32.042: ERROR/AndroidRuntime(5918): FATAL EXCEPTION: main
java.lang.NullPointerException
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
at android.widget.AbsListView.obtainView(AbsListView.java:2143)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
at android.widget.ListView.onMeasure(ListView.java:1158)
at android.view.View.measure(View.java:15518)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15518)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15518)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15518)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
at android.view.View.measure(View.java:15518)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)

最佳答案

对于初学者,此行

eventsList= getListView();

应该在 onCreate()内。但是将 ListView eventsList声明为字段变量,因为您只是在 onCreate()中初始化它

这是 NPE的来源,因为它从 ListView中获取了 Layout,而您直到调用 setContentView()或使用 inflate对其进行 inflater时才知道。因此,对 getListView()的调用也需要在 setContentView()之后

您还需要在 ListView中包含 android:id="@android:id/list"Layout
我建议您仔细阅读 ListActivity Docs,因为除非您了解它们,否则它们可能会很棘手。我还建议观看 Turbo Charge your UI,我相信我是通过@Sam了解到的。这是一个古老的但仍然有很多好的信息。它处理 UI的不同部分,但具有有关更有效地使用 ListView的良好信息。

编辑

就像它在这里说的那样:
Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

这就是为什么我最初在答案中添加此行的原因:)

You also will need a ListView with android:id="@android:id/list" in your Layout



The example from the Docs
<ListView android:id="@android:id/list"  //this is the important part
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>

关于android - Android应用程式当机。将JSON数据放入Listview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15279351/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com