在Android中的第一个应用程序启动第二个应用程序的活

时间:2014-02-12 13:18:36

标签: java android android-intent runtime-error

我是Android开发的新手。我试图在按钮点击的第一个应用程序启动第二个应用程序的活动。但我的应用程序被停止了。我可能会对意图做错事。 这是LOGCAT:

02-12 18:06:50.948: D/gralloc_goldfish(1247): Emulator without GPU emulation detected.
02-12 18:07:02.338: D/AndroidRuntime(1247): Shutting down VM
02-12 18:07:02.338: W/dalvikvm(1247): threadid=1: thread exiting with uncaught exception (group=0xb3ad1b90)
02-12 18:07:02.528: E/AndroidRuntime(1247): FATAL EXCEPTION: main
02-12 18:07:02.528: E/AndroidRuntime(1247): Process: smd.homework1.question2, PID: 1247
02-12 18:07:02.528: E/AndroidRuntime(1247): java.lang.IllegalStateException: Could not execute method of the activity
02-12 18:07:02.528: E/AndroidRuntime(1247):     at android.view.View$1.onClick(View.java:3814)
02-12 18:07:02.528: E/AndroidRuntime(1247):     at android.view.View.performClick(View.java:4424)
02-12 18:07:02.528: E/AndroidRuntime(1247):     at android.view.View$PerformClick.run(View.java:18383)
02-12 18:07:02.528: E/AndroidRuntime(1247):     at android.os.Handler.handleCallback(Handler.java:733)
02-12 18:07:02.528: E/AndroidRuntime(1247):     at android.os.Handler.dispatchMessage(Handler.java:95)
02-12 18:07:02.528: E/AndroidRuntime(1247):     at android.os.Looper.loop(Looper.java:137)
02-12 18:07:02.528: E/AndroidRuntime(1247):     at android.app.ActivityThread.main(ActivityThread.java:4998)
02-12 18:07:02.528: E/AndroidRuntime(1247):     at java.lang.reflect.Method.invokeNative(Native Method)
02-12 18:07:02.528: E/AndroidRuntime(1247):     at java.lang.reflect.Method.invoke(Method.java:515)
02-12 18:07:02.528: E/AndroidRuntime(1247):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
02-12 18:07:02.528: E/AndroidRuntime(1247):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
02-12 18:07:02.528: E/AndroidRuntime(1247):     at dalvik.system.NativeStart.main(Native Method)
02-12 18:07:02.528: E/AndroidRuntime(1247): Caused by: java.lang.reflect.InvocationTargetException
02-12 18:07:02.528: E/AndroidRuntime(1247):     at java.lang.reflect.Method.invokeNative(Native Method)
02-12 18:07:02.528: E/AndroidRuntime(1247):     at java.lang.reflect.Method.invoke(Method.java:515)
02-12 18:07:02.528: E/AndroidRuntime(1247):     at android.view.View$1.onClick(View.java:3809)
02-12 18:07:02.528: E/AndroidRuntime(1247):     ... 11 more
02-12 18:07:02.528: E/AndroidRuntime(1247): Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=textViewActivity }
02-12 18:07:02.528: E/AndroidRuntime(1247):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1632)
02-12 18:07:02.528: E/AndroidRuntime(1247):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
02-12 18:07:02.528: E/AndroidRuntime(1247):     at android.app.Activity.startActivityForResult(Activity.java:3423)
02-12 18:07:02.528: E/AndroidRuntime(1247):     at android.app.Activity.startActivityForResult(Activity.java:3384)
02-12 18:07:02.528: E/AndroidRuntime(1247):     at android.app.Activity.startActivity(Activity.java:3626)
02-12 18:07:02.528: E/AndroidRuntime(1247):     at android.app.Activity.startActivity(Activity.java:3594)
02-12 18:07:02.528: E/AndroidRuntime(1247):     at smd.homework1.question2.MainActivity.function1(MainActivity.java:36)
02-12 18:07:02.528: E/AndroidRuntime(1247):     ... 14 more

第36行是startActivity(i); 按钮点击功能是:

public void function1(View v){
    Intent i= new Intent("textViewActivity");
    startActivity(i);
}

的主要内容中
<activity
    android:name="Activity2"
    android:label="@string/a2" >
    <intent-filter>
        <action android:name="textViewActivity" />
    </intent-filter>
</activity>

2 个答案:

答案 0 :(得分:3)

在android中,每个应用程序都是一个单独的进程,除了通过意图之外你不能进行任何通信,所以你需要从app1发送一个动作意图并在app2中实现一个捕获意图并激活你的活动的广播接收器。

app1->activityA->send intent->app2 receiver->start activity-> activityB

here是官方文档提供的另一种方式,您可以跳过broacastreceivers,因为您的活动将成为某个intent filter的接收者

答案 1 :(得分:2)

AndroidManifest.xml中的活动声明为

<activity
     android:name="Activity2"
     android:label="@string/a2"
     android:exported="true" >
</activity>

执行它的代码是:

Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example", "com.example.Activity2"));
startActivity(intent);

com.example替换为第二个应用包名称,将com.example.Activity2替换为包限定活动名称。


AndroidManifest.xml中的活动声明为

<activity
     android:name="Activity2"
     android:label="@string/a2" >
     <intent-filter>
         <action android:name="com.example.myintent" />
     </intent-filter>
</activity>

执行它的代码是:

Intent intent = new Intent();
intent.setAction("com.example.myintent");
startActivity(intent);

com.example.myintent替换为您的意图名称。