【发布时间】:2013-09-25 01:51:20
【问题描述】:
我目前正在开发一个应用程序来将值从一个数字系统转换为另一个数字系统。 我已经定义了一些代码,允许用户设置他/她的“最喜欢的”转换。
以下代码读取“conversion from”和“conversion to”的存储值,并在滑动视图中设置相应的片段。还检查了片段中相应的 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 都是正确的,我已经交叉检查了它们。 这是来自滑动视图的片段(八进制)之一的 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 编程,如果代码效率低下且幼稚,请见谅。 提前致谢。 :)
【问题讨论】:
-
b1=(RadioButton)findViewById(2131296268);NEVER 这样的硬编码视图 ID。请改用来自R.id.<your view id here>的值。整数值经常变化,使用来自R.id.的值将确保它是正确的。 -
发布您的 .xml 布局文件并使用布局中的 id。
-
你说“在“八进制”和“十六进制”部分,RadioButtons 没有被初始化”,所以我不得不问:当你运行那部分代码时会发生什么?应用程序会崩溃吗?它只是没有在您的活动中显示任何内容吗?因为如果应用程序没有崩溃并且似乎什么也没发生,那么它们就会被初始化,它们只是被初始化为当前不可见的 RadioButton。
-
是的,应用程序崩溃了。在调试错误时,我注意到 RadioButton 变量保持为“空”
-
这个问题的过程对您来说进展如何?任何新闻?你解决了吗?
标签: android radio-button swipe