I’m trying to teach myself to create android app in eclipse. My project is to create an app that shows pictures from an tumblr account.
I found som existing code from and similar app for twitter.
I have tried to alter the code in the way where i thought i my app for tumblr could run. After spending hours trying to figure out whats wrong and why the emulator keeps crashing i hope to find some help here.
My code looks like this:
package com.example.example;
public class Example extends 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;
}
}
}
The log in eclipse gives me these errors
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
Anybody that can spot the problem?
Any help is highly appriciated!
hard to tell without line numbers, but try this in
getTweetsinstead:also, you’ll need some exception handling around it
Related to JSONArray undefined methods:
Try editing your code which is creating the jsonArray:
try to use the specific methods when possible, instead of the generic
get()Not sure if this will help with the errors, but it will help keep the code a bit cleaner.
Update
It seems the original code was for JSONSimple.
Android comes with JSON parsing, so there isn’t much point in using this, unless you need something specific from it.
To parse using the Android default:
–
and remove the existing
JSONParsercode.