片段UI组件为null TextViews

时间:2014-02-20 20:26:55

标签: android android-fragments null textview fragment

这里有新的Android开发人员。我正在尝试创建一个动态UI,根据用户对RadioGroup的选择加载。根据他们的选择,3个可能的片段之一将被加载到LinearLayout部分。这是我第一次尝试自己的示例问题,而不仅仅是一个演练教程。这是主要活动:

public class BaseConverter extends Activity {
    RadioGroup convert;
    Fragment toFragment;

    RadioGroup toRadioGroup = null;
    TextView inputDisplay = null;
    TextView outputDisplay = null;
    TextView resultTitle = null;    

    @Override
    public void onCreate(Bundle sIS) {
        super.onCreate(sIS);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.setContentView(R.layout.base_converter);
        convert = (RadioGroup) this.findViewById(R.id.bc_convert_group);
        convert.setOnCheckedChangeListener(new ConvertListener());

        FragmentManager fm = getFragmentManager();

        FragmentTransaction converterFragment = fm.beginTransaction();
        ConvertEmptyFragment emptyTo = new ConvertEmptyFragment();
        converterFragment.replace(R.id.bc_converter_fragment, emptyTo);
        converterFragment.commit();

        FragmentTransaction toFragment = fm.beginTransaction();
        ConvertEmptyFragment emptyConverter = new ConvertEmptyFragment();
        toFragment.replace(R.id.bc_to_fragment, emptyConverter);
        toFragment.commit();
    }

    @Override
    public void onResume() {
        convert.clearCheck();
        super.onResume();
    }

    @Override
    public void onPause() {
        convert.clearCheck();
        super.onPause();
    }

    // I put a little null check so you can see how I'm trying to access the TextViews and what results
    public void updateUIComponents(){
        View converterView = this.findViewById(R.id.bc_converter_fragment);
        inputDisplay = (TextView)converterView.findViewById(R.id.bc_display_input);
        outputDisplay = (TextView)converterView.findViewById(R.id.bc_display_output);
        if (inputDisplay == null){
            Log.d("BaseConverter", "inputDisplay == null");
        } else {
            Log.d("BaseConverter", "inputDisplay != null");
        }
    }

    class ConvertListener implements OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            Fragment toFragment;
            Fragment converterFragment;
            switch (checkedId) {
            case R.id.bc_convert_binary:
                toFragment = new ConvertRBFragmentBinary();
                converterFragment = new ConverterFragmentBinary();
                break;
            case R.id.bc_convert_decimal:
                toFragment = new ConvertRBFragmentDecimal();
                converterFragment = new ConverterFragmentDecimal();
                break;
            case R.id.bc_convert_hex:
                toFragment = new ConvertRBFragmentHex();
                converterFragment = new ConverterFragmentHex();
                break;
            default:
                toFragment = new ConvertEmptyFragment();
                converterFragment = new ConvertEmptyFragment();
                break;
            }
            FragmentManager fm = getFragmentManager();

            FragmentTransaction converterTransaction = fm.beginTransaction();
            converterTransaction.replace(R.id.bc_converter_fragment, converterFragment);
            converterTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            converterTransaction.commit();

            FragmentTransaction toTransaction = fm.beginTransaction();
            toTransaction.replace(R.id.bc_to_fragment, toFragment);
            toTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            toTransaction.commit();

            updateUIComponents();
        }
    }

因此,根据用户选择的内容,将正确的片段加载到相应的LinearLayout部分中。但是,现在我想实现片段的业务逻辑(这只是整数基本转换;即二进制数到十进制......)但是当我尝试访问TextViews时,如updateUIComponents方法中所见,我得到空指针。我错过了什么?

这是ConverterFragmentBinary类供参考:

public class ConverterFragmentBinary extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sIS){
        View v = inflater.inflate(R.layout.converter_fragment_binary, container, false);
        return v;
    }   
}    

及其各自的xml布局供参考:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FF000000"
    android:gravity="center_horizontal"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/bc_binary_converter_logo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="15dip"
        android:maxHeight="30dip"
        android:src="@drawable/binary_converter" />

    <TextView
        android:id="@+id/bc_display_input"
        style="@style/input_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dip"
        android:layout_marginLeft="15dip"
        android:layout_marginRight="15dip"
        android:layout_marginTop="5dip"
        android:gravity="center_vertical|right"
        android:lines="1"
        android:minHeight="30sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FF000000"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button_num_0"
            style="@style/op_button_land"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dip"
            android:gravity="center"
            android:onClick="num0"
            android:text="@string/num_0" />

        <Button
            android:id="@+id/button_num_1"
            style="@style/op_button_land"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dip"
            android:gravity="center"
            android:onClick="num1"
            android:text="@string/num_1" />
    </LinearLayout>

    <TextView
        android:id="@+id/bc_result_title"
        style="@style/radio_button_header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dip"
        android:layout_marginRight="5dip"
        android:layout_marginTop="10dip"
        android:gravity="left"
        android:text="@string/choose_convert" />

    <TextView
        android:id="@+id/bc_display_output"
        style="@style/display_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dip"
        android:layout_marginRight="15dip"
        android:layout_marginTop="5dip"
        android:gravity="center_vertical|right"
        android:lines="1"
        android:minHeight="30sp" />
</LinearLayout>

然后是它加载的主要活动:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/base_conversion_layout"
    style="@style/layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center_horizontal"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="5"
        android:baselineAligned="false"
        android:gravity="center_vertical|left"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:orientation="vertical" >

            <TextView
                style="@style/radio_button_header"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/convert" />

            <RadioGroup
                android:id="@+id/bc_convert_group"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:padding="5dip" >

                <RadioButton
                    android:id="@+id/bc_convert_binary"
                    style="@style/radio_button"
                    android:text="@string/binary" />

                <RadioButton
                    android:id="@+id/bc_convert_decimal"
                    style="@style/radio_button"
                    android:text="@string/decimal" />

               <RadioButton
                    android:id="@+id/bc_convert_hex"
                    style="@style/radio_button"
                    android:text="@string/hex" />
            </RadioGroup>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/bc_to_fragment"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:orientation="vertical" >
        </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/bc_converter_fragment"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="13"
        android:orientation="vertical" >
    </LinearLayout>

</LinearLayout>

提前感谢并为长代码块感到抱歉,但我认为最好包含更多代码。

1 个答案:

答案 0 :(得分:1)

此外,您应该扩充Fragments布局,将其从XML引入Java代码,而不是简单地使用findViewById()方法引用它。

所以不要这样做,

View converterView = this.findViewById(R.id.bc_converter_fragment);

在片段的onCreateView方法中执行此操作,

View converterView = infalter.inflate(R.id.bc_converter_fragment,null);
updateUIComponents(converterView);//call this methid and pass your view

新方法看起来像这样,

public void updateUIComponents(View converterView){

    inputDisplay = (TextView)converterView.findViewById(R.id.bc_display_input);
    outputDisplay = (TextView)converterView.findViewById(R.id.bc_display_output);
    if (inputDisplay == null){
        Log.d("BaseConverter", "inputDisplay == null");
    } else {
        Log.d("BaseConverter", "inputDisplay != null");
    }
}