Solace
Solace

Reputation: 9020

Why doesn't my App Chooser dialogue show?

I am using this tutorial to learn about the basics of Intents in Android. The code sample I am using is adopted from the section named "Force an App Chooser" on the page linked.

The following is the method which should be invoked on the click of the button.

public void startSecondActivity(View view) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        String fileChooserLabel = getResources().getString(R.string.fileChooserLabel); 

        Intent fileChooserIntent = Intent.createChooser(intent, fileChooserLabel);
        if (intent.resolveActivity(getPackageManager())!=null) { 
            startActivity(intent);
        } else {
            textView = (TextView) findViewById(R.id.text_view); 
            textView.setText("False");
        }

}

But it just enters the else block of the if-else conditional. I tried this app on both a real device and the emulator. So can anybody point out what might be wrong here, and what I can do about it.

Note: I did not add anything to the Manifest file, since I am using Eclipse IDE and I suppose whatever is required at this point is automatically added to the manifest file.

Upvotes: 0

Views: 72

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007554

It is returning null because there are no activities on the device that support your Intent. In this case, your ACTION_SEND Intent is not properly set up.

Note that the code sample you used as a reference is not a tutorial. It is not designed to be a complete code sample. In fact, what they list there will not even compile, as their ... is meant to be replaced by your own code to complete setting up the Intent.

You will need to fully configure your ACTION_SEND Intent, most notably setting the MIME type, as is covered elsewhere in the documentation. Replacing:

Intent intent = new Intent(Intent.ACTION_SEND);

with something like:

Intent intent = new Intent(Intent.ACTION_SEND)
                      .setType("text/plain")
                      .putExtra(Intent.EXTRA_TEXT, "IM IN UR STAK OVERFLO ANZWR");

should suffice.

Upvotes: 1

Related Questions