我正在尝试在DetailFragment类中使用ShareActionProvider。
public static class DetailFragment extends Fragment {
private static final String LOG_TAG = DetailFragment.class.getSimpleName();
private static final String FORECAST_SHARE_HASHTAG = " # SunshineApp";
private String mForecastStr;
public DetailFragment() {
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_detail, container, false);
Intent intent = getActivity().getIntent();
if (intent != null && intent.hasExtra(Intent.EXTRA_TEXT)) {
mForecastStr = intent.getStringExtra(Intent.EXTRA_TEXT);
TextView info = (TextView)rootView.findViewById(R.id.info);
Log.d(LOG_TAG, "mForecastStr is : " + mForecastStr);
info.setText(mForecastStr);
}
return rootView;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.detailfragment, menu);
MenuItem menuItem = menu.findItem(R.id.action_share);
ShareActionProvider mShareActionProvider = (ShareActionProvider)MenuItemCompat.getActionProvider(menuItem);
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(createShareForecastIntent());
} else {
Log.d(LOG_TAG, "Share Action Provider is null?");
}
}
private Intent createShareForecastIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, mForecastStr + FORECAST_SHARE_HASHTAG);
return shareIntent;
}
}
--EOF--
但我不仅可以获得null类而不是mShareActionProvider。错误消息在这里。
07-19 18:07:36.348: W/SupportMenuInflater(31261): Cannot instantiate class: app.support.v7.widget.ShareActionProvider
07-19 18:07:36.348: W/SupportMenuInflater(31261): java.lang.ClassNotFoundException: Didn't find class "app.support.v7.widget.ShareActionProvider" on path: DexPathList[[zip file "/data/app/com.example.project-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.project-1, /vendor/lib, /system/lib]]
似乎Android Studio无法找到app.support.v7.widget.ShareActionProvider。我已经安装了Android支持库。我该怎么办?
更新!!
这是我的build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.shinjaehun.sunshine"
minSdkVersion 10
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:support-v4:22.2.1'
compile 'com.android.support:appcompat-v7:22.2.1'
}
--EOF---
我正在使用SDK构建工具版本22.0.1,但我的appcompat库版本是22.2.1。这就是我失败的原因,不是吗?我如何使用appcompat版本22.0.1?只有Android支持库Ver 22.2.1。