我正在尝试自己在eclipse中创建Android应用程序。我的项目是创建一个显示tumblr帐户图片的应用程序。
我发现了来自Twitter的现有代码以及类似的应用程序。
我试图改变代码的方式,我认为我的tumblr应用程序可以运行。花了好几个小时试图弄清楚什么是错的以及为什么模拟器不断崩溃我希望在这里找到一些帮助。
我的代码如下所示:
package com.example.example;
public class示例扩展了Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<Tweet> tweets = getTweets("my api key");
ListView listView = (ListView) findViewById(R.id.ListViewId);
listView.setAdapter(new UserItemAdapter(this, R.layout.listitem, tweets));
}
public class UserItemAdapter extends ArrayAdapter<Tweet> {
private ArrayList<Tweet> tweets;
public UserItemAdapter(Context context, int imageViewResourceId,
ArrayList<Tweet> tweets) {
super(context, imageViewResourceId, tweets);
this.tweets = tweets;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.listitem, null);
}
Tweet tweet = tweets.get(position);
if (tweet != null) {
ImageView image = (ImageView) v.findViewById(R.id.avatar);
if (image != null) {
image.setImageBitmap(getBitmap(tweet.image_url));
}
}
return v;
}
}
public Bitmap getBitmap(String bitmapUrl) {
try {
URL url = new URL(bitmapUrl);
return BitmapFactory.decodeStream(url.openConnection()
.getInputStream());
} catch (Exception ex) {
return null;
}
}
public ArrayList<Tweet> getTweets(String key) {
String searchUrl = "http://api.tumblr.com/v2/blog/www.richkidsofinstagram.tumblr.com/posts?api_key="
+ key;
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 (Object t : arr) {
Tweet tweet = new Tweet(((JSONObject) t).get("photos").toString());
tweets.add(tweet);
}
return tweets;
}
public class Tweet {
public String image_url;
public Tweet(String url) {
this.image_url = url;
}
}
}
eclipse中的日志给了我这些错误
08-30 15:58:34.549: D/dalvikvm(330): GC_FOR_MALLOC freed 1827 objects / 187744 bytes in 60ms
08-30 15:58:34.879: D/dalvikvm(330): GC_FOR_MALLOC freed 9343 objects / 277800 bytes in 60ms
08-30 15:58:34.969: D/AndroidRuntime(330): Shutting down VM
08-30 15:58:34.969: W/dalvikvm(330): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-30 15:58:34.989: E/AndroidRuntime(330): FATAL EXCEPTION: main
08-30 15:58:34.989: E/AndroidRuntime(330): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.example/com.example.example.Example}: java.lang.NullPointerException
08-30 15:58:34.989: E/AndroidRuntime(330): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-30 15:58:34.989: E/AndroidRuntime(330): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-30 15:58:34.989: E/AndroidRuntime(330): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-30 15:58:34.989: E/AndroidRuntime(330): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-30 15:58:34.989: E/AndroidRuntime(330): at android.os.Handler.dispatchMessage(Handler.java:99)
08-30 15:58:34.989: E/AndroidRuntime(330): at android.os.Looper.loop(Looper.java:123)
08-30 15:58:34.989: E/AndroidRuntime(330): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-30 15:58:34.989: E/AndroidRuntime(330): at java.lang.reflect.Method.invokeNative(Native Method)
08-30 15:58:34.989: E/AndroidRuntime(330): at java.lang.reflect.Method.invoke(Method.java:521)
08-30 15:58:34.989: E/AndroidRuntime(330): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-30 15:58:34.989: E/AndroidRuntime(330): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-30 15:58:34.989: E/AndroidRuntime(330): at dalvik.system.NativeStart.main(Native Method)
08-30 15:58:34.989: E/AndroidRuntime(330): Caused by: java.lang.NullPointerException
08-30 15:58:34.989: E/AndroidRuntime(330): at com.example.example.Example.getTweets(Example.java:119)
08-30 15:58:34.989: E/AndroidRuntime(330): at com.example.example.Example.onCreate(Example.java:35)
08-30 15:58:34.989: E/AndroidRuntime(330): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-30 15:58:34.989: E/AndroidRuntime(330): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-30 15:58:34.989: E/AndroidRuntime(330): ... 11 more
任何可以发现问题的人? 任何帮助都非常高兴!
答案 0 :(得分:0)
很难说没有行号,但请在getTweets
代替:
for ( int i=0 ; i<arr.length() ; i++ ) {
Tweet tweet = new Tweet( arr.getJSONObject( i ).getString( "photos" ) );
tweets.add( tweet );
}
另外,你需要处理一些异常处理
尝试编辑创建jsonArray的代码:
try {
arr = jsonObject.getJSONArray("results");
} catch (Exception ex) {
Log.v("TEST", "Exception: " + ex.getMessage());
}
尝试尽可能使用specific methods,而不是通用的get()
不确定这是否有助于解决错误,但这有助于保持代码更清晰。
原始代码似乎是针对JSONSimple。
Android附带了JSON解析,所以使用它没什么意义,除非你需要特定的东西。
使用Android默认值解析:
import org.json.JSONObject;
import org.json.JSONArray;
-
JSONObject jsonObject = new JSONObject(responseBody);
并删除现有的JSONParser
代码。