Note that there are some explanatory texts on larger screens.

plurals
  1. POReceiving text from other apps via Share menu
    primarykey
    data
    text
    <p>I'm writing a language dictionary app for myself (as a kind of Android-programming learning exercise).</p> <p>I want my app to be listed as an option in the intent chooser of other apps, so I add these code lines to my manifest: </p> <pre><code> &lt;intent-filter&gt; &lt;action android:name="android.intent.action.SEND" /&gt; &lt;category android:name="android.intent.category.DEFAULT" /&gt; &lt;data android:mimeType="text/plain" /&gt; &lt;/intent-filter&gt; </code></pre> <p>My app successfully appears in the action menu of other apps (e.g. Browser), which is good. My app is also successfully called via Share when I choose it from Browser action menu.</p> <p>Now I want my app to receive and process text (a word) sent from other apps, and then automatically look the text up for its definition. I add the following code lines to the main activity of my app:</p> <pre><code>//(under onCreate) Intent intent = getIntent(); String action = intent.getAction(); String type = intent.getType(); EditText edittext = (EditText) findViewById(R.id.edWord); if (Intent.ACTION_SEND.equals(action) &amp;&amp; type != null) { if ("text/plain".equals(type)) { String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT); if (sharedText != null) { edittext.setText(sharedText); } } } </code></pre> <p>However at this stage, Browser gets crashed every time my app is chosen from the action menu via Share. </p> <p>So it comes to my questions:</p> <ol> <li><strong>What have I done wrong?</strong> </li> <li><strong>How/What could I do to "force" my app to accept text from other apps, and automatically display its definition?</strong></li> </ol> <p>Sorry for these are noobie questions (my headache, though). </p> <p>Thank you very much for your help.</p> <p><strong>UPDATE 1</strong></p> <p>Thanks, CommonWare. Nothing is produced when Browser is crashed but when I choose "Share" from the web view of my app, I get this log cat:</p> <pre><code>08-09 18:17:23.726: W/dalvikvm(1111): threadid=1: thread exiting with uncaught exception (group=0x409961f8) 08-09 18:17:23.776: E/AndroidRuntime(1111): FATAL EXCEPTION: main 08-09 18:17:23.776: E/AndroidRuntime(1111): java.lang.RuntimeException: Unable to start activity ComponentInfo{peter.kadict/peter.kadict.Kadict}: java.lang.NullPointerException 08-09 18:17:23.776: E/AndroidRuntime(1111): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955) 08-09 18:17:23.776: E/AndroidRuntime(1111): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980) 08-09 18:17:23.776: E/AndroidRuntime(1111): at android.app.ActivityThread.access$600(ActivityThread.java:122) 08-09 18:17:23.776: E/AndroidRuntime(1111): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146) 08-09 18:17:23.776: E/AndroidRuntime(1111): at android.os.Handler.dispatchMessage(Handler.java:99) 08-09 18:17:23.776: E/AndroidRuntime(1111): at android.os.Looper.loop(Looper.java:137) 08-09 18:17:23.776: E/AndroidRuntime(1111): at android.app.ActivityThread.main(ActivityThread.java:4340) 08-09 18:17:23.776: E/AndroidRuntime(1111): at java.lang.reflect.Method.invokeNative(Native Method) 08-09 18:17:23.776: E/AndroidRuntime(1111): at java.lang.reflect.Method.invoke(Method.java:511) 08-09 18:17:23.776: E/AndroidRuntime(1111): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 08-09 18:17:23.776: E/AndroidRuntime(1111): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 08-09 18:17:23.776: E/AndroidRuntime(1111): at dalvik.system.NativeStart.main(Native Method) 08-09 18:17:23.776: E/AndroidRuntime(1111): Caused by: java.lang.NullPointerException 08-09 18:17:23.776: E/AndroidRuntime(1111): at peter.kadict.Kadict.onCreate(Kadict.java:449) 08-09 18:17:23.776: E/AndroidRuntime(1111): at android.app.Activity.performCreate(Activity.java:4465) 08-09 18:17:23.776: E/AndroidRuntime(1111): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 08-09 18:17:23.776: E/AndroidRuntime(1111): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919) 08-09 18:17:23.776: E/AndroidRuntime(1111): ... 11 more </code></pre> <p><strong>UPDATE 2</strong></p> <p>This code has solved the problem thanks to <strong>CommonsWare</strong>. </p> <pre><code> Intent intent = getIntent(); String action = intent.getAction(); String type = intent.getType(); if (Intent.ACTION_SEND.equals(action) &amp;&amp; type != null) { if ("text/plain".equals(type)) { String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT); if (sharedText != null) { setContentView(R.layout.main); EditText edittext = (EditText) findViewById(R.id.edWord); edittext.setText(sharedText); } } } </code></pre> <p>But it does not do its task: the app is loaded but does not seem to receive and process the text (word) sent from other apps. </p> <p>Can you you guys help? Many thanks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload