android - RadioButton 未初始化

标签 android radio-button swipe

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

以下代码读取“转换自”和“转换至”的存储值,并在滑动 View 中设置相应的 fragment 。还检查了 fragment 中相应的 RadioButton: (更新代码):

 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 语句中,RadioButtons 被正确初始化并且代码有效,但在“八进制”和“十六进制”部分,RadioButtons 甚至没有被初始化执行该代码时。所有的 id 都是正确的,我已经对它们进行了交叉检查。 这是来自滑动 View 的 fragment 之一(八进制)的 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 编程,如果代码效率低下且幼稚,请原谅。 提前致谢。 :)

最佳答案

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

所以我再说一遍:

NEVER NEVER NEVER NEVER NEVER EVER(对不起,我真的不能强调这一点)这样的硬编码 View ID。使用来自 R.id 的值。反而。整数值经常变化,使用来自 R.id 的值。将确保它是正确的。如果您在代码中引用 R.id. 值,我相信它对您有用。

关于android - RadioButton 未初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18903818/

相关文章:

ios - UITableViewCell - 发送到实例的无法识别的选择器

android - SwipeListview openanimate(postion)方法不起作用

java - 无法看到保存的图像,因为它在 imageview 中看起来

android - 从intellij 12导入项目到android studio beta

android - 如何在按钮单击时调用或弹出 fragment

css - struts2 s :radio cssClass and cssStyle add attribute twice

iphone - 如何在 iphone Objective-c 中用手指移动 View

android - Android 上的 Delphi 阿拉伯字符显示问题

android - Radiobutton drawable 对 state_checked 没有反应

javascript - 删除不适用于 MooTools 的类