views:

882

answers:

2

Hi,

My app offers a "share/tell-a-friend" function. When the "share" button is pressed the following method is being called to open a list of apps, which can perform the action (Eg. Gmail, Twittroid, Facebook...):

public void share() {
   Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
   shareIntent.setType("text/plain");
   shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, getText(R.string.menu_share_subject));
   shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, getText(R.string.menu_share_body));

   startActivity(Intent.createChooser(shareIntent, getText(R.string.menu_share_intent)));      
  }

The sharing functionality works basically. But when the sharing app (Facebook, Twitter, ...) tries to return to my app, a force close is thrown.

I guess that my app gets closed in the background during the sharing process... at least that is what the debugger sais...

Any ideas?


The problem seems to be that the app closes in the background. Also when I open the browser from my app an try to return to my app with the back key. A force close is thrown. This is the Log Cat output

04-13 22:28:42.003: ERROR/AndroidRuntime(18915): Uncaught handler: thread main exiting due to uncaught exception 04-13 22:28:42.253: ERROR/AndroidRuntime(18915): java.lang.RuntimeException: Unable to start activity ComponentInfo{eu.xxx.xxx/eu.xxx.xxx.xxx}: java.lang.NullPointerException 04-13 22:28:42.253: ERROR/AndroidRuntime(18915): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401) 04-13 22:28:42.253: ERROR/AndroidRuntime(18915): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417) 04-13 22:28:42.253: ERROR/AndroidRuntime(18915): at android.app.ActivityThread.access$2100(ActivityThread.java:116) 04-13 22:28:42.253: ERROR/AndroidRuntime(18915): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 04-13 22:28:42.253: ERROR/AndroidRuntime(18915): at android.os.Handler.dispatchMessage(Handler.java:99) 04-13 22:28:42.253: ERROR/AndroidRuntime(18915): at android.os.Looper.loop(Looper.java:123) 04-13 22:28:42.253: ERROR/AndroidRuntime(18915): at android.app.ActivityThread.main(ActivityThread.java:4203) 04-13 22:28:42.253: ERROR/AndroidRuntime(18915): at java.lang.reflect.Method.invokeNative(Native Method) 04-13 22:28:42.253: ERROR/AndroidRuntime(18915): at java.lang.reflect.Method.invoke(Method.java:521) 04-13 22:28:42.253: ERROR/AndroidRuntime(18915): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 04-13 22:28:42.253: ERROR/AndroidRuntime(18915): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549) 04-13 22:28:42.253: ERROR/AndroidRuntime(18915): at dalvik.system.NativeStart.main(Native Method) 04-13 22:28:42.253: ERROR/AndroidRuntime(18915): Caused by: java.lang.NullPointerException 04-13 22:28:42.253: ERROR/AndroidRuntime(18915): at eu.xxx.xxx.xxx.fillData(xxx.java:178) 04-13 22:28:42.253: ERROR/AndroidRuntime(18915): at eu.xxx.xxx.xxx.access$1(xxx.java:173) 04-13 22:28:42.253: ERROR/AndroidRuntime(18915): at eu.xxx.xxx.xxx$1.onTextChanged(xxx.java:139) 04-13 22:28:42.253: ERROR/AndroidRuntime(18915): at android.widget.TextView.sendOnTextChanged(TextView.java:6096) 04-13 22:28:42.253: ERROR/AndroidRuntime(18915): at android.widget.TextView.setText(TextView.java:2677) 04-13 22:28:42.253: ERROR/AndroidRuntime(18915): at android.widget.TextView.setText(TextView.java:2542) 04-13 22:28:42.253: ERROR/AndroidRuntime(18915): at android.widget.EditText.setText(EditText.java:71) 04-13 22:28:42.253: ERROR/AndroidRuntime(18915): at android.widget.TextView.setText(TextView.java:2517) 04-13 22:28:42.253: ERROR/AndroidRuntime(18915): at android.widget.TextView.onRestoreInstanceState(TextView.java:2417) 04-13 22:28:42.253: ERROR/AndroidRuntime(18915): at android.view.View.dispatchRestoreInstanceState(View.java:5689) 04-13 22:28:42.253: ERROR/AndroidRuntime(18915): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:1125) 04-13 22:28:42.253: ERROR/AndroidRuntime(18915): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:1125) 04-13 22:28:42.253: ERROR/AndroidRuntime(18915): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:1125) 04-13 22:28:42.253: ERROR/AndroidRuntime(18915): at android.view.View.restoreHierarchyState(View.java:5668) 04-13 22:28:42.253: ERROR/AndroidRuntime(18915): at com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:1506) 04-13 22:28:42.253: ERROR/AndroidRuntime(18915): at android.app.Activity.onRestoreInstanceState(Activity.java:833) 04-13 22:28:42.253: ERROR/AndroidRuntime(18915): at android.app.ListActivity.onRestoreInstanceState(ListActivity.java:221) 04-13 22:28:42.253: ERROR/AndroidRuntime(18915): at android.app.Activity.performRestoreInstanceState(Activity.java:805) 04-13 22:28:42.253: ERROR/AndroidRuntime(18915): at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1172) 04-13 22:28:42.253: ERROR/AndroidRuntime(18915): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2378) 04-13 22:28:42.253: ERROR/AndroidRuntime(18915): ... 11 more
+4  A: 
04-13 22:28:42.253: ERROR/AndroidRuntime(18915): Caused by: java.lang.NullPointerException
04-13 22:28:42.253: ERROR/AndroidRuntime(18915): at eu.greenrobot.kennzeichen.kennzeichen.fillData(kennzeichen.java:178)

You have a NullPointerException. Fix that, and with luck, your problems will go away.

CommonsWare
Is there a generic way to fix that?
Johe Green
Yes -- don't try calling methods on a variable or data member that is `null`. If you are new to Java, I really recommend spending some time getting comfortable with the language outside of Android. Android is just strange enough that trying to learn both Java and Android at once may be confusing.
CommonsWare
It was a problem with the lifecycle management. The variable existed in the internal app lifecycle but not when returning from another application.Your hint and the Android developer ressources helped me to solve the problem, so thanks to both of you ;)Helpful articles:http://developer.android.com/guide/tutorials/notepad/notepad-ex3.htmlhttp://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
Johe Green
A: 
String marketUri = "http://example.com/?q=app&title=test";
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "I found an awesome app");
intent.putExtra(Intent.EXTRA_TEXT, "Check this app out: " + marketUri);
Intent chooser = Intent.createChooser(intent, "Which app you want to share with");
startActivity(chooser);

Here is my code, when I run this, it open up the chooser but when I choose Facebook, it goes to browser and when I click post to profile; it just refresh the page. it doesn't post to my profile page. Why is it happen? Thank in advance.

Nyan