3

Below is my Activity Code.

The problem is that i am not able to use AppCompat library properly and its giving me

ClasscastException android.support.v7.widget.ShareActionProvider to action.View.ActionProvider

   import android.content.Intent;
   import android.os.Bundle;
   import android.support.v4.view.MenuItemCompat;
   import android.support.v7.app.ActionBarActivity;
   import android.support.v7.widget.ShareActionProvider;
   import android.view.Menu;
   import android.view.MenuItem;

    public class MainActivity extends ActionBarActivity{
android.support.v7.widget.ShareActionProvider shareAction;

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


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    MenuItem item =(MenuItem) menu.findItem(R.id.menu_settings);
    shareAction = (ShareActionProvider) MenuItemCompat
            .getActionProvider(item);
    shareAction.setShareIntent(createShareIntent());
    return true;
}

public Intent createShareIntent() {
    Intent share = new Intent(Intent.ACTION_SEND);
    share.setAction(Intent.ACTION_SEND).putExtra(Intent.EXTRA_TEXT, "TEST")
            .setType("text/plain");
    return share;
}

}

According the Guide at developer.android.com i am using the library correctly

Below is my menu file

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

<item
    android:id="@+id/menu_settings"
    android:orderInCategory="100"
    easyshare:showAsAction="ifRoom"
    easyshare:actionProviderClass="android.support.v7.widget.ShareActionProvider"
    />
  </menu>

** Edited LogCat output ** Sorry earlier i was getting the exception as given in label but seeing now i am getting this error and somehow not able to find the problem

    10-02 02:17:46.566: E/AndroidRuntime(896): FATAL EXCEPTION: main
    10-02 02:17:46.566: E/AndroidRuntime(896): java.lang.NoClassDefFoundError: android.support.v7.appcompat.R$styleable
    10-02 02:17:46.566: E/AndroidRuntime(896):  at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:98)
    10-02 02:17:46.566: E/AndroidRuntime(896):  at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:58)
    10-02 02:17:46.566: E/AndroidRuntime(896):  at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
    10-02 02 :17:46.566: E/AndroidRuntime(896):     at net.mobitexter.easyshare.MainActivity.onCreate(MainActivity.java:16)
    10-02 02:17:46.566: E/AndroidRuntime(896):  at android.app.Activity.performCreate(Activity.java:4465)
    10-02 02:17:46.566: E/AndroidRuntime(896):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
    10-02 02:17:46.566: E/AndroidRuntime(896):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
    10-02 02:17:46.566: E/AndroidRuntime(896):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
    10-02 02:17:46.566: E/AndroidRuntime(896):  at android.app.ActivityThread.access$600(ActivityThread.java:127)
     10-02 02:17:46.566: E/AndroidRuntime(896):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
     10-02 02:17:46.566: E/AndroidRuntime(896):     at android.os.Handler.dispatchMessage(Handler.java:99)
     10-02 02:17:46.566: E/AndroidRuntime(896):     at android.os.Looper.loop(Looper.java:137)
     10-02 02:17:46.566: E/AndroidRuntime(896):     at android.app.ActivityThread.main(ActivityThread.java:4448)
     10-02 02:17:46.566: E/AndroidRuntime(896):     at java.lang.reflect.Method.invokeNative(Native Method)
     10-02 02:17:46.566: E/AndroidRuntime(896):     at java.lang.reflect.Method.invoke(Method.java:511)
     10-02 02:17:46.566: E/AndroidRuntime(896):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
     10-02 02:17:46.566: E/AndroidRuntime(896):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
     10-02 02:17:46.566: E/AndroidRuntime(896):     at dalvik.system.NativeStart.main(Native Method)
     10-02 02:18:02.152: I/Process(896): Sending signal. PID: 896 SIG: 9

*See if the manifest helps * The below is the code of my manifest file

 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.mobitexter.easyshare"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="9"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="net.mobitexter.easyshare.MainActivity"
        android:label="@string/app_name"
         >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>
Anuranjit Maindola
  • 1,507
  • 1
  • 14
  • 30
  • @CommonsWare: `android.widget.ShareActionProvider` isn't used, is it? I can only see `android.support.v7.widget.ShareActionProvider` here - so it's probably not a duplicate. – Trinimon Oct 01 '13 at 20:09
  • "android.widget.ShareActionProvider isn't used, is it?" -- since the OP claims that it is the error, it must be used. Unfortunately, the OP did not post a complete stack trace. – CommonsWare Oct 01 '13 at 20:14
  • @CommonsWare: I agree, it's a good guess - for sure. However, I can't see any evidence. Don't mind ... :) – Trinimon Oct 01 '13 at 20:23
  • @CommonsWare Could u help me regarding this. because i am getting nowhere with the problem – Anuranjit Maindola Oct 02 '13 at 06:09
  • 1
    hey, try here http://developer.android.com/reference/android/support/v7/widget/ShareActionProvider.html This is the standard sample from google in case of v7 actioncompat. – TeeTracker Dec 09 '13 at 20:59

1 Answers1

0

you need to replace the cast of ShareActionProvider like this

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
MenuItem item =(MenuItem) menu.findItem(R.id.menu_settings);
shareAction = (android.support.v7.widget.ShareActionProvider) item.getActionProvider();
shareAction.setShareIntent(createShareIntent());
return true;
}

It should be work correctly. Please Test it and tell me if it work correctly.

wSakly
  • 387
  • 1
  • 10