Programing

작업 공급자를 작업 공급자를 공유하도록 캐스팅 할 수 없습니다.

crosscheck 2020. 12. 6. 21:13
반응형

작업 공급자를 작업 공급자를 공유하도록 캐스팅 할 수 없습니다.


아래는 내 활동에 대한 코드입니다.

    import android.app.Activity;
    import android.os.Bundle;
    import android.support.v7.widget.ShareActionProvider;
    import android.view.Menu;
    import android.view.MenuItem;

    public class MainActivity extends Activity {
    private ShareActionProvider shareAction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        MenuItem item = menu.getItem(R.id.menu_settings);
        shareAction = (ShareActionProvider) item.getActionProvider();
        return true;
    }
}

문제는 ActionProvider에서 ShareActionProvider로 캐스트 오류가 있다는 것입니다. 그 이유는 아래와 같습니다. activity_menu.xml

 <menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:id="@+id/menu_settings"
    android:orderInCategory="100"
    android:showAsAction="always"
    android:title="@string/menu_settings"
    android:actionProviderClass="android.widget.ShareActionProvider"
    />

</menu>

나는 같은 문제가 있었고 해결책을 찾았습니다.

1) 다음을 사용해야합니다.

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:bwq="http://schemas.android.com/apk/res-auto" >

    <item
        android:id="@+id/menu_share"
        android:title="@string/menu_share"
        bwq:actionProviderClass="android.support.v7.widget.ShareActionProvider"
        bwq:showAsAction="always"/>
</menu>

2) 및 Java

import android.support.v7.widget.ShareActionProvider;

// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.menu_share);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);

메뉴:

<item
    android:id="@+id/action_share"
    android:title="@string/action_share"
    app:showAsAction="ifRoom"
    app:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>

자바:

MenuItem menuItem = menu.findItem(R.id.action_share);
mActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);

당신은 사용하는 android.widget.ShareActionProvider네이티브 API 레벨 11 + 작업 표시 줄에 대한 인. 작업 표시 줄의 AppCompat 백 포트를 사용 android.support.v7.widget.ShareActionProvider하는 경우 대신 사용해야 합니다.


나는 기본적으로 당신이하고 있는 안드로이드 개발 액션 바 가이드에 따라이 문제에 직면했습니다 . 이전 버전과 호환되는 v7 및 v4 지원 라이브러리를 사용하여 작업 표시 줄을 활용하는 샘플을 살펴본 후 onCreateOptionsMenu ()에 대해 다음 코드를 사용하게되었습니다.

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        File file = new File(mFilePath);


        ShareCompat.IntentBuilder b = ShareCompat.IntentBuilder.from(this)
        .setType("image/png")
        .setStream(Uri.fromFile(file));


        MenuItem item = menu.add("Share");
        ShareCompat.configureMenuItem(item, b);
        MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
        return true;
    }

Couple of things to note here is that you are not inflating from a menu resource. The menu is having the default share button added to it. You simply need to specify what type of resource your are sharing with .setType. Since I am sharing a file I need to setStream, with Uri.fromFile(new File()); If you were sharing text you would setType("text/plain").

Also make sure you have imported the $SDK\extras\android\support\v7\appcompat library project, which contains the needed packages. Also since have imported that library project, your project does not need the v4support.jar in your libs folder because the library project already has it.


The problem was that what @CommonsWare said about not using the support library ShareActionProvider and also even if i did then it also wont have worked out because when using Support Library we require Custom Prefixes for some actions like showAsAction


Follow a Simple rule that I found useful

With AppCompatActivity use this,

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:support="http://schemas.android.com/apk/res-auto">

    <!--
      To use ShareActionProvider, we reference the class by set the
      support:actionProviderClass attribute with the full class name of ShareActionProvider.
    -->
    <item
        android:id="@+id/menu_share"
        android:title="@string/menu_share"
        support:actionProviderClass="android.support.v7.widget.ShareActionProvider"
        support:showAsAction="always" />

</menu>

You can also replace support:actionProviderClass with app:actionProviderClass and support:showAsAction with app:showAsAction

In your onCreateOptionsMenu()

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu resource
        getMenuInflater().inflate(R.menu.main_menu, menu);

        // Retrieve the share menu item
        MenuItem shareItem = menu.findItem(R.id.menu_share);

        // Now get the ShareActionProvider from the item
        mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);

         //set its ShareIntent.
        setShareIntent(shareIntent);

        return super.onCreateOptionsMenu(menu);
    }

With Activity use this,

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/share"
        android:actionProviderClass="android.widget.ShareActionProvider"
        android:showAsAction="ifRoom"
        tools:ignore="MenuTitle" />

</menu>

In your onCreateOptionsMenu()

@Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.actions, menu);

    ShareActionProvider share=
        (ShareActionProvider)menu.findItem(R.id.share)
                                 .getActionProvider();
    share.setShareIntent(shareIntent);

    return(super.onCreateOptionsMenu(menu));
  }

None of the solutions here solved my problem with ShareActionProvider, not casting / returning null. I ended up replacing ShareActionProvider with just an Intent.SEND_ACTION to share images in my app, like presented in Android Developers tutorial: https://developer.android.com/training/sharing/send

Although Google mentions in this tutorial that: Note: The best way to add a share action item to an ActionBar is to use ShareActionProvider, which became available in API level 14. ShareActionProvider is discussed in the lesson about Adding an Easy Share Action. I found much simpler to implement just the Intention.SEND_ACTION. Not sure if there are other reasons to implement the ShareActionProvider...


This was asked years ago, so the existing answers probably worked back then. However, at the time of this writing the suggested code gives many deprecation warnings and it doesn't solve the problem.

I did eventually solve the problem and it was not documented anywhere on the web (that I could find), so hopefully this answer will help people who currently run into this same problem.

The solution for me was in the import statement. When I used SharedActionProvider for the first time, Android Studio can add the import automatically. It provides two options for what to import: android.widget.ShareActionProvider and androidx.appcompat.widget.ShareActionProvider.

The former is broken and results in the error about the cast never succeeding. The latter will make everything work properly. The app:ActionProviderClass in the menu file must be identical to the imported file name.

참고URL : https://stackoverflow.com/questions/19118051/unable-to-cast-action-provider-to-share-action-provider

반응형