我正在尝试在操作栏中放置一个分享按钮,每当我尝试启动该应用时它都会崩溃。
我的错误如下:
java.lang.UnsupportedOperationException: This is not supported, use MenuItemCompat.getActionProvider()
at android.support.v7.view.menu.MenuItemImpl.getActionProvider(MenuItemImpl.java:644)
at com.example.install.factapp.MainActivity.onCreateOptionsMenu(MainActivity.java:42)
at android.app.Activity.onCreatePanelMenu(Activity.java:2646)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:298)
at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:241)
at android.support.v7.app.AppCompatDelegateImplV7.preparePanel(AppCompatDelegateImplV7.java:1273)
at android.support.v7.app.AppCompatDelegateImplV7.doInvalidatePanelMenu(AppCompatDelegateImplV7.java:1553)
at android.support.v7.app.AppCompatDelegateImplV7.access$100(AppCompatDelegateImplV7.java:89)
at android.support.v7.app.AppCompatDelegateImplV7$1.run(AppCompatDelegateImplV7.java:129)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
这是我的main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/shareButton"
android:actionProviderClass="android.widget.ShareActionProvider"
android:title="@string/app_name"
android:showAsAction="always"/>
</menu>
以下是所有内容的主要活动,包括进口,以便我可以诊断出我的问题:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.ShareActionProvider;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView factBox;
TextView factNumberBox;
FactNumbers numbersHolder = new FactNumbers();
Facts factHolder = new Facts();
Backgrounds backs = new Backgrounds();
LinearLayout bg;
Animation scaleIn;
Animation slideOut;
private ShareActionProvider mShareActionProvider;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
factBox = (TextView) findViewById(R.id.factTextBox);
factNumberBox = (TextView) findViewById(R.id.numberBox);
bg = (LinearLayout) findViewById(R.id.background);
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.shareButton).getActionProvider();
mShareActionProvider.setShareIntent(getDefaultShareIntent());
return super.onCreateOptionsMenu(menu);
}
private Intent getDefaultShareIntent(){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "SUBJECT");
intent.putExtra(Intent.EXTRA_TEXT,"Extra Text");
return intent;
}
public void genFact(View view) {
factBox.setText(factHolder.nextFact());
factNumberBox.setText(numbersHolder.nextFactNumbers());
slideOut = AnimationUtils.loadAnimation(this, R.anim.slideout);
factBox.startAnimation(slideOut);
}
public void previousFact(View view) {
factBox.setText(factHolder.previousFact());
factNumberBox.setText(numbersHolder.previousFactNumbers());
scaleIn = AnimationUtils.loadAnimation(this, R.anim.slidein);
factBox.startAnimation(scaleIn);
}
public void randomButton(View view) {
factBox.setText(factHolder.randomButton());
factNumberBox.setText(factHolder.randomNumber());
}
}
答案 0 :(得分:1)
您正在使用AppCompatActivity
。这意味着你有各种各样的错误。
首先,您需要使用android.support.v7.widget.ShareActionProvider
,而不是android.widget.ShareActionProvider
。
其次,android:showAsAction
和android:actionProviderClass
需要切换为app:showAsAction
和app:actionProviderClass
,如the documentation for android.support.v7.widget.ShareActionProvider
所示。您还需要将xmlns:app="http://schemas.android.com/apk/res-auto"
添加到根<menu>
元素。
第三,在将导入更改为MenuItemCompat
后,使用ShareActionProvider
获取代码中的android.support.v7.widget.ShareActionProvider
。
This sample project演示了使用appcompat-v7
及其版本ShareActionProvider
。我的菜单资源是:
<item
android:id="@+id/share"
android:title="Share!"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
app:showAsAction="ifRoom"/>
,Java代码使用MenuItemCompat
:
/***
Copyright (c) 2012 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.
From _The Busy Coder's Guide to Android Development_
https://commonsware.com/Android
*/
package com.commonsware.android.sap;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.ShareActionProvider;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements
ShareActionProvider.OnShareTargetSelectedListener, TextWatcher {
private ShareActionProvider share=null;
private Intent shareIntent=new Intent(Intent.ACTION_SEND);
private EditText editor=null;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
shareIntent.setType("text/plain");
editor=(EditText)findViewById(R.id.editor);
editor.addTextChangedListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.actions, menu);
MenuItem item=menu.findItem(R.id.share);
share=(ShareActionProvider)MenuItemCompat.getActionProvider(item);
share.setOnShareTargetSelectedListener(this);
return(super.onCreateOptionsMenu(menu));
}
@Override
public boolean onShareTargetSelected(ShareActionProvider source,
Intent intent) {
Toast.makeText(this, intent.getComponent().toString(),
Toast.LENGTH_LONG).show();
return(false);
}
@Override
public void afterTextChanged(Editable s) {
shareIntent.putExtra(Intent.EXTRA_TEXT, s.toString());
share.setShareIntent(shareIntent);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// ignored
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// ignored
}
}
</menu>