简体   繁体   中英

Not able to run an app on Android

I am parsing Json data from a URL in Android. I am not getting any error while executing. But nothing is coming up.

Here is my MainActivity.Java code:

public class MainActivity extends ListActivity {

/** Called when the activity is first created. */

@SuppressWarnings({ "rawtypes", "unchecked" })

@Override

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, 
 this.fetchTwitterPublicTimeline()));

}

public ArrayList<String> fetchTwitterPublicTimeline()

{

    ArrayList<String> listItems = new ArrayList<String>();

    try {

        URL twitter = new URL(

                "http://twitter.com/statuses/public_timeline.json");

        URLConnection tc = twitter.openConnection();

        BufferedReader in = new BufferedReader(new InputStreamReader(

                tc.getInputStream()));

        String line;

        while ((line = in.readLine()) != null) {

            JSONArray ja = new JSONArray(line);

            for (int i = 0; i < ja.length(); i++) {

                JSONObject jo = (JSONObject) ja.get(i);

                listItems.add(jo.getString("text"));

            }

        }

    } catch (MalformedURLException e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

    } catch (IOException e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

    } catch (JSONException e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

    }

    return listItems;

}

}

And this is my manifest.xml content:

  package="com.example.harsh"

  android:versionCode="1"

  android:versionName="1.0">

<uses-sdk android:minSdkVersion="8" />

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

 <application

    android:allowBackup="true"

    android:icon="@drawable/ic_launcher"

    android:label="@string/app_name"

    android:theme="@style/AppTheme" >

    <activity android:name=".MainActivity"

              android:label="Android List View">

        <intent-filter>

            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />

        </intent-filter>

    </activity>



</application>

This is the R.java file:

package com.example.harsh;

public final class R {


public static final class attr {

}

public static final class dimen {

public static final int activity_horizontal_margin=0x7f040000;


  public static final int activity_vertical_margin=0x7f040001;

}

public static final class drawable {

    public static final int ic_launcher=0x7f020000;

}

public static final class id {

    public static final int action_settings=0x7f080000;

}

public static final class layout {

    public static final int activity_main=0x7f030000;

}

public static final class menu {

    public static final int main=0x7f070000;

}

public static final class string {

    public static final int action_settings=0x7f050001;

    public static final int app_name=0x7f050000;

    public static final int hello_world=0x7f050002;

}

public static final class style {public static final int 
 activity_horizontal_margin=0x7f040000;

    public static final int activity_vertical_margin=0x7f040001;

}

public static final class drawable {

    public static final int ic_launcher=0x7f020000;

}

public static final class id {

    public static final int action_settings=0x7f080000;

}

public static final class layout {

    public static final int activity_main=0x7f030000;

}

public static final class menu {

    public static final int main=0x7f070000;

}

public static final class string {

    public static final int action_settings=0x7f050001;

    public static final int app_name=0x7f050000;

    public static final int hello_world=0x7f050002;

}

public static final class style {

public static final int AppBaseTheme=0x7f060000;

    public static final int AppTheme=0x7f060001;
}
}

While running it nothing is coming up after this:

[2013-10-15 23:37:39 - FirstApp] adb is running normally.

[2013-10-15 23:37:39 - FirstApp] Performing com.example.harsh.MainActivity activity launch

[2013-10-15 23:37:40 - FirstApp] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'abc'

[2013-10-15 23:37:40 - FirstApp] Uploading FirstApp.apk onto device 'emulator-5554'

[2013-10-15 23:37:46 - FirstApp] Installing FirstApp.apk...

[2013-10-15 23:38:26 - FirstApp] Success!

So i am not able to figure out what is the problem.

It looks like you're doing network/internet access on the main UI thread by calling your fetchTwitterPublicTimeline method in the activities onCreate. This will throw an exception in API levels newer than Honeycomb.

You should move that to an AsyncTask or background thread that is kicked off in the onCreate method and then fill your adapter when the AsyncTask completes and has data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM