2011-06-28 75 views
1

我正在嘗試創建一個行佈局,其中每個列表項都有一個要遵循的模板。目前我有這個,它允許我在每個列表中顯示一行文本。Android XML佈局問題

<?xml version="1.0" encoding="utf-8"?> 
<TextView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:textSize="20sp" 
    android:padding="100dp" > 
</TextView> 

然而,當我試圖改變它,讓我增加更多的項目,如圖像按鈕和更多的文本字段,它總是不允許我進行編譯。

我試着developer.android的網站是拿一塊XML從一個教程,

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

<TextView android:id="@+id/text1" 
    android:textSize="16sp" 
    android:textStyle="bold" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"/> 

<TextView android:id="@+id/text2" 
    android:textSize="16sp" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"/> 
</LinearLayout> 

但它引發了我的錯誤,說明 - 處理指令目標匹配「[XX] [MM] [11] 「不允許 。 - 錯誤:解析XML錯誤:XML或文本聲明不在實體的開始

有人可以幫助我嗎?我不太確定這個錯誤的含義。

UPDATE:

什麼,我試圖做的是有像右側的邊欄,顯示的項目清單,與我張貼的第一個XML片段,我能拿到我想要的結果,但我無法對其進行任何更改。

package com.project.test; 

import android.app.ListFragment; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 

public class TestListFragment extends ListFragment { 

    String [] Items = {"Item A", "Item B", "Item C"}; 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     setListAdapter(new ArrayAdapter<String>(getActivity(), 
       R.layout.listtemplate, Items)); 
    } 

    @Override 
    public void onListItemClick(ListView l, View v, int position, long id) { 
     Log.i("FragmentList", "Item clicked: " + id); 
    } 
} 
+0

不更新你的問題,使它成爲完全不同的一個。馬茲的回答是100%正確的,現在它是多餘的。你應該已經標記了它的答案並開始了一個新的問題。 – Blundell

回答

2

可能是因爲您缺少LinearLayout的開始標記?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:orientation="vertical"> 

<TextView android:id="@+id/text1" 
    android:textSize="16sp" 
    android:textStyle="bold" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"/> 

<TextView android:id="@+id/text2" 
    android:textSize="16sp" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"/> 
</LinearLayout> 
+0

感謝您的回覆,這解決了xml拋出錯誤的問題,但現在它會拋出一個非法的狀態,爲什麼會這樣呢? –

+0

嗨簡化,我敢肯定,異常沒有任何關係的XML文件。但嘗試粘貼它,我們可以看到。 –

+0

感謝您的耐心等待,我使用片段內的代碼更新了問題。 –

21

你不會相信,要解決這個...我有同樣的問題,答案是「刪除所有的空格之前啓動XML第一線」

+1

+1我在xml聲明之前插入了一條評論(<! - Some Comment-- >這是我的情況的問題和linearLayout – AgentKnopf

+1

失蹤結束標記我相信你:) – Vaibs