why MenuItemCompat.getActionProvider returns null?

前端 未结 12 1115
天命终不由人
天命终不由人 2020-11-29 06:33

I tried to use android.support.v7.widget.ShareActionProvider on actionbar in my app. So I followed the example from android document but got some issues.
Here\'s my m

12条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-29 07:23

    After some reading and that includes probably some of you responses, finally this issued is solved:

    1. Share_Menu.xml. Make sure you have a custom namespace and the actionProvider class is from that custom namespace as well as the correct value: android.support.v7.widget.ShareActionProvider

    2. Detail_Activity.java
      2.1. Inherit from ActionBarActivity instead of Activity
      2.2. Add the correct imports

      import android.support.v4.app.Fragment; import android.support.v4.view.MenuItemCompat; import android.support.v7.app.ActionBarActivity; import android.support.v7.widget.ShareActionProvider;

    3. AndroidManifest.xml Add the android:theme="@style/Theme.AppCompat.Light"

    4. Build.gradle
      4.1. In my case just to stay in the safe side I turn offf ProGuard in Debug.

      debug { runProguard false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' }

    4.2. Make sure that you have the following compile section in dependencies

    `compile 'com.android.support:appcompat-v7:20.0.+'` 
    

提交回复
热议问题