我正在尝试自学在 Eclipse 中创建 android 应用程序。我的项目是创建一个显示来自 tumblr 帐户的图片的应用程序。
我从 twitter 和类似的应用程序中找到了一些现有代码。
我试图以我认为我的 tumblr 应用程序可以运行的方式更改代码。在花了几个小时试图找出问题所在以及模拟器不断崩溃的原因之后,我希望在这里找到一些帮助。
我的代码如下所示:
包 com.example.example;
公共类示例扩展活动{
@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
任何人都可以发现问题?任何帮助都非常受欢迎!