无法在Android上运行应用

时间:2013-10-15 18:18:14

标签: java android

我正在从Android中的URL解析Json数据。执行时我没有收到任何错误。但没有任何事情发生。

这是我的MainActivity.Java代码:

公共类MainActivity扩展了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;

}

}

这是我的manifest.xml内容:

  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>

这是R.java文件:

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;
}
}

在运行它之后,没有任何事情发生了:

[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!

所以我无法弄清问题是什么。

1 个答案:

答案 0 :(得分:3)

通过在onCreate活动中调用fetchTwitterPublicTimeline方法,您似乎正在主UI线程上进行网络/ Internet访问。这将在比Honeycomb更新的API级别中引发异常。

您应该将其移动到在onCreate方法中启动的AsyncTask或后台线程,然后在AsyncTask完成并填充数据时填充适配器。