我正在学习android,我正在尝试在操作菜单中添加共享项。我为此引用http://developer.android.com/training/sharing/shareaction.html。但是item.getActionProvider()在语句中返回null mShareActionProvider =(ShareActionProvider)item.getActionProvider(); 。 我试图在通过EditText给出名称的外部目录中播放视频文件。 如果我直接传递意图,则播放视频文件。所以我确信,Intent没有问题 这是完整的代码:
package com.example.sampleactivities;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.view.MenuItemCompat;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ShareActionProvider;
import android.widget.Toast;
import android.os.Build;
public class UriTesting extends ActionBarActivity {
public ShareActionProvider actionprovider;
private Menu opmenu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_uri_testing);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
((Button)findViewById(R.id.SubmitForActionButton)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
UriTesting.this.doshare();}});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
this.opmenu=menu;
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.uri_testing, menu);
MenuItem item=menu.findItem(R.id.menu_item_share);
actionprovider=(ShareActionProvider)item.getActionProvider();
if (actionprovider == null) {
Toast.makeText(getApplicationContext(),"action provider is null",Toast.LENGTH_SHORT).show();}
return super.onCreateOptionsMenu(menu);
}
private void doshare() {
if(actionprovider==null)
{
MenuItem item=opmenu.findItem(R.id.menu_item_share);
actionprovider=(ShareActionProvider)item.getActionProvider();
if (actionprovider == null) {
Toast.makeText(getApplicationContext(),"action provider is null ",Toast.LENGTH_SHORT).show();}
}
EditText url_edittext=(EditText)findViewById(R.id.urlreadingEditText);
String filename=url_edittext.getText().toString();
try {
String encodedfilename = URLEncoder.encode(filename,"UTF-8");
String PATH = Environment.getExternalStorageDirectory()+"/Videos/"+encodedfilename.trim().toString();
Uri url=Uri.parse("file://"+PATH);
Intent intent=new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setType("video/");
intent.setData(url);
//intent.setDataAndType(Uri.parse(url.toString()),"audio/");
// Intent chooser=Intent.createChooser(intent, "select any");
// startActivity(intent);
Toast.makeText(getApplicationContext(),"sett",Toast.LENGTH_SHORT).show();
if (actionprovider != null) {
Toast.makeText(getApplicationContext(),"sett not null",Toast.LENGTH_SHORT).show();
actionprovider.setShareIntent(intent);}
}
catch(Exception e)
{
Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_SHORT).show();
}
}
@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();
if (id == R.id.action_settings) {
return true;
}
if(id==R.id.menu_item_share)
doshare();
return super.onOptionsItemSelected(item);
}
这是菜单的
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.sampleactivities.UriTesting" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never"/>
<item
android:id="@+id/menu_item_share"
android:title="share"
app:actionProviderClass="android.widget.ShareActionProvider"
app:showAsAction="ifRoom"/>
</menu>
答案 0 :(得分:0)
如果您使用ActionBarActivity
,则需要使用android.support.v7.widget.ShareActionProvider,而不是您正在使用的android.widget.ShareActionProvider
。此外,您必须使用MenuItemCompat.getActionProvider()来检索ActionProvider
,而不是直接致电getActionProvider()
。