xml - 使用 Typedarray 的自定义 View 返回 null 所有参数

标签 xml android-layout android-custom-view android

我必须创建一个自定义线性布局以供重复使用。线性布局有imageview和textview。

我正在关注当前 creating custom layout

请找到我的代码

MainActivity.java

    package com.customview.compoundview;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Toast;
import com.customview.compoundview.R;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);
    }

    public void onClicked(View view) {
        String text = view.getId() == R.id.view1 ? "Background" : "Foreground";
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
      }



}

ColorOptionsView.java

/**
 * 
 */
package com.customview.compoundview;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.customview.compoundview.R;

/**
 * @author gaurav
 * Feb 18, 2015
 */
public class ColorOptionsView extends LinearLayout {

    ImageView imageButton_icon;
    TextView textView_caption;

    public ColorOptionsView(Context context) {
        super(context);
    }

    public ColorOptionsView(Context context, AttributeSet attrs) {
        super(context, attrs);
        Log.d("ColorOptionsView",""+1);
        LayoutInflater inflater = LayoutInflater.from(context);
        Log.d("ColorOptionsView",""+2);
        inflater.inflate(R.layout.view_color_options, this);
        Log.d("ColorOptionsView",""+3);

    imageButton_icon = (ImageView) findViewById(R.id.imageView);
    textView_caption = (TextView) findViewById(R.id.textView_caption);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CaptionButton);
        Log.d("ColorOptionsView",""+4);
        Drawable icon;
        String caption;
        try{
            icon = a.getDrawable(R.styleable.CaptionButton_button_icon);
            Log.d("ColorOptionsView",""+5);
            caption = a.getString(R.styleable.CaptionButton_caption);
            Log.d("ColorOptionsView",""+6 +caption );
        }
        finally
        {
            a.recycle();
        }

        setIcon(icon);
        setCaption(caption);
    }

    public void setIcon(Drawable icon) {
        try{
            Log.d("ColorOptionsView",""+7 +icon );
            imageButton_icon.setImageDrawable(icon);
        }
        finally{
            System.out.println("error in seticon "+icon);
        }
    }

    public void setCaption(String caption) {
        try{
        Log.d("ColorOptionsView",""+8 +caption );
        textView_caption.setText(caption);
        }
        finally
        {
            System.out.println("error in setCaption"+caption);
        }
    }
}

fragment_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:myapp="http://schemas.android.com/apk/lib/com.vogella.android.view.compoundview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:showDividers="middle"
    android:divider="?android:attr/listDivider"
    tools:context=".MainActivity" >

        <com.customview.compoundview.ColorOptionsView

            android:id="@+id/view1"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            myapp:caption="background color"
            myapp:button_icon="@drawable/handy_man"
             />
        <TextView 
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="#f0f"
        android:text="hdffdlsakjfkdasj"
        />

        <com.customview.compoundview.ColorOptionsView

            android:id="@+id/view1"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            myapp:caption="Foreground color"
            myapp:button_icon="@drawable/history"
             />


</LinearLayout>

view_color_options.xml

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

              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/linearLayout"
              android:orientation="horizontal">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:src="@drawable/handy_man"/>

    <TextView
        android:layout_marginLeft="50dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Caption fdafasfs"
        android:id="@+id/textView_caption"
        android:gravity="center_horizontal"/>

</LinearLayout>

attrs.xml

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

    <declare-styleable name="CaptionButton">
        <attr name="button_icon" format="reference" />
        <attr name="caption" format="string" />
    </declare-styleable>

</resources>

由于收到空值,我在设置文本和图标时总是出错。这是 logcat 输出

02-18 17:25:26.057: E/AndroidRuntime(16164): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.customview.compoundview/com.customview.compoundview.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class com.customview.compoundview.ColorOptionsView
02-18 17:25:26.057: E/AndroidRuntime(16164): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class com.customview.compoundview.ColorOptionsView
02-18 17:25:26.057: E/AndroidRuntime(16164):    at com.customview.compoundview.ColorOptionsView.setIcon(ColorOptionsView.java:61)
02-18 17:25:26.057: E/AndroidRuntime(16164):    at com.customview.compoundview.ColorOptionsView.<init>(ColorOptionsView.java:54)

任何帮助将不胜感激。提前致谢。

现在它不会给出任何错误,但不会创建线性布局。输出如下。 enter image description here

最佳答案

ImageButton imageButton_icon;
TextView textView_caption;

永远不会被初始化。即使您调用 inflater,将 this 作为参数,您也必须调用 findViewById 来初始化这两个成员。我还注意到您正在实例化 LayoutInflaterViewGroup 有静态方法 inflate .你可以替换

  inflater.inflate(R.layout.view_color_options, this);

inflate(R.layout.view_color_options, this);

关于xml - 使用 Typedarray 的自定义 View 返回 null 所有参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28583156/

相关文章:

c# - 使用 DataContractSerializer 反序列化对象中的 XML

java - JAXB 何时生成@XmlElementRef 和@XmlElement 类型的注释?

Android 在谷歌地图中添加地点图标

android - 从 android xml 布局文件中提取字符串到 strings.xml 文件的最简单方法是什么?

android - 在自定义 View 中使用进度条?

java spring XML编程的要点是什么?

.net - WCF、ASMX 基本 HTTP 绑定(bind)和 IIS

android - 获取 TapTargetView 的 MenuItem View 引用

android - 如何获取引用属性的值

android - 如何更改起点drawrect customview android