0

我对 Android 还很陌生,正在尝试在我的 android 应用程序中构建一个自定义组件。为此,我扩展了 FrameLayout,以便我的自定义组件可以包含视图顶部的微调器(我最终将在视图中绘制,但目前它只是空白)。

在我扩展 FrameLayout 的类中,我想设置微调器按钮,我已经在 xml 中布置了它。但是,从该类中调用 findViewById 会返回 null。通过阅读此处类似问题的答案,我了解到我需要从活动中调用 findViewById(实际上,如果我从扩展活动的类中设置微调器,一切正常)。然后一种解决方案是将活动传递给我的 FrameLayout 类的构造函数。但是,我从来没有自己调用我的 FrameLayout 类的构造函数,因为我使用了一个 layoutinflater 来给它充气。我假设布局充气器正在调用我的 FrameLayout 类的构造函数,并且我不确定如何在其构造函数调用中添加参数。

谁能告诉我我做错了什么,或者我应该如何解决这个问题?

这是我的代码的简化版本:

我的自定义 FrameLayout 类:

package com.example.myoscilloscope.gui;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.FrameLayout;
import android.widget.Spinner;

import com.example.myoscilloscope.R;

public class Oscilloscope extends FrameLayout {

    //variables
    private Spinner chanSelector; //the channel selector

    // overloading constructors
    public Oscilloscope(Context context) { this(context, null, 0); }
    public Oscilloscope(Context context, AttributeSet attrs) { this(context, attrs, 0); }

    public Oscilloscope(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

            //set up the spinner.
            chanSelector = (Spinner) findViewById(R.id.channel_spinner);

            if(chanSelector == null){
                Log.d("FROMTOM", "CHANNELSELECTOR IS NULL!");
            }

    }

}

我的活动课程很大,但是按下按钮时示波器会膨胀,所以相关部分(我认为)是:

import com.example.myoscilloscope.gui.Oscilloscope;

// ...

public void addchannel(View v){ 
    //this initialization is actually done elsewhere, but I've copied it here
    //  for simplicity's sake
    private LayoutInflater oscilloscopeInflater; 
    LinearLayout myOscilloscopeHolder;

    oscilloscopeInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    myOscilloscopeHolder = (LinearLayout)findViewById(R.id.oscilloscopeHolder); 

    Oscilloscope myOscilliscope = (Oscilloscope)oscilloscopeInflater.inflate(R.layout.oscilloscope, myOscilloscopeHolder, false);       
    myOscilloscopeHolder.addView(trOscilloscopes[trChannelsDisplayed]);
}

这是我的 Oscilloscope.xml 文件,它由我的主程序扩展:

<?xml version="1.0" encoding="utf-8"?>
<com.example.myoscilloscope.gui.Oscilloscope xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/oscilloscope"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1" >

    <com.example.myoscilloscope.gui.Oscillator
        android:id="@+id/oscillator"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

       <Spinner 
        android:id="@+id/channel_spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
    />


</com.example.myoscilloscope.gui.Oscilloscope>

这是我的 main.xml 的相关部分:

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/background_color" >

    <LinearLayout
        android:id="@id/oscilloscopeHolder"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@id/rg_channels"
        android:layout_toLeftOf="@id/HistogramArea"
        android:orientation="vertical"
        android:background="#FFFFFF" >


    </LinearLayout>
</RelativeLayout>

希望这有点道理。任何见解将不胜感激。

汤姆

4

2 回答 2

4

哎呀抱歉没有注意到这些是不同的观点……这应该可以解决您的问题。

您不应该在构造函数中使用 findViewById,因为此时膨胀尚未完成。将 findViewById 代码移动到名为onFinishInflate().

    public class Oscilloscope extends FrameLayout {

        private Spinner chanSelector; //the channel selector

        public Oscilloscope(Context context) { this(context, null, 0); }
        public Oscilloscope(Context context, AttributeSet attrs) { this(context, attrs, 0); }
        public Oscilloscope(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }

        @Override
        public void onFinishInflate(){
                super.onFinishInflate();        

                chanSelector = (Spinner) findViewById(R.id.channel_spinner);

                if (chanSelector == null) {
                    //Should never get here
                }
        }
    }
于 2012-03-22T20:08:31.523 回答
0

你可以迭代孩子......

getChildCount();
getChildAt(index);

然后,您可以将子 ID 与您要查找的内容进行比较。

于 2012-03-22T20:14:04.137 回答