单选按钮未初始化



我目前正在开发一个应用程序,用于将值从一个数字系统转换为另一个数字系统。我已经定义了一些代码,允许用户设置他/她的"收藏夹"转换。

以下代码读取"转换自"和"转换为"的存储值,并在轻扫视图中设置相应的片段。此外,还检查了片段中相应的单选按钮:(更新的代码):

 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都是正确的,我已经交叉检查了它们。这是轻扫视图中其中一个片段(八进制)的布局.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编程,所以如果代码效率低下和孩子气,请原谅我。提前谢谢。:)

您不必在二进制、十进制、八进制和十六进制代码中指定相同的 ID。这就是为什么我拒绝相信All the id's are correct.您必须根据我在下面的建议更改代码,向我展示您的布局.xml,R.java,然后我会相信硬编码的 id 值不是问题。

所以我再说一遍:

从来没有

从来没有 从来没有 从来没有使用 R.id 中的值。相反。整数值经常使用 R.id 中的值进行更改。将确保它是正确的。如果您在代码中引用R.id.值,我相信它会为您工作。

最新更新