-1

Good day! Noob here on android java.

I've been searching the entire Stackoverflow and asking a lot of questions about Intent. This is probably the last post that I'm going to do because I've been flooding the StackOverFlow with redundant questions about Intent.

I don't want to be totally spoon fed so I created two quick android application. One for the main launcher and the second one is for the application to be called. I've added a button on my main launcher and below is my code for that button that seems to be correct base on this tutorial (https://www.youtube.com/watch?v=qurvm-E9AiU).

public void ClickMe(View v) {
    Intent i=new Intent(this, com.idd.applicationtocall.MainActivity.class);
    startActivity(i);
}

After launching, the application crashes once I click on the button. I don't know what's wrong. I've almost tried all of the recommended answers which I think is all working for them. Below is a download link of the two quick applications that I created. I don't want you to finish it because I won't learn anything from it. I just wanted you guys to tell me what am I missing, what did I do wrong or what I did not understand about the Intent function.

https://www.mediafire.com/?wflsmaah5n7x49y

I'm using Java Eclipse and BlueStacks for my emulator.

Below is the logcat:

08-26 15:22:11.197: E/dalvikvm(13823): Could not find class 'com.idd.applicationtocall.MainActivity', referenced from method com.idd.applicationtolaunch.MainActivity.ClickMe
08-26 15:22:11.197: W/dalvikvm(13823): VFY: unable to resolve const-class 1140 (Lcom/idd/applicationtocall/MainActivity;) in Lcom/idd/applicationtolaunch/MainActivity;
08-26 15:22:11.197: D/dalvikvm(13823): VFY: replacing opcode 0x1c at 0x0002
08-26 15:22:11.227: I/PGA(13823): New SOCKET connection: icationtolaunch (pid 13823, tid 13823)
08-26 15:22:14.897: D/AndroidRuntime(13823): Shutting down VM
08-26 15:22:14.897: W/dalvikvm(13823): threadid=1: thread exiting with uncaught exception (group=0xb2c00180)
08-26 15:22:14.897: I/Process(13823): Sending signal. PID: 13823 SIG: 9
08-26 15:22:14.897: D/AndroidRuntime(13823): procName from cmdline: com.idd.applicationtolaunch
08-26 15:22:14.897: E/AndroidRuntime(13823): in writeCrashedAppName, pkgName :com.idd.applicationtolaunch
08-26 15:22:14.897: D/AndroidRuntime(13823): file written successfully with content: com.idd.applicationtolaunch StringBuffer : ;com.idd.applicationtolaunch
08-26 15:22:14.897: E/AndroidRuntime(13823): FATAL EXCEPTION: main
08-26 15:22:14.897: E/AndroidRuntime(13823): java.lang.IllegalStateException: Could not execute method of the activity
08-26 15:22:14.897: E/AndroidRuntime(13823):    at android.view.View$1.onClick(View.java:3044)
08-26 15:22:14.897: E/AndroidRuntime(13823):    at android.view.View.performClick(View.java:3511)
08-26 15:22:14.897: E/AndroidRuntime(13823):    at android.view.View$PerformClick.run(View.java:14105)
08-26 15:22:14.897: E/AndroidRuntime(13823):    at android.os.Handler.handleCallback(Handler.java:605)
08-26 15:22:14.897: E/AndroidRuntime(13823):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-26 15:22:14.897: E/AndroidRuntime(13823):    at android.os.Looper.loop(Looper.java:137)
08-26 15:22:14.897: E/AndroidRuntime(13823):    at android.app.ActivityThread.main(ActivityThread.java:4424)
08-26 15:22:14.897: E/AndroidRuntime(13823):    at java.lang.reflect.Method.invokeNative(Native Method)
08-26 15:22:14.897: E/AndroidRuntime(13823):    at java.lang.reflect.Method.invoke(Method.java:511)
08-26 15:22:14.897: E/AndroidRuntime(13823):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
08-26 15:23:39.927: E/dalvikvm(14122): Could not find class 'com.idd.applicationtocall.MainActivity', referenced from method com.idd.applicationtolaunch.MainActivity.ClickMe
08-26 15:23:39.927: W/dalvikvm(14122): VFY: unable to resolve const-class 1140 (Lcom/idd/applicationtocall/MainActivity;) in Lcom/idd/applicationtolaunch/MainActivity;
08-26 15:23:39.927: D/dalvikvm(14122): VFY: replacing opcode 0x1c at 0x0002
08-26 15:23:39.957: I/PGA(14122): New SOCKET connection: icationtolaunch (pid 14122, tid 14122)
Agent Smith
  • 23
  • 1
  • 7

3 Answers3

1

Use this in you OnClick:

public void ClickMe(View v) {
    Intent intent = new Intent();
    intent.setComponent(new ComponentName("com.idd.applicationtocall","com.idd.applicationtocall.MainActivity"));
    startActivity(intent);
}

And the manifest file of ApplicationToCall

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
            android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

You need to export the activity:

android:exported="true"

That way it works for me.

Wagner Michael
  • 2,027
  • 12
  • 28
  • The ApplicationToLaunch is the first form that's suppose to show up. The ApplicationToCall is the form that I want to show once I clicked the button. Shouldn't it be like Intent i=new Intent(this, com.idd.applicationtocall.MainActivity.class); because I'm trying to launch the applicationtocall.MainActivity? – Agent Smith Aug 26 '14 at 07:47
  • -this application works perfectly fine for me. I don't know why it didn't work out for you. I'm unable to help you with that because I'm unable to help myself. xD – Agent Smith Aug 26 '14 at 07:48
  • It still doesn't work. I also tried the setComponent function before. I've almost tried everything in the StackOverFlow but it doesn't seem to work. The last thing that I haven't thoroughly check is my Manifest file. – Agent Smith Aug 26 '14 at 07:50
  • Yes. I tried the setComponent. Its in the 3rd comment. I know that that code works for you guys. I also know that it supposed to work for me. I've been working for this for two weeks and I still have no progress... T-T – Agent Smith Aug 26 '14 at 07:55
  • i have edited and it works for me now. You forgot to export the activity of ApplicationToCall in its manifest.xml – Wagner Michael Aug 26 '14 at 07:57
  • I don't know how to thank you but you really gave me a relief!! So that's why it didn't work!! – Agent Smith Aug 26 '14 at 08:01
0

Your logcat says the activity class is not found. That might be because you didn't add it to your AndroidManifest.xml. You should add the tag

 <activity android:name="com.idd.applicationtocall.MainActivity"></activity>

to your ApplicationToLaunch AndroidManifest.xml.

southerton
  • 992
  • 8
  • 20
0

I managed to start the launcher activity of another application explicitly using the following codes. I found it on Stack Overflow, but I can't link it because I don't remember what I looked for to find it.

        if (v.getId() == R.id.main_button)
        {
            android.content.pm.PackageManager mPm = getActivity().getPackageManager();
            PackageInfo info = null;
            try
            {
                info = mPm.getPackageInfo("com.example.other.app", 0);
            }
            catch (PackageManager.NameNotFoundException e)
            {
                Log.d(this.getClass().getSimpleName(), e.getMessage() + "does not exist", e);
            }
            Boolean isAppInstalled = info != null;
            Log.i(getClass().getSimpleName(), "The package was found: " + isAppInstalled);
        }
        else if(v.getId() == R.id.main_action_button)
        {
            Intent LaunchIntent = getActivity().getPackageManager().getLaunchIntentForPackage("com.example.other.app");
            startActivity(LaunchIntent);
        }

But if you need to explicitly open a specific Activity with specific data, then you should probably specify a custom action, and try to launch the other app by using that.

Start Activity Using Custom Action

Community
  • 1
  • 1
EpicPandaForce
  • 71,034
  • 25
  • 221
  • 371