gpt4 book ai didi

android - MenuItemCompat 无法解析

转载 作者:行者123 更新时间:2023-11-29 20:16:42 24 4
gpt4 key购买 nike

   package com.example.dylan.sunshine;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ShareActionProvider;
import android.widget.TextView;
import android.support.v4.view.MenuItemCompat;

public class DetailActivity extends Activity {




@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
if (savedInstanceState == null ) {
getFragmentManager().beginTransaction().add(R.id.container, new DetailFragment()).commit();
}

}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
startActivity(new Intent(this, SettingsActivity.class));
return true;
}

return super.onOptionsItemSelected(item);
}


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) rootView.findViewById(R.id.detail_text))
.setText(mForecastStr);


}
return rootView;
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the menu; this adds items to the action bar if it is present.
inflater.inflate(R.menu.detailfragment, menu);

// Retrieve the share menu item
MenuItem menuItem = menu.findItem(R.id.action_share);

// Get the provider and hold onto it to set/change the share intent.
ShareActionProvider mShareActionProvider =
(ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);

// Attach an intent to this ShareActionProvider. You can update this at any time,
// like when the user selects a new piece of data they might like to share.
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;
}
}

}

这是我的 Detailactivity,我在 MenuitemCompat 上收到一个错误,它说它无法解析符号 'MenuitemCompat'

这是我的 detailfragment.xml

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

<item
android:id="@+id/action_share"
android:title="@string/action_share"
android:showAsAction="always"
/>

</menu>

谁能帮我解释一下我做错了什么是错的

已解决:我在这篇文章中使用了第二个答案 Want to use ViewPager, cannot get android.support.* to be recognized?

添加了 v4、v7 和 v13 库,并对我的导入进行了一些小的调整,一切正常

最佳答案

如果您尚未将此导入添加到您的 Detailactivity,您可能会收到该错误。

import android.support.v4.view.MenuItemCompat;

关于android - MenuItemCompat 无法解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33725522/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com