简体   繁体   中英

Implicit intent causes the app to crash at button press

In an attempt to understand how the implicit intents works, I wanted to create a layout with two edittext and one button . In the first edittext , the user should enter his/her the email address, and in the second textview he/she should enter the email address of the recipient, and when he/she clicks on the button , all the component registered for the Action SEND should appear, including my app.

Below are my attempts and the logcat :

UPDATED_JavaCode

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        String from = et_from.getText().toString();
        String to = et_to.getText().toString();
        String data = from + to;

        Intent i = new Intent();
        i.setAction(android.content.Intent.ACTION_SEND);
        i.setType("text/plain");
        i.putExtra(android.content.Intent.EXTRA_TEXT, from + to);
        //i.setData(Uri.parse(data.toString()));
        startActivity(i);
    }
});

UPDATED_Manifest :

<uses-sdk
    android:minSdkVersion="19"
    android:targetSdkVersion="19" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".EmailActivity"
        android:label="@string/title_activity_intents_test01" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="CustomActivity"> </activity>
    <intent-filter>
        <action android:name="android.intent.action.SEND" />
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:scheme="text/plain"/>
    </intent-filter>
</application>

Logcat :

11-15 14:47:19.714: E/AndroidRuntime(18239): FATAL EXCEPTION: main
11-15 14:47:19.714: E/AndroidRuntime(18239): Process: com.example.emailactivity, PID:18239
11-15 14:47:19.714: E/AndroidRuntime(18239): android.content.ActivityNotFoundException:
                    No Activity found to handle Intent { act=android.intent.action.SEND dat=yt (has extras)
                    }
11-15 14:47:19.714: E/AndroidRuntime(18239):     at
                    android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1672)
11-15 14:47:19.714: E/AndroidRuntime(18239):     at
                    android.app.Instrumentation.execStartActivity(Instrumentation.java:1442)
11-15 14:47:19.714: E/AndroidRuntime(18239):     at
                    com.example.emailactivity.EmailActivity$1.onClick(EmailActivity.java:46)

When using implicit intents it is possible that there are no applications that can handle your intent (which, between other problems, seems to be your problem). To avoid this, before calling startActivity() you should first verify that there is at least one application registered in the system that can handle the intent. To do this use resolveActivity() on your intent object:

// Verify that there are applications registered to handle this intent
// (resolveActivity returns null if none are registered)
if (sendIntent.resolveActivity(getPackageManager()) != null) {
    startActivity(sendIntent);
}

ACTION_SEND does not use setData() -- please remove that.

Also, please use the keys documented in the ACTION_SEND documentation for your extras. Note that there is no "from" and there is no "to" , so you can remove those extras. You will need to provide EXTRA_TEXT or EXTRA_STREAM to provide the actual content that you are sharing.

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