2015-07-03 37 views
0

我使用一個在線教程,可在http://www.raywenderlich.com/78576/android-tutorial-for-beginners-part-2無法施展android.support.v4.view.MenuItemCompat到android.widget.ShareActionProvider

已順利迄今發現目前正在學習Android的,但是我現在有一些問題,即使我的代碼與教程的代碼相匹配,我會得到上面的錯誤消息(此線程的標題)。

我的進口情況如下...

import android.app.Activity; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.support.v4.view.MenuItemCompat; 
import android.support.v7.app.ActionBarActivity; 
import android.widget.ShareActionProvider; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.EditText; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Button; 

我有以下問題,if語句中......

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    // Inflate the menu. 
    // Adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 

    // Access the Share Item defined in menu XML 
    MenuItem shareItem = menu.findItem(R.id.menu_item_share); 

    // Access the object responsible for 
    // putting together the sharing submenu 
    if (shareItem != null) { 
     mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem); 
    } 

    // Create an Intent to share your content 
    setShareIntent(); 

    return true; 
} 

private void setShareIntent() { 

    if (mShareActionProvider != null) { 

     // create an Intent with the contents of the TextView 
     Intent shareIntent = new Intent(Intent.ACTION_SEND); 
     shareIntent.setType("text/plain"); 
     shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Android Development"); 
     shareIntent.putExtra(Intent.EXTRA_TEXT, mainTextView.getText()); 

     // Make sure the provider knows 
     // it should work with that Intent 
     mShareActionProvider.setShareIntent(shareIntent); 
    } 
} 

我想這可能是由於最近的更新造成了一些代碼變得過時了,但我並沒有太多的想法,因爲我沒有經驗過android。

感謝您的幫助

回答

1

你需要導入從支持庫的ShareActionProvider,就像這樣:

import android.support.v7.widget.ShareActionProvider; 

因爲要支持較舊的Android版本,並使用支持庫實現MenuItemCompat你也如果可用,則需要使用與其交互的其他類的支持版本。使用自動導入時需要注意,如果有選擇,請選擇支持版本。

+0

謝謝,這整理出來了。好建議。我會在將來注意自動進口! –

相關問題