2014-01-14 39 views
0

這是一個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)); 
} 
} 
+0

你把words.xml到下水庫的XML目錄? –

+0

是的,我做了創建一個名爲「XML」下的res文件夾/並取得2個稱爲「words.xml」和「searchable.xml」作爲教程 –

回答

1

嘗試使用XmlResourceParser代替XmlPullParser 在這一行:XmlPullParser xpp=getResources().getXml(R.xml.word);

+0

我還是得到了同樣的錯誤提供一步XML文件中的XML –

+0

強調即使我導入的android.content.res.XmlResourceParser; –

+0

你確定你命名爲file file.xml嗎?你想要打開word.xml而不是像你問的問題也許這裏的概率... – koni

相關問題