2015-05-02 32 views
1

我有通過適配器顯示從Web XML提要取出一個listActivity,ADN佈局文件activity_list_feed.xml:我不能添加按鈕線性佈局

`<ListView 
    android:id="@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:drawSelectorOnTop="false" 
    android:transcriptMode="normal" 
    />` 

在圖形編輯器中我不能拖動按鈕進入這個佈局,當我嘗試硬編碼按本文件:

`<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 
<Button 
    android:id="@+id/addBtn" 
    android:text="Add New Item" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:onClick="addItems"/> 
<ListView 
    android:id="@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:drawSelectorOnTop="false" 
    android:transcriptMode="normal" 
    /> 
</LinearLayout>` 

我得到一個編譯錯誤消息:

java.lang.ClassCastException:android.widget.LinearLayout不能被投射到android.widget.TextView

如何在列表頂部添加按鈕,因爲我想刷新pull.thank。

回答

0

您提供的代碼實際工作在Android Studio中的當前狀態:

請確保您的Java代碼,引用的是正確的項目。確保您將總體佈局設置爲該佈局,然後執行findViewById(R.layout.addBtn)

此外,請嘗試將您的IDE中的Android版本更改爲22(因爲我已將它設置在圖片的右上角)。這可能會解決你的錯誤。

0

我設法在編輯器中添加按鈕,而不必按照圖片設置爲API 22。但是該程序仍然沒有編譯。

消息錯誤「ArrayAdapter需要資源ID爲TextView」,這意味着我沒有爲適配器提供正確的參數。

根據這樣的回答: "ArrayAdapter requires the resource ID to be a TextView" xml problems 看來該構造的選擇是很重要的,因爲我想在視圖中添加一個按鈕,我必須使用4個參數的構造函數(附加參數是的id查看): http://developer.android.com/reference/android/widget/ArrayAdapter.html#ArrayAdapter(android.content.Context,int,int,java.util.List)

使用這個構造函數解決了這個問題。