我只是想从特定的标签中获取所有推文,并在Android的列表视图中显示它们。我得到了一些没有任何错误的代码,但是当我运行应用程序时,Android意外关闭,我认为这可能是大多数问题所在:
public ArrayList<Tweet> getTweets(String searchTerm, int page) throws JSONException {
String searchUrl = "http://search.twitter.com/search.json?q=@"
+ searchTerm + "&rpp=100&page=" + page;
ArrayList<Tweet> tweets = new ArrayList<Tweet>();
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(searchUrl);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = null;
try{
responseBody = client.execute(get, responseHandler);
}catch(Exception ex) {
ex.printStackTrace();
}
JSONObject jsonObject = null;
JSONParser parser=new JSONParser();
try {
Object obj = parser.parse(responseBody);
jsonObject=(JSONObject)obj;
}catch(Exception ex){
Log.v("TEST","Exception: " + ex.getMessage());
}
JSONArray arr = null;
try {
Object j = jsonObject.get("results");
arr = (JSONArray)j;
}catch(Exception ex){
Log.v("TEST","Exception: " + ex.getMessage());
}
for(int i = 0; i < arr.length(); i++) {
JSONObject t = (JSONObject) arr.get(i);
Tweet tweet = new Tweet(
t.get("from_user").toString(),
t.get("text").toString(),
t.get("profile_image_url").toString()
);
tweets.add(tweet);
}
return tweets;
}
或者代码初始化时
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.info_main_layout);
ArrayList<Tweet> tweets = null;
try {
tweets = getTweets("android", 1);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ListView listView = (ListView) findViewById(R.id.ListViewId);
listView.setAdapter(new UserItemAdapter(this, R.layout.info_layout, tweets));
}
public class UserItemAdapter extends ArrayAdapter<Tweet> {
private ArrayList<Tweet> tweets;
public UserItemAdapter(Context context, int textViewResourceId, ArrayList<Tweet> tweets) {
super(context, textViewResourceId, tweets);
this.tweets = tweets;
}
在DDMS中我得到了这些编译错误:
03-23 16:14:02.253:E / AndroidRuntime(215):未捕获的处理程序:由于未捕获的异常导致主线程退出
03-23 16:14:02.263:E / AndroidRuntime(215):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.uwe.bus / com.uwe.bus.InfoActivity}: 显示java.lang.NullPointerException
03-23 16:14:02.263:E / AndroidRuntime(215):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
03-23 16:14:02.263:E / AndroidRuntime(215):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
03-23 16:14:02.263:E / AndroidRuntime(215):在android.app.ActivityThread.access $ 2200(ActivityThread.java:119)
03-23 16:14:02.263:E / AndroidRuntime(215):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1863)
03-23 16:14:02.263:E / AndroidRuntime(215):在android.os.Handler.dispatchMessage(Handler.java:99) 03-23 16:14:02.263:E / AndroidRuntime(215):在android.os.Looper.loop(Looper.java:123)
03-23 16:14:02.263:E / AndroidRuntime(215):在android.app.ActivityThread.main(ActivityThread.java:4363)
03-23 16:14:02.263:E / AndroidRuntime(215):at java.lang.reflect.Method.invokeNative(Native Method)
03-23 16:14:02.263:E / AndroidRuntime(215):at java.lang.reflect.Method.invoke(Method.java:521)
03-23 16:14:02.263:E / AndroidRuntime(215):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:860)
03-23 16:14:02.263:E / AndroidRuntime(215):引起:java.lang.NullPointerException 03-23 16:14:02.263:E / AndroidRuntime(215):at com.uwe.bus.InfoActivity.getTweets(InfoActivity.java:133)
03-23 16:14:02.263:E / AndroidRuntime(215):at com.uwe.bus.InfoActivity.onCreate(InfoActivity.java:39)
03-23 16:14:02.263:E / AndroidRuntime(215):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-23 16:14:02.263:E / AndroidRuntime(215):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
03-23 16:14:02.263:E / AndroidRuntime(215):... 11更多
第39行:
ArrayList<tweet> tweets = null;
try {
tweets = getTweets("android", 1);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
第133行:
for(int i = 0; i < arr.length(); i++) {
JSONObject t = (JSONObject) arr.get(i);
Tweet tweet = new Tweet(
t.get("from_user").toString(),
t.get("text").toString(),
t.get("profile_image_url").toString()
);
tweets.add(tweet);
}
第一个错误看起来像来自Manifest.xml,但清单一尘不染,所以我没有得到它。
或者有人知道在Android中显示推文的更简单方法......
答案 0 :(得分:0)
如果for循环是第133行。
您的arr
变量尚未实例化且为空