以下是我的活动代码。
问题是我无法正确使用AppCompat库并且它给我
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;
}
}
根据developer.android.com上的指南,我正在使用该库
以下是我的菜单文件
<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>
**编辑的LogCat输出**很抱歉早些时候我收到标签中给出的异常,但现在看到我收到此错误并且无法找到问题
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
* 查看清单是否有帮助* 以下是我的清单文件的代码
<?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>
答案 0 :(得分:0)
你需要像这样替换ShareActionProvider的演员
@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;
}
它应该正常工作。请测试它并告诉我它是否正常工作。