2016-06-09 105 views
0

我想在我的应用上添加共享按钮。如何在我的Android应用上添加共享按钮

如果用户使用该应用程序,将会有一个选项,他们可以共享我的应用程序,但不幸的是没有发生。

这里是我的代码:

MainActivity.java

package com.vvhvb.hesselfeenstra.vvheerenveenseboys; 

import android.content.Intent; 
import android.os.Bundle; 
import android.support.v4.view.MenuItemCompat; 
import android.support.v7.app.AppCompatActivity; 
import android.widget.ShareActionProvider; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.webkit.WebView; 

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     String url ="http://dehvb.nl/"; 
     WebView view=(WebView) this.findViewById(R.id.webView); 
     view.getSettings().setJavaScriptEnabled(true); 
     view.getSettings().setBuiltInZoomControls(true); 
     view.getSettings().setDisplayZoomControls(false); 
     view.loadUrl(url); 

    } 

    private ShareActionProvider mShareActionProvider; 
    @Override 

    public boolean onCreateOptionsMenu(Menu menu) { 

     getMenuInflater().inflate(R.menu.menu_main, menu); 
     MenuItem item = menu.findItem(R.id.menu_item_share); 
     mShareActionProvider = (ShareActionProvider)MenuItemCompat.getActionProvider(item); 
     Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); 
     sharingIntent.setType("text/plain"); 
     String shareBody = "here goes your share content body"; 
     sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Share Subject"); 
     sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody); 

     //then set the sharingIntent 
     mShareActionProvider.setShareIntent(sharingIntent); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.menu_item_share) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

menu_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context="com.vvhvb.hesselfeenstra.vvheerenveenseboys.MainActivity"> 

    <item 
     android:id="@+id/menu_item_share" 
     app:showAsAction="ifRoom" 
     android:title="Share" 
     android:icon="@drawable/share" 
     android:actionProviderClass="android.support.v7.widget.ShareActionProvider" /> 

</menu> 

的logcat:

06-10 14:37:09.841 24733-24733/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/Timeline: Timeline: Activity_idle id: [email protected] time:33915488 
06-10 14:37:15.501 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/cr_Ime: ImeThread is not enabled. 
06-10 14:37:15.511 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/Activity: performCreate Call Injection manager 
06-10 14:37:15.511 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/InjectionManager: dispatchOnViewCreated > Target : com.vvhvb.hesselfeenstra.vvheerenveenseboys.MainActivity isFragment :false 
06-10 14:37:15.521 28065-28195/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true 
06-10 14:37:15.531 28065-28196/com.vvhvb.hesselfeenstra.vvheerenveenseboys E/libEGL: validate_display:255 error 3008 (EGL_BAD_DISPLAY) 
06-10 14:37:15.531 28065-28196/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/libEGL: eglInitialize EGLDisplay = 0xdccbf864 
06-10 14:37:15.541 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/PhoneWindow: *FMB* isFloatingMenuEnabled mFloatingMenuBtn : null 
06-10 14:37:15.541 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/PhoneWindow: *FMB* isFloatingMenuEnabled return false 
06-10 14:37:15.591 28065-28195/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/libEGL: eglInitialize EGLDisplay = 0xdd13fc54 
06-10 14:37:15.591 28065-28195/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/OpenGLRenderer: Initialized EGL, version 1.4 
06-10 14:37:15.591 28065-28195/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/OpenGLRenderer: HWUI protection enabled for context , &this =0xeedbea00 ,&mEglDisplay = 1 , &mEglConfig = -189468004 
06-10 14:37:15.591 28065-28195/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/OpenGLRenderer: Get maximum texture size. GL_MAX_TEXTURE_SIZE is 8192 
06-10 14:37:15.591 28065-28195/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/OpenGLRenderer: Enabling debug mode 0 
06-10 14:37:15.591 28065-28195/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/mali_winsys: new_window_surface returns 0x3000, [1080x1920]-format:1 
06-10 14:37:15.791 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/InjectionManager: dispatchCreateOptionsMenu :com.vvhvb.hesselfeenstra.vvheerenveenseboys.MainActivity 
06-10 14:37:15.791 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/InjectionManager: dispatchPrepareOptionsMenu :com.vvhvb.hesselfeenstra.vvheerenveenseboys.MainActivity 
06-10 14:37:15.901 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/Timeline: Timeline: Activity_idle id: [email protected] time:33921540 
06-10 14:37:16.601 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 28065 
06-10 14:37:16.601 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/cr_Ime: [InputMethodManagerWrapper.java:59] isActive: true 
06-10 14:37:16.601 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/cr_Ime: [InputMethodManagerWrapper.java:68] hideSoftInputFromWindow 
06-10 14:37:21.811 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN 
06-10 14:37:21.881 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/InjectionManager: dispatchOptionsItemSelected :com.vvhvb.hesselfeenstra.vvheerenveenseboys.MainActivity 
06-10 14:37:22.261 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN 
06-10 14:37:22.361 28065-28065/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/InjectionManager: dispatchOptionsItemSelected :com.vvhvb.hesselfeenstra.vvheerenveenseboys.MainActivity 
06-10 14:40:49.801 31316-31316/com.vvhvb.hesselfeenstra.vvheerenveenseboys D/AndroidRuntime: Shutting down VM 
06-10 14:40:49.801 31316-31316/com.vvhvb.hesselfeenstra.vvheerenveenseboys E/AndroidRuntime: FATAL EXCEPTION: main 
                          Process: com.vvhvb.hesselfeenstra.vvheerenveenseboys, PID: 31316 
                          java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.ShareActionProvider.setShareIntent(android.content.Intent)' on a null object reference 
                           at com.vvhvb.hesselfeenstra.vvheerenveenseboys.MainActivity.onCreateOptionsMenu(MainActivity.java:48) 
                           at android.app.Activity.onCreatePanelMenu(Activity.java:3124) 
                           at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:340) 
                           at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85) 
                           at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:258) 
                           at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85) 
                           at android.support.v7.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:454) 
                           at android.support.v7.app.ToolbarActionBar$1.run(ToolbarActionBar.java:61) 
                           at android.os.Handler.handleCallback(Handler.java:739) 
                           at android.os.Handler.dispatchMessage(Handler.java:95) 
                           at android.os.Looper.loop(Looper.java:145) 
                           at android.app.ActivityThread.main(ActivityThread.java:6917) 
                           at java.lang.reflect.Method.invoke(Native Method) 
                           at java.lang.reflect.Method.invoke(Method.java:372) 
                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 
06-10 14:40:54.201 31316-31316/com.vvhvb.hesselfeenstra.vvheerenveenseboys I/Process: Sending signal. PID: 31316 SIG: 9 
+0

什么是 '分享应用程式的' 是什么意思? – mjn

+0

我希望我的应用程序用户可以通过社交媒体将应用程序分享给其他人。 – Hessel

+0

请参阅https://developer.android.com/training/sharing/shareaction.html – mjn

回答

3

首先,修改menu.xml文件的文件包括分享行动菜单项:

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context="com.vvhvb.hesselfeenstra.vvheerenveenseboys.MainActivity"> 
    <item 
     android:id="@+id/action_settings" 
     android:orderInCategory="100" 
     android:title="@string/action_share" 
     android:icon="@drawable/share" 
     app:showAsAction="ifRoom" /> 
     <-- here is the share action menu item --> 
     <item 
      android:id="@+id/menu_item_share" 
      app:showAsAction="ifRoom" 
      android:title="Share" 
      app:actionProviderClass= 
      "android.support.v7.widget.ShareActionProvider" /> 
</menu> 

然后改变onCreateOptionsMenu通过增加基于股权分置操作菜单项ShareActionProvider代码的:

//add the following imports (you may already have them) 
    import android.support.v4.view.MenuItemCompat; 
    import android.support.v7.app.AppCompatActivity; 
    import android.support.v7.widget.ShareActionProvider; 
    ... 
    private ShareActionProvider mShareActionProvider; 
    ... 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

     getMenuInflater().inflate(R.menu.menu_main, menu); 
     MenuItem item = menu.findItem(R.id.menu_item_share); 
     mShareActionProvider = (ShareActionProvider)MenuItemCompat.getActionProvider(item); 
     //create the sharing intent 
     Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); 
     sharingIntent.setType("text/plain"); 
     String shareBody = "here goes your share content body"; 
     sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Share Subject"); 
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody); 

    //then set the sharingIntent 
    mShareActionProvider.setShareIntent(sharingIntent); 
      return true; 
    } 

我希望这可以帮助你。请尝试一下,让我知道。您也可以参考这些密切相关链接:

Adding an Easy Share Action

Activating Share Button in Android

Basic sharing example

Example of using appcompat-v7 ShareActionProvider

+0

的可能重复我得到错误由(ShareActionProvider)item.getActionProvider();我怎样才能删除这个错误? – Hessel

+0

什么是错误? – ishmaelMakitla

+0

它由行给出:mShareActionProvider =(ShareActionProvider)item.getActionProvider();下一个错误:不可转换的类型;无法将'android.view.actionprovider'强制转换为'android.support.v7.widget.ShareActionProvider' – Hessel

相关问题