【问题标题】:Android: xml cannot be resolved or is not in the fieldAndroid:xml无法解析或不在字段中
【发布时间】:2014-01-14 20:54:18
【问题描述】:

这是一个 Android 项目。我正在尝试这个教程:http://www.edumobile.org/android/android-programming-tutorials/search-interface/

我完成了所有步骤,但在 R.xml.words 中有错误。带下划线的xml 有谁知道如何解决这个问题?

package com.app.SearchInterfaceDemo;

import android.os.Bundle;
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import com.app.SearchInterfaceDemo.R;

abstract public class SearchInterfaceBase extends ListActivity {
abstract ListAdapter makeMeAnAdapter(Intent intent);

private static final int LOCAL_SEARCH_ID = Menu.FIRST+1;
private static final int GLOBAL_SEARCH_ID = Menu.FIRST+2;
TextView selection;
ArrayList<String> items=new ArrayList<String>();

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    selection=(TextView)findViewById(R.id.selection);

    try {
        XmlPullParser xpp=getResources().getXml(R.xml.words);

        while (xpp.getEventType()!=XmlPullParser.END_DOCUMENT) {
            if (xpp.getEventType()==XmlPullParser.START_TAG) {
                if (xpp.getName().equals("word")) {
                    items.add(xpp.getAttributeValue(0));
                }
            }
                xpp.next();
        }
    }
    catch (Throwable t) {
        Toast
            .makeText(this, "Request failed: "+t.toString(), 4000)
            .show();
    }

    setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
    onNewIntent(getIntent());
}

@Override
public void onNewIntent(Intent intent) {
    ListAdapter adapter=makeMeAnAdapter(intent); 
    if (adapter==null) {
        finish();
    }
    else {
        setListAdapter(adapter);
    }
}

public void onListItemClick(ListView parent, View v, int position,long id) {
    selection.setText(parent.getAdapter().getItem(position).toString());
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(Menu.NONE, LOCAL_SEARCH_ID, Menu.NONE, "Local Search").setIcon(android.R.drawable.ic_search_category_default);
    menu.add(Menu.NONE, GLOBAL_SEARCH_ID, Menu.NONE, "Global Search").setIcon(R.drawable.search).setAlphabeticShortcut(SearchManager.MENU_KEY);

    return(super.onCreateOptionsMenu(menu));
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case LOCAL_SEARCH_ID:
            onSearchRequested(); 
            return(true);

        case GLOBAL_SEARCH_ID:
            startSearch(null, false, null, true); 
            return(true);
    }
    return(super.onOptionsItemSelected(item));
}
}

【问题讨论】:

  • 你是不是把words.xml放到res下的xml目录下?
  • 是的,我确实在 res/ 下创建了一个名为“xml”的文件夹,并按照教程中提供的步骤制作了 2 个名为“words.xml”和“searchable.xml”的 xml 文件

标签: android


【解决方案1】:

尝试使用XmlResourceParser 而不是XmlPullParser 在这一行:XmlPullParser xpp=getResources().getXml(R.xml.word);

【讨论】:

  • 即使我导入了 android.content.res.XmlResourceParser;
  • 您确定将文件命名为 word.xml 吗?你想打开 word.xml 而不是 words.xml 像你问的那样可能是一个问题......
  • 将其更改为 xml.words 但我仍然收到错误...下划线的是 xml 不是单词虽然:(
  • 尝试清理您的项目或重新启动您的 IDE,我试过这段代码,它工作正常
  • 还是一样。 R.xml.words 中带下划线的 xml 和一个新错误: in manifest
猜你喜欢
  • 2022-01-09
  • 2014-03-27
  • 2014-01-05
  • 2016-05-20
  • 2016-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多