Android 无法解决菜单项

Posted

技术标签:

【中文标题】Android 无法解决菜单项【英文标题】:Android Cannot Resolve Menu Item 【发布时间】:2011-12-14 11:34:35 【问题描述】:

我正在开发适用于 android 的 RSS 阅读器,但在添加菜单项时遇到了问题。我想创建一个项目来手动刷新提要,但我收到一个错误“Menu.Item 无法解析为一种类型”。错误在这里:

public boolean onOptionsItemSelected(Menu.Item item)
    switch (item.getId()) 
    case 0:

        Log.i(tag,"Set RSS Feed");
        return true;
    case 1:
        Log.i(tag,"Refreshing RSS Feed");
        return true;
    
    return false;

完整课程在这里:

package com.CalvaryChapelMelbourne.feedparser;


import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.widget.TextView;
import android.widget.ListView;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ArrayAdapter;
import android.widget.AdapterView.OnItemClickListener; 
import android.util.Log;
import java.util.ArrayList;
import java.net.URL;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;

import org.xml.sax.XMLReader;

import android.content.Intent;

import com.CalvaryChapelMelbourne.feedparser.ShowDescription;

public class Rs-s-reader extends Activity implements OnItemClickListener


public final String RSSFEEDOFCHOICE = "http://app.calvaryccm.com/mobile/android/v1/devos";

public final String tag = "Rs-s-reader";
private RSSFeed feed = null;

/** Called when the activity is first created. */

public void onCreate(Bundle icicle) 
    super.onCreate(icicle);
    setContentView(R.layout.main);

    // go get our feed!
    feed = getFeed(RSSFEEDOFCHOICE);

    // display UI
    UpdateDisplay();




private RSSFeed getFeed(String urlToRssFeed)

    try
    
        // setup the url
       URL url = new URL(urlToRssFeed);

       // create the factory
       SAXParserFactory factory = SAXParserFactory.newInstance();
       // create a parser
       SAXParser parser = factory.newSAXParser();

       // create the reader (scanner)
       XMLReader xmlreader = parser.getXMLReader();
       // instantiate our handler
       RSSHandler theRssHandler = new RSSHandler();
       // assign our handler
       xmlreader.setContentHandler(theRssHandler);
       // get our data via the url class
       InputSource is = new InputSource(url.openStream());
       // perform the synchronous parse           
       xmlreader.parse(is);
       // get the results - should be a fully populated RSSFeed instance, or null on error
       return theRssHandler.getFeed();
    
    catch (Exception ee)
    
        // if we have a problem, simply return null
        return null;
    

public boolean onCreateOptionsMenu(Menu menu) 

    super.onCreateOptionsMenu(menu);

    menu.add(0,0,"Choose RSS Feed");
    menu.add(0,1,"Refresh");
    Log.i(tag,"onCreateOptionsMenu");
    return true;


public boolean onOptionsItemSelected(Menu.Item item)
    switch (item.getId()) 
    case 0:

        Log.i(tag,"Set RSS Feed");
        return true;
    case 1:
        Log.i(tag,"Refreshing RSS Feed");
        return true;
    
    return false;



private void UpdateDisplay()

    TextView feedtitle = (TextView) findViewById(R.id.feedtitle);
    TextView feedpubdate = (TextView) findViewById(R.id.feedpubdate);
    ListView itemlist = (ListView) findViewById(R.id.itemlist);


    if (feed == null)
    
        feedtitle.setText("No RSS Feed Available");
        return;
    

    feedtitle.setText(feed.getTitle());
    feedpubdate.setText(feed.getPubDate());

    ArrayAdapter<RSSItem> adapter = new ArrayAdapter<RSSItem>(this,android.R.layout.simple_list_item_1,feed.getAllItems());

    itemlist.setAdapter(adapter);

    itemlist.setOnItemClickListener(this);

    itemlist.setSelection(0);




 public void onItemClick(AdapterView parent, View v, int position, long id)
 
     Log.i(tag,"item clicked! [" + feed.getItem(position).getTitle() + "]");

     Intent itemintent = new Intent(this,ShowDescription.class);

     Bundle b = new Bundle();
     b.putString("title", feed.getItem(position).getTitle());
     b.putString("description", feed.getItem(position).getDescription());
     b.putString("link", feed.getItem(position).getLink());
     b.putString("pubdate", feed.getItem(position).getPubDate());

     itemintent.putExtra("android.intent.extra.INTENT", b);

     startSubActivity(itemintent,0);
 


感谢您的帮助!

【问题讨论】:

【参考方案1】:

应该是MenuItem,而不是Menu.Item

public boolean onOptionsItemSelected(MenuItem item)

【讨论】:

【参考方案2】:

您需要将Menu.Item 更改为MenuItem

【讨论】:

【参考方案3】:

onOptionsItemSelected 中的第一个参数应该是 MenuItem,而不是 Menu.Item。只需删除该点,您就可以开始使用了。

【讨论】:

以上是关于Android 无法解决菜单项的主要内容,如果未能解决你的问题,请参考以下文章

带有 actionLayout 的 Android 操作栏菜单项无法正常工作

Android菜单项背景

如何在 Android 中更改菜单项的文本颜色?

Android:更改菜单项中的图标颜色

InflateException:无法解析菜单项 onClick 处理程序

在我的WordPress菜单项中添加#无法正常工作