我正在开发android应用程序。在我的应用程序中,我添加了Viber共享功能。所以我在网上搜索了“如何在android中与viber共享内容”。然后我找到维伯做生意。所以对于share按钮,我遵循了这个链接,https://www.viber.com/en/viber-share-button。如你所见,这是如此简单。它只是要求用一个url重定向到浏览器。所以我打开了一个带有url的浏览器,在viber上共享如下。
private void shareOnViberClick()
{
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("viber://forward?text=textIWantToShare"));
startActivity(browserIntent);
}
但当我单击share按钮并调用该方法时,它会抛出以下错误
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: FATAL EXCEPTION: main
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=viber://forward?text=မန္ယူအသင္း၏ ပရိတ္သတ္ဟုထုတ္ေဖာ္ေျပာျပခဲ့သည့္ ေဒါ့မြန္လူငယ္ၾကယ္ပြင့္ }
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1632)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at android.app.Activity.startActivityForResult(Activity.java:3390)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at android.app.Activity.startActivityForResult(Activity.java:3351)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:842)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at android.app.Activity.startActivity(Activity.java:3587)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at android.app.Activity.startActivity(Activity.java:3555)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at tonightfootballreport.com.tfr.NewsDetailsActivity.shareOnViberClick(NewsDetailsActivity.java:364)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at tonightfootballreport.com.tfr.NewsDetailsActivity.access$200(NewsDetailsActivity.java:47)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at tonightfootballreport.com.tfr.NewsDetailsActivity$4.onClick(NewsDetailsActivity.java:134)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at android.view.View.performClick(View.java:4240)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:17721)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:730)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:92)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5103)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:525)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-07 05:33:02.357 11715-11715/tonightfootballreport.com.tfr E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
09-07 05:33:02.637 11715-11830/tonightfootballreport.com.tfr D/dalvikvm: GC_FOR_ALLOC freed 601K, 8% free 9230K/10032K, paused 56ms, total 60ms
09-07 05:33:05.029 11715-11833/tonightfootballreport.com.tfr D/dalvikvm: GC_FOR_ALLOC freed 258K, 6% free 10866K/11548K, paused 8ms, total 8ms
为什么会抛出这个错误?我所遵循的Viber Share API是否正确?
最佳答案
无法保证存在与您的需求匹配的intent-filter
活动,因此startActivity()
可能会失败,这应该在您的代码中处理。如果发生这种情况,它会抛出ActivityNotFoundException
,所以只需用startActivity()
包装您的try/catch
。
关于android - Viber共享按钮在Android中引发异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39366372/