2013-05-18 2 views
0

Я пытаюсь создать составной элемент управления и установить для него настраиваемые атрибуты.Пользовательский составной элемент управления - несвязанный префикс, не удалось создать экземпляр

Класс для управления:

package hu.ppke.itk.kozcs.android.activities; 

import hu.ppke.itk.kozcs.android.project.R; 
import android.content.Context; 
import android.content.res.TypedArray; 
import android.util.AttributeSet; 
import android.view.LayoutInflater; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 

public class SettingBox extends RelativeLayout { 

    public SettingBox(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     String service = Context.LAYOUT_INFLATER_SERVICE; 
     LayoutInflater li = (LayoutInflater) getContext().getSystemService(service); 
     RelativeLayout layout = (RelativeLayout) li.inflate(R.layout.setting_box, this, true); 
     TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SettingBox); 
     String title = a.getString(R.styleable.SettingBox_title); 
     String description = a.getString(R.styleable.SettingBox_description); 

     TextView titleText = (TextView) layout.findViewById(R.id.title); 
     TextView descriptionText = (TextView) layout.findViewById(R.id.description); 

     titleText.setText(title); 
     descriptionText.setText(description); 

     a.recycle(); 
    } 


} 

разреш/значения/attrs.xml:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 

    <declare-styleable name="SettingBox"> 
     <attr name="title" format="string" /> 
     <attr name="description" format="string" /> 
    </declare-styleable> 

</resources> 

Схема XML для контроля (setting_box.xml):

<?xml version="1.0" encoding="utf-8"?> 
<merge xmlns:android="http://schemas.android.com/apk/res/android" > 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_marginLeft="5dp" 
     android:layout_marginTop="5dp" 
     android:layout_toLeftOf="@+id/checkbox" 
     android:text="@string/use_gps" 
     android:textColor="@android:color/white" 
     android:textSize="20sp" /> 

    <TextView 
     android:id="@+id/description" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/title" 
     android:layout_marginLeft="5dp" 
     android:layout_toLeftOf="@+id/checkbox" 
     android:text="@string/gps_descr" 
     android:textSize="13sp" /> 

    <CheckBox 
     android:id="@+id/checkbox" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:layout_marginRight="10dp" /> 

</merge> 

и, наконец, макет, где я использую этот элемент управления (settings.xml):

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

    <hu.ppke.itk.kozcs.android.activities.SettingBox 
     andrid:id="@+id/test_setting_box" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:description="description" 
     app:title="title" /> 

</LinearLayout> 

Каждый раз, когда я сохранить последний XML, я получил сообщение об ошибке: settings.xml:9: error: Error parsing XML: unbound prefix

Также есть ошибка в журнале: hu.ppke.itk.kozcs.android.activities.SettingBox failed to instantiate.

Что я здесь отсутствует?

+0

в слиянии тег, вы можете удалить пробел между "и>? – Blackbelt

+0

Должен ли я? На самом деле код форматировщик его туда. – WonderCsabo

+0

Какой из settings.xml? – Blackbelt

ответ

3

Ваш android:id атрибут называется andrid:id.

<hu.ppke.itk.kozcs.android.activities.SettingBox 
    andrid:id="@+id/test_setting_box" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    app:description="description" 
    app:title="title" /> 
+2

nice catch. +1 – Blackbelt

+0

@blackbelt спасибо;) – mmBs

+1

Я не могу в это поверить. Я искал эту ошибку часами. :) Спасибо. – WonderCsabo

Смежные вопросы