简体   繁体   English

隐式意图导致应用程序在按下按钮时崩溃

[英]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 .为了理解隐式意图是如何工作的,我想创建一个包含两个edittext和一个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.在第一个edittext 中,用户应该输入他/她的电子邮件地址,在第二个textview 中他/她应该输入收件人的电子邮件地址,当他/她点击按钮时,所有组件都为 Action 注册SEND 应该出现,包括我的应用程序。

Below are my attempts and the logcat :以下是我的尝试和logcat

UPDATED_JavaCode 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.为避免这种情况,在调用 startActivity() 之前,您应该首先验证系统中是否至少注册了一个可以处理该意图的应用程序。 To do this use resolveActivity() on your intent object:为此,请在您的意图对象上使用 resolveActivity():

// 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. ACTION_SEND不使用setData() -- 请删除它。

Also, please use the keys documented in the ACTION_SEND documentation for your extras.此外,请使用ACTION_SEND文档中记录的密钥作为您的附加功能。 Note that there is no "from" and there is no "to" , so you can remove those extras.请注意,没有"from"也没有"to" ,因此您可以删除这些额外内容。 You will need to provide EXTRA_TEXT or EXTRA_STREAM to provide the actual content that you are sharing.您需要提供EXTRA_TEXTEXTRA_STREAM以提供您共享的实际内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM