单选按钮未初始化

3
我目前正在开发一款应用程序,用于将一个数值系统的值转换为另一个数值系统的值。 我已经定义了一些代码,允许用户设置他/她“喜欢”的转换。
以下代码读取“转换自”和“转换到”的存储值,并设置滑动视图中对应的片段。此外,片段中对应的单选按钮也被选中: (更新后的代码):
 public void putFav() {
    RadioButton b1,b2,b3;
    String from=prefs.getString("from", "--");
    String to=prefs.getString("to", "--");
    if(from.contentEquals("Binary"))
    {
        b1=(RadioButton)findViewById(R.id.bToD);
        b2=(RadioButton)findViewById(R.id.bToO);
        b3=(RadioButton)findViewById(R.id.bToH);
        mViewPager.setCurrentItem(1);
        BinFragment ob=new BinFragment();
        ob.onCreateView(getLayoutInflater(), mViewPager, mCollectionPagerAdapter.args);
        if(to.contentEquals("Decimal"))
        {
            //Toast.makeText(getApplication(), String.valueOf(ob.DecButton.getId())+" "+String.valueOf(ob.OctButton.getId())+" "+String.valueOf(ob.HexButton.getId())+" ", Toast.LENGTH_LONG).show();
            b1.setChecked(true);
            b2.setChecked(false);
            b3.setChecked(false);
        }
        if(to.contentEquals("Octal"))
        {

            b1.setChecked(false);
            b2.setChecked(true);
            b3.setChecked(false);
        }
        if(to.contentEquals("Hexadecimal"))
        {

            b1.setChecked(false);
            b2.setChecked(false);
            b3.setChecked(true);
        }
    }
    else if(from.contentEquals("Decimal"))
    {
        b1=(RadioButton)findViewById(R.id.dToB);
        b2=(RadioButton)findViewById(R.id.dToO);
        b3=(RadioButton)findViewById(R.id.dToH);
        mViewPager.setCurrentItem(0);
        DecFragment ob=new DecFragment();
        ob.onCreateView(getLayoutInflater(), mViewPager, mCollectionPagerAdapter.args);
        if(to.contentEquals("Binary"))
        {
            b1.setChecked(true);
            b2.setChecked(false);
            b3.setChecked(false);

        }
        if(to.contentEquals("Octal"))
        {

            b1.setChecked(false);
            b2.setChecked(true);
            b3.setChecked(false);
        }
        if(to.contentEquals("Hexadecimal"))
        {
            b1.setChecked(false);
            b2.setChecked(false);
            b3.setChecked(true);
        }
    }
    else if(from.contentEquals("Octal"))
    {
        b1=(RadioButton)findViewById(R.id.oToB);
        b2=(RadioButton)findViewById(R.id.oToD);
        b3=(RadioButton)findViewById(R.id.oToH);
        mViewPager.setCurrentItem(2);
        OctFragment ob=new OctFragment();
        ob.onCreateView(getLayoutInflater(), mViewPager, mCollectionPagerAdapter.args);
        if(to.contentEquals("Decimal"))
        {
            Toast.makeText(getApplication(), String.valueOf(ob.BinButton.getId())+" "+String.valueOf(ob.DecButton.getId())+" "+String.valueOf(ob.HexButton.getId())+" ", Toast.LENGTH_LONG).show();
            /*b1.setChecked(false);
            b2.setChecked(true);
            b3.setChecked(false);*/
        }
        if(to.contentEquals("Binary"))
        {

            b1.setChecked(true);
            b2.setChecked(false);
            b3.setChecked(false);
        }
        if(to.contentEquals("Hexadecimal"))
        {

            b1.setChecked(false);
            b2.setChecked(false);
            b3.setChecked(true);
        }
    }
    else if(from.contentEquals("Hexadecimal"))
    {

        b1=(RadioButton)findViewById(R.id.hToB);
        b2=(RadioButton)findViewById(R.id.hToO);
        b3=(RadioButton)findViewById(R.id.hToD);
        mViewPager.setCurrentItem(3);
        HexFragment ob=new HexFragment();
        ob.onCreateView(getLayoutInflater(), mViewPager, mCollectionPagerAdapter.args);
        if(to.contentEquals("Decimal"))
        {
            //Toast.makeText(getApplication(), String.valueOf(ob.BinButton.getId())+" "+String.valueOf(ob.OctButton.getId())+" "+String.valueOf(ob.DecButton.getId())+" ", Toast.LENGTH_LONG).show();
            b1.setChecked(false);
            b2.setChecked(false);
            b3.setChecked(true);
        }
        if(to.contentEquals("Octal"))
        {

            b1.setChecked(false);
            b2.setChecked(true);
            b3.setChecked(false);
        }
        if(to.contentEquals("Binary"))
        {

            b1.setChecked(true);
            b2.setChecked(false);
            b3.setChecked(false);
        }
    }
    else
    {
        Toast.makeText(this, "Favourites Not Declared. Settings->Set Favourite Conversion", Toast.LENGTH_LONG).show();
    }

}

我遇到的问题是,在“二进制”和“十进制”if语句中,单选按钮被正确初始化并且代码可以正常工作,但在“八进制”和“十六进制”部分中,即使执行了该代码,单选按钮也没有被正确初始化。所有ID都是正确的,我已经进行了交叉检查。 以下是一个片段(八进制)的layout.xml,它属于滑动视图:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".OctFragment" 
>

<TextView
    android:id="@+id/textView1"
    android:textColor="#FFFFFF"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="10dp"
    android:text="@string/oct_msg"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/editText1"
    android:textColor="#FFFFFF"
    android:layout_width="125dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_alignLeft="@+id/textView1"
    android:digits="01234567."
    android:layout_marginTop="10dp"
    android:ems="10"
    android:inputType="number" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/textView2"
    android:textColor="#FFFFFF"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/editText1"
    android:layout_below="@+id/editText1"
    android:layout_marginTop="45dp"
    android:text="@string/convert_msg"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<RadioButton
    android:id="@+id/oToB"
    android:textColor="#FFFFFF"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_below="@+id/textView2"
    android:layout_marginTop="2dp"
    android:text="@string/binary" />

<RadioButton
    android:id="@+id/oToD"
    android:textColor="#FFFFFF"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/oToB"
    android:layout_below="@+id/oToB"
    android:layout_marginTop="1dp"
    android:text="@string/decimal" />

<RadioButton
    android:id="@+id/oToH"
    android:layout_width="wrap_content"
    android:textColor="#FFFFFF"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/oToD"
    android:layout_below="@+id/oToD"
    android:layout_marginTop="1dp"
    android:text="@string/hexa" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#FFFFFF"
    android:layout_below="@+id/oToH"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="25dp"
    android:text="@string/convert" />

<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:textColor="#FFFFFF"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView2"
    android:layout_toRightOf="@+id/editText1"
    android:layout_margin="5dp"
    android:text="@string/equals"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:textColor="#FFFFFF"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView2"
    android:layout_alignParentRight="true"
    android:layout_toRightOf="@+id/textView5"
    android:layout_margin="5dp"
    android:hint="@string/null_string"
    android:text="@string/null_string"
    android:textAppearance="?android:attr/textAppearanceMedium" />

如果有人知道解决方法或问题的原因,请回复。 :) 我还在学习Java编程,所以如果代码效率低下且幼稚,请谅解。 提前感谢您。 :)

5
b1 = (RadioButton)findViewById(R.id.<your view id here>);不要硬编码视图ID,使用“R.id.<您的视图ID>”中的值。整数值经常更改,使用“R.id.”中的值将确保其正确。 - Simon Forsberg
请发布您的.xml布局文件,并使用布局中的id。 - Philipp Jahoda
你说“在八进制和十六进制部分,单选按钮没有被初始化”,所以我必须问一下:当你运行代码的这部分时会发生什么?应用程序会崩溃吗?还是在你的活动中什么也没有显示?因为如果应用程序没有崩溃并且似乎什么都没有发生,那么它们已经被初始化了,只是初始化为一个当前不可见的单选按钮。 - Simon Forsberg
是的,应用程序崩溃了。在调试错误时,我注意到RadioButton变量仍然为“null”。 - anks257
你们在解决这个问题的过程中如何?有什么进展吗?你们解决了吗? - Simon Forsberg
嗨,是的,我自己解决了。问题在于视图本身没有得到初始化。所以我只是交换了几个语句,并先调用了setCurrentItem(),然后初始化了按钮。那样完美地解决了 :) - anks257
1个回答

0

你的二进制、十进制、八进制和十六进制代码中没有指定相同的id。这就是为什么我拒绝相信All the id's are correct。你需要根据我的建议更改你的代码,展示你的layout.xml、R.java文件,然后我才会相信硬编码的id值不是问题所在。

所以我再次强调:

永远不要像那样硬编码视图id(很抱歉,但我真的无法强调这一点)。使用R.id.中的值代替。整数值经常变化,使用R.id.中的值可以确保它是正确的。如果你在代码中引用了R.id.值,我相信它会对你有帮助。


感谢您的关注。 我最初使用标识符标签代替值,但我认为这可能会有问题,所以暂时更改了。但是同样的问题仍然存在。 我认为这与if-else-if阶梯逻辑有关。因为“十进制”代码完美运行,但后面两个代码不行。 - anks257
请编辑您的问题并添加更新后的代码以及您的layout.xml文件,我可以查看。 - Simon Forsberg
我已更新我的问题,包括更改后的代码和布局文件,针对其中一个片段(出现问题的片段)。 - anks257

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接