简体   繁体   English

Android StartActivityForResult无法开始活动?

[英]Android StartActivityForResult not starting the activity?

I'm working on an app that automates a bunch of e-mail sending, and I'm having trouble getting the activity that allows the user to select a file to start at all. 我正在开发一个可以自动执行大量电子邮件发送的应用程序,但是我无法获得允许用户选择一个文件来完全启动文件的活动。 Everything seems to be correct in the code, but when I'm stepping through instructions, it seems like the activity is never started at all. 代码中的所有内容似乎都是正确的,但是当我逐步执行说明时,似乎该活动根本没有开始。 Here's my code: 这是我的代码:

Calling Activity, EmailSender: 呼叫活动,EmailSender:

public class EmailSender extends Activity{
//declarations
Intent fileIntent;

    @Override
    public void onCreate(Bundle savedInstanceState) {

    //instantiations
    String pathName;
    fileIntent = new Intent(EmailSender.this, FileChooser.class);

    //email sending functions that work fine

    try {
        GmailSender attachmentSender = new GmailSender(gsn, gpw)

        String[] toArr = new String[6];    //array of recipient addresses
        toArr[0] = efull;
        toArr[1] = afull;
        toArr[2] = ysn;
        toArr[3] = csn;
        toArr[4] = hsn;
        toArr[5] = gsn;     

        attachmentSender.setSubject("Attachment Download Test");
        attachmentSender.setFrom(gsn);
        attachmentSender.setTo(toArr);
        attachmentSender.setBody("Attachment Downloading Test");

        startActivityForResult(fileIntent, 1);
        attachmentSender.addAttachment(pathName);
        attachmentSender.send();
    }
    catch (Exception e) {
        Log.e("EmailSender", e.getMessage(), e);
    }
    finish();

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if(requestCode == 1)
        {
            if(resultCode == RESULT_OK)
                pathName = data.getStringExtra("result");
        }
        if(resultCode == RESULT_CANCELED)
        {
            pathName = "";
        }
    }
}

The File Chooser is from the library posted in this question: Android file chooser 文件选择器来自以下问题中发布的库: Android文件选择器

Only the relevant methods of the class that extends the File Chooser there are posted below: 下面仅发布扩展文件选择器的类的相关方法:

public class FileChooser extends FileChooserActivity    {

    // TAG for log messages.
    private static final String TAG = "FileSelectorTestActivity";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // We must check to ensure that the calling Intent is not Intent.ACTION_GET_CONTENT
        if (!isIntentGetContent()) {
            // Display the file chooser with all file types
            showFileChooser();
        }
    }

    @Override
    protected void onFileSelect(File file) {
        if (file != null) {
            //final Context context = getApplicationContext();

            // Get the path of the Selected File.
            final String path = file.getAbsolutePath();
            Log.d(TAG, "File path: " + path);

            Intent returnIntent = new Intent();
            returnIntent.putExtra("result", path);
            setResult(RESULT_OK, returnIntent);   
            finish();
        }   
    }
}

Finally, here's the snippet of my manifest where the called class is declared: 最后,这是清单中声明被调用类的代码段:

<activity
    android:name=".FileChooser"
    android:label="Choose a file"
    android:exported="false" >
        <intent-filter>
            <action android:name="android.intent.action.GET_CONTENT" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.OPENABLE" />

            <data android:mimeType="*/*" />
        </intent-filter>
</activity>

I do not get any exceptions in Logcat until EmailSender attempts to attach an empty filename. 在EmailSender尝试附加空文件名之前,我在Logcat中没有任何异常。 The debugger pretty much just steps through instructions from the Android API until it comes back to the EmailSender activity and continues where it left off. 调试器几乎只是逐步浏览Android API中的指令,直到返回到EmailSender活动,并从中断处继续。 The only time I have an indication of possibly being able to select a file is once the exception has been thrown and logged and the code has paused after the finish(). 我唯一有可能选择文件的提示是,在throw()之后引发了异常并记录了异常并且代码已暂停。 At this point, a pop-up opens asking for a file choosing program to be selected (what's supposed to happen is that the built-in file chooser is used automatically). 此时,将打开一个弹出窗口,要求选择文件选择程序(应该发生的是自动使用内置文件选择器)。

If anyone can help me understand what's going on and why the FileChooser activity isn't being called in the first place, I'd really appreciate it. 如果有人可以帮助我了解发生了什么以及为什么不首先调用FileChooser活动,我将非常感激。 I've found a lot of resources on problems with OnActivityResult(), but unfortunately it's not even getting that far. 我发现了很多有关OnActivityResult()问题的资源,但不幸的是,它甚至还没有解决。 Thanks for any help! 谢谢你的帮助!

Um, right after your try catch block, you are finishing the activity. 嗯,在您的try catch块之后,您正在完成活动。 remove the finish() line so you can startActivityForResult and have something to come back to. 删除finish()行,以便您可以启动startActivityForResult并返回一些内容。

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

相关问题 Android:startActivityForResult在启动新活动期间崩溃 - Android: startActivityForResult crashes during starting new activity startActivityForResult 用于 3 个活动 Android - startActivityForResult usage for 3 activity Android Android-活动之外的startActivityForResult? - Android - startActivityForResult outside an Activity? android:Activity.startActivityForResult()或ActivityGroup? - android: Activity.startActivityForResult() or ActivityGroup? Android活动startActivityForResult没有被调用 - Android activity startActivityForResult not being called Android startActivityForResult 和子活动开始另一个活动 - Android startActivityForResult and child activity starts another activity Android:视图类和活动类的startActivityForResult和setResult - Android: startActivityForResult & setResult for a view class and an activity class Android:将StartActivityForResult设置为一个Activity,但将结果返回另一个 - Android: StartActivityForResult to one Activity but return result in another 调用startActivityForResult()时,Android 8.1关闭活动 - Android 8.1 Closes Activity when startActivityForResult() is called 在Android的非活动类中使用startActivityForResult() - Use startActivityForResult() in a non-activity Class in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM