【问题标题】:Android application chrashes after "share" intentAndroid 应用程序在“共享”意图后崩溃
【发布时间】:2011-02-07 15:08:15
【问题描述】:

我的应用提供“分享/告诉朋友”功能。当按下“分享”按钮时,会调用以下方法来打开应用列表,这些应用可以执行操作(例如 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)));      
}

分享功能基本正常。但是当共享应用程序(Facebook、Twitter、...)试图返回我的应用程序时,会引发强制关闭。

我猜我的应用在共享过程中在后台关闭。至少调试器是这么说的。

有什么想法吗?


问题似乎是应用程序在后台关闭。此外,当我从我的应用程序打开浏览器时,尝试使用返回键返回我的应用程序。强制关闭。这是 Log Cat 的输出

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

【问题讨论】:

  • 发布相关的调试输出
  • ...其中 jqpublic 表示与“强制关闭”对话框关联的 Java 堆栈跟踪。您可以通过adb logcat、DDMS 或 Eclipse 中的 DDMS 透视图获取此堆栈跟踪。

标签: android facebook twitter android-intent share


【解决方案1】:
04-13 22:28:42.253: ERROR/AndroidRuntime(18915): Caused by: java.lang.NullPointerException
04-13 22:28:42.253: ERROR/AndroidRuntime(18915): at xx.xxx.xxx.xxx.fillData(xxx.java:178)

您有一个NullPointerException。解决这个问题,运气好的话,你的问题就会消失。

【讨论】:

  • 有没有通用的方法来解决这个问题?
  • 是的——不要尝试对null 的变量或数据成员调用方法。如果您是 Java 新手,我真的建议您花一些时间熟悉 Android 之外的语言。 Android 太奇怪了,试图同时学习 Java 和 Android 可能会令人困惑。
  • 生命周期管理有问题。该变量存在于内部应用程序生命周期中,但从另一个应用程序返回时不存在。您的提示和 Android 开发人员资源帮助我解决了这个问题,非常感谢你们;) 有用的文章:developer.android.com/guide/tutorials/notepad/notepad-ex3.htmldeveloper.android.com/reference/android/app/…
【解决方案2】:
public void share() {
   Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
   shareIntent.setType("text/plain");
   shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, new String(getText(R.string.menu_share_subject)));
   shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, new String(getText(R.string.menu_share_body)));

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

做的工作:) 不知道为什么它在其他任何地方都可以工作,但是有了这个添加,它可以按预期工作:) 也感谢您的回复

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-12
    • 2011-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    相关资源
    最近更新 更多