如何在eclipse上组合2个Android项目?

时间:2014-09-02 16:16:48

标签: java android xml eclipse android-manifest

我将第一个项目添加为库。然后,我将这个库添加到第二个项目中。现在,在第二个项目中,我需要在第一个项目中从一个活动转移到另一个活动(该应用程序在此步骤中崩溃,但两个项目都运行良好)。我尝试了此链接http://developer.android.com/tools/projects/projects-eclipse.html步骤但没有更改。此外,我试图将第一个项目的文件添加到第二个项目,但出现同样的问题。 Logcat错误:

09-02 19:33:21.437: E/AndroidRuntime(27777): FATAL EXCEPTION: main
09-02 19:33:21.437: E/AndroidRuntime(27777): Process: com.vatrina.eg, PID: 27777
09-02 19:33:21.437: E/AndroidRuntime(27777): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vatrina.eg/com.vatrina.eg.CustomizedListView}: android.os.NetworkOnMainThreadException
09-02 19:33:21.437: E/AndroidRuntime(27777):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
09-02 19:33:21.437: E/AndroidRuntime(27777):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
09-02 19:33:21.437: E/AndroidRuntime(27777):    at android.app.ActivityThread.access$900(ActivityThread.java:161)
09-02 19:33:21.437: E/AndroidRuntime(27777):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
09-02 19:33:21.437: E/AndroidRuntime(27777):    at android.os.Handler.dispatchMessage(Handler.java:102)
09-02 19:33:21.437: E/AndroidRuntime(27777):    at android.os.Looper.loop(Looper.java:157)
09-02 19:33:21.437: E/AndroidRuntime(27777):    at android.app.ActivityThread.main(ActivityThread.java:5356)
09-02 19:33:21.437: E/AndroidRuntime(27777):    at java.lang.reflect.Method.invokeNative(Native Method)
09-02 19:33:21.437: E/AndroidRuntime(27777):    at java.lang.reflect.Method.invoke(Method.java:515)
09-02 19:33:21.437: E/AndroidRuntime(27777):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
09-02 19:33:21.437: E/AndroidRuntime(27777):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
09-02 19:33:21.437: E/AndroidRuntime(27777):    at dalvik.system.NativeStart.main(Native Method)
09-02 19:33:21.437: E/AndroidRuntime(27777): Caused by: android.os.NetworkOnMainThreadException
09-02 19:33:21.437: E/AndroidRuntime(27777):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1156)
09-02 19:33:21.437: E/AndroidRuntime(27777):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
09-02 19:33:21.437: E/AndroidRuntime(27777):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
09-02 19:33:21.437: E/AndroidRuntime(27777):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
09-02 19:33:21.437: E/AndroidRuntime(27777):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
09-02 19:33:21.437: E/AndroidRuntime(27777):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
09-02 19:33:21.437: E/AndroidRuntime(27777):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
09-02 19:33:21.437: E/AndroidRuntime(27777):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
09-02 19:33:21.437: E/AndroidRuntime(27777):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670)
09-02 19:33:21.437: E/AndroidRuntime(27777):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509)
09-02 19:33:21.437: E/AndroidRuntime(27777):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
09-02 19:33:21.437: E/AndroidRuntime(27777):    at com.vatrina.eg.XMLParser.getXmlFromUrl(XMLParser.java:45)
09-02 19:33:21.437: E/AndroidRuntime(27777):    at com.vatrina.eg.CustomizedListView.onCreate(CustomizedListView.java:41)
09-02 19:33:21.437: E/AndroidRuntime(27777):    at android.app.Activity.performCreate(Activity.java:5426)
09-02 19:33:21.437: E/AndroidRuntime(27777):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
09-02 19:33:21.437: E/AndroidRuntime(27777):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
09-02 19:33:21.437: E/AndroidRuntime(27777):    ... 11 more

1 个答案:

答案 0 :(得分:0)

问题可能是你没有在主项目android清单文件中从libary声明活动。

像这样:

   <application>

     <activity
        android:name="com.libary.activity.example"
        android:configChanges="orientation|screenSize" >
     </activity>

    </application>

但我只是在这里猜测。请发布错误消息和代码。


更新

查看错误消息Caused by: android.os.NetworkOnMainThreadException

似乎valbertos提到customlistview活动正在UIThread上执行网络操作。

一个非常讨厌的快速修复方法是将其添加到customlistview活动中。

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy); 

但只是暂时这样做,因为它会导致用户界面滞后。

将所有网络操作放在AsyncTask中要好得多。

点击here,了解AsyncTask的好消息。