2010-11-20 143 views
1

我得到一個解析錯誤,我不知道爲什麼(我是編程新手)。由於這個錯誤,R.java消失了,我最終有更多的錯誤。希望有人能幫助我。這裏就是錯誤來自:list.xml解析XML時出錯:未找到元素。它不會生成R.java,因爲它

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    > 
    <ListView android:id="@+id/ListView01" android:layout_height="wrap_content" 
    android:layout_width="fill_parent"> 
    <TextView android:text="@+id/TextView01" android:id="@+id/TextView01" 
    android:layout_width="wrap_content" android:layout_height="wrap_content" 
    android:layout_marginLeft="10px" android:textColor="#0099CC"></TextView> 

</ListView> 

</LinearLayout> 

,這裏是其中的XML文件名爲:list.java

package com.aha; 

import java.util.HashMap; 

import android.app.ListActivity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.SimpleAdapter; 
import android.widget.SimpleAdapter.ViewBinder; 
import android.widget.Adapter; 
import android.widget.BaseAdapter; 

public class list extends ListActivity { 

    private HashMap<String, Class<?>> yourMap; 
    @Override 
    public void onCreate(Bundle b) { 
    yourMap = new HashMap<String, Class<?>>(); 
    yourMap.put("Matches", matches.class); 
    yourMap.put("Economy", finances.class); 
    } 

    public void onCreate1(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    String[] menu = getResources().getStringArray(R.array.menu_array); 
    setListAdapter(new ArrayAdapter<String>(this, R.layout.list, menu)); 

    ListView lv = getListView(); 
    lv.setTextFilterEnabled(true); 
    lv.setOnItemClickListener(new OnItemClickListener() { 
     public <View> void onItemClick(AdapterView<?> parent, View view, 
      int position, long id) { 
     // When clicked, show a toast with the TextView text 
      Object o = this.getListAdapter().getItem(position); 
      String keyword = o.toString(); 
      Intent intent = new Intent(this, yourMap.get(keyword)); 
      startActivity(intent); 

     } 
    }); 
    } 
} 

也是在這行我得到一個錯誤說我必須實現一個抽象的方法:

lv.setOnItemClickListener(new OnItemClickListener() { 

我只是想知道這是什麼意思,不一定是一個解決方案。希望我能自己找出一個。解析錯誤我已經有一兩天了,無法單獨解決。

感謝所有幫助

回答

0

什麼是您所使用的編輯器?

從我的經驗,解析在Eclipse問題

被下一個

  1. 您的視圖名稱之一是造成駱駝情況*不要使用使用NO CAPSLOCK *
  2. R檔是不是「刷新」,因此新的元素不會出現

嘗試

  1. 用小寫字母編寫* .xml(在視圖文件夾中)。
  2. 乾淨所有的項目,然後建立
  3. 也許你沒有申報的項目適當notice for this table for the SDK version

請告知我要實際ERR(寫在控制檯)爲進一步幫助。

+0

我使用日食。所以我的listview的id不應該是ListView01,而是listview01? – Robby 2010-11-21 15:45:19

+0

錯誤來自問題控制檯:解析XML時出錯:格式錯誤(無效令牌)\t main.xml Android AAPT問題 – Robby 2010-11-21 16:11:20

1

ListView應該沒有子元素,請改用適配器。

+0

我使用了一個適配器,但是我收到了錯誤信息。這裏是錯誤: 類型new AdapterView.OnItemClickListener(){}必須實現繼承的抽象方法AdapterView.OnItemClickListener.onItemClick(AdapterView ,View,int,long)。我不知道這是什麼意思 – Robby 2010-11-21 16:10:14

+0

添加一個方法到'onItemClick'(AdapterView ,View,int,long)' – Pentium10 2010-11-21 17:12:11

+0

我需要將任何東西放入方法中,還是可以放在方法中代碼但實際上沒有做任何事情? – Robby 2010-11-21 17:38:11

0

我有一個類似的問題,事實證明,因爲Eclipse添加了「import android.R;」 (如果您傾向於使用Ctrl-Shift-O導入文件,這可能會發生)隱藏在R.java中的.java文件隱藏聲明中。 刪除此導入行後,再次按預期查看R.java。

0

採取以下步驟:

  1. 保存並關閉.XML
  2. 刪除.out.xml和/或在菜單.out.out.xml
  3. 單擊項目並選擇清洗.. 。
  4. 爲「下面選擇清潔工程」爲您的項目
  5. 點擊複選框,選擇單選按鈕,然後單擊確定
  6. 運行程序。
0

你的問題似乎是在佈局文件本身,

已添加TextView在佈局文件中ListView標籤內。只需將ListView移出TextView。

您的佈局文件應該是這樣的,

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    > 
    <ListView android:id="@+id/ListView01" android:layout_height="wrap_content" 
    android:layout_width="fill_parent"> 
    </ListView> 

    <TextView android:text="@+id/TextView01" android:id="@+id/TextView01" 
    android:layout_width="wrap_content" android:layout_height="wrap_content" 
    android:layout_marginLeft="10px" android:textColor="#0099CC"></TextView> 

</LinearLayout> 
相關問題