這是一個Android項目。我一直想把這個教程:http://www.edumobile.org/android/android-programming-tutorials/search-interface/Android:xml無法解析或不在現場
我做了所有的步驟,但我有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到下水庫的XML目錄? –
是的,我做了創建一個名爲「XML」下的res文件夾/並取得2個稱爲「words.xml」和「searchable.xml」作爲教程 –