How to RadioButton get selected - java

I have an API and there is a value of broadcast and the options are public, private, logged in and unlisted. I want a radiobutton to be selected itself as public if broadcast is public.
This is my XML code:
<TextView
android:textColor="#060606"
android:layout_marginTop="10dp"
android:textSize="15sp"
android:text="Broadcast Options"
android:id="#+id/broadcast_head"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioGroup
android:orientation="vertical"
android:id="#+id/radio_group_broadcast"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:textSize="16sp"
android:text="Public"
android:id="#+id/radio_button_public"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:textSize="16sp"
android:text="Private"
android:id="#+id/radio_button_private"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:textSize="16sp"
android:text="Unlisted"
android:id="#+id/radio_button_unlisted"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:textSize="16sp"
android:text="Logged Only"
android:id="#+id/radio_button_logged_only"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
</TextView>
and this is my Java code;
I don't know whether this code is correct or not.
rbPub = (RadioButton)findViewById(R.id.radio_button_public);
rbPri = (RadioButton)findViewById(R.id.radio_button_private);
rbUnl = (RadioButton)findViewById(R.id.radio_button_unlisted);
rbOnl = (RadioButton)findViewById(R.id.radio_button_logged_only);
radioGroup1 = (RadioGroup) findViewById(R.id.radio_group_broadcast);
VideoDetail videoDetailBroadcast = null; //this is the problem, can't fix(null)
if (videoDetailBroadcast.isPublic()) {
rbPub.setChecked(true);
}
else if (videoDetailBroadcast.isPrivate()) {
rbPri.setChecked(true);
}
else if (videoDetailBroadcast.isUnlisted()) {
rbUnl.setChecked(true);
}
else if (videoDetailBroadcast.isLoggedOnly()) {
rbOnl.setChecked(true);
}

You should should initialize videoDetailBroadcast that way : VideoDetail videoDetailBroadcast = new VideoDetail()

Related

How do i ensure that the selection of a radio button from a radio group affects the selection of a radio button in a different radio group.

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:paddingLeft="24dp"
android:text="1. Do you have an anti-virus installed on your computer :"
android:textAllCaps="false" />
<RadioGroup
android:id="#+id/radio_virus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="48dp">
<RadioButton
android:id="#+id/radio_virusyes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:onClick="assessUserInput"
android:text="Yes" />
<RadioButton
android:id="#+id/radio_virusno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="assessUserInput"
android:text="No" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="48dp"
android:text="If No, skip question 2"
android:textAllCaps="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingLeft="24dp"
android:text="2. How many anti-viruses do you use on your laptop :"
android:textAllCaps="false" />
<RadioGroup
android:id="#+id/radio_avnumber"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="48dp">
<RadioButton
android:id="#+id/radio_avnumberone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="assessUserInput"
android:text="1" />
<RadioButton
android:id="#+id/radio_avnumbermore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="assessUserInput"
android:text="More than 1" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:paddingLeft="24dp"
android:text="3. Do you use pop-up/advert blockers:"
android:textAllCaps="false" />
<RadioGroup
android:id="#+id/radio_adblock"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="48dp">
<RadioButton
android:id="#+id/radio_adblockyes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:onClick="assessUserInput"
android:text="Yes" />
<RadioButton
android:id="#+id/radio_adblockno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="assessUserInput"
android:text="No" />
</RadioGroup>
Instead of writing text to guide a user to skip question 2 after he
selects "No" in question 1, i want to be able to find a way to disable question two using java or xml codes when the user selects a "No". or automatically take question two away.
Activity:
(Note:i dont know why you want the strings so i separated them from the disabling method but with this it will disable the radio buttons if the user pressed no and they will enable when the user presses yes)
public class main extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainz);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio_virus);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioGroup rg1 = (RadioGroup) findViewById(R.id.radio_avnumber);
Log.w("","click!");
if (checkedId == R.id.radio_virusno) {
Log.w("","asdasda");
for (int i = 0; i < rg1.getChildCount(); i++) {
rg1.getChildAt(i).setEnabled(false);
}
}else
{
Log.w("","else");
for (int i = 0; i < rg1.getChildCount(); i++) {
rg1.getChildAt(i).setEnabled(true);
}
}
}
});
}
public void assessUserInput(View view){
//// constructor method for Radio Group : Radio_virus
RadioGroup rgVirus=(RadioGroup)findViewById(R.id.radio_virus);
String radioVirus=((RadioButton)findViewById(rgVirus.getCheckedRadioButtonId())).getText().toString();
//// constructor method for Radio Group : Radio_avnumber
RadioGroup rgAvnNumber=(RadioGroup)findViewById(R.id.radio_avnumber);
String radioAvNumber=((RadioButton)findViewById(rgAvnNumber.getCheckedRadioButtonId())).getText().toString();
/// constructor method for Radio Group : Radio_adblock
RadioGroup rgAdBlock=(RadioGroup)findViewById(R.id.radio_adblock);
String radioAdBlock=((RadioButton)findViewById(rgAdBlock.getCheckedRadioButtonId())).getText().toString();
// constructor method for Input field
//EditText nameTextField=(EditText)findViewById(R.id.edit_name);
//String hasName=nameTextField.getText().toString();
}
}
Used xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:paddingLeft="24dp"
android:text="1. Do you have an anti-virus installed on your computer :"
android:textAllCaps="false" />
<RadioGroup
android:id="#+id/radio_virus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="48dp">
<RadioButton
android:id="#+id/radio_virusyes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Yes" />
<RadioButton
android:id="#+id/radio_virusno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="48dp"
android:text="If No, skip question 2"
android:textAllCaps="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingLeft="24dp"
android:text="2. How many anti-viruses do you use on your laptop :"
android:textAllCaps="false" />
<RadioGroup
android:id="#+id/radio_avnumber"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="48dp">
<RadioButton
android:id="#+id/radio_avnumberone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" />
<RadioButton
android:id="#+id/radio_avnumbermore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="More than 1" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:paddingLeft="24dp"
android:text="3. Do you use pop-up/advert blockers:"
android:textAllCaps="false" />
<RadioGroup
android:id="#+id/radio_adblock"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="48dp">
<RadioButton
android:id="#+id/radio_adblockyes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Yes" />
<RadioButton
android:id="#+id/radio_adblockno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No" />
</RadioGroup>
</LinearLayout>
You have to handle onClick of your radiobutton :
public void assessUserInput(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radio_virusyes:
if (checked){
for(int i = 0; i < radio_avnumber.getChildCount(); i++){
((RadioButton)radio_avnumber.getChildAt(i)).setEnabled(true);}}
break;
case R.id.radio_virusno:
if (checked){
for(int i = 0; i < radio_avnumber.getChildCount(); i++){
((RadioButton)radio_avnumber.getChildAt(i)).setEnabled(false);}
}
break;
}
}
public void assessUserInput(View view) {
//// constructor method for Radio Group : Radio_virus
RadioGroup rgVirus = (RadioGroup) findViewById(R.id.radio_virus);
String radioVirus = ((RadioButton) findViewById(rgVirus.getCheckedRadioButtonId())).getText().toString();
//// constructor method for Radio Group : Radio_avnumber
RadioGroup rgAvnNumber = (RadioGroup) findViewById(R.id.radio_avnumber);
String radioAvNumber = ((RadioButton) findViewById(rgAvnNumber.getCheckedRadioButtonId())).getText().toString();
/// constructor method for Radio Group : Radio_adblock
RadioGroup rgAdBlock = (RadioGroup) findViewById(R.id.radio_adblock);
String radioAdBlock = ((RadioButton) findViewById(rgAdBlock.getCheckedRadioButtonId())).getText().toString();
// constructor method for Input field
EditText nameTextField = (EditText) findViewById(R.id.edit_name);
String hasName = nameTextField.getText().toString();
// Is the button now checked
radioVirus.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId = radio_virusno) {
//set the style of the things you want to disable here
}
}
});

Android eclipse error TextView and EditView

My name is Marek.
I'm from Poland.
I have problem with my project.
java.lang.ClassCastException: android.widget.TextView cannot be cast andoid.widget.EditView
It's not working on Virtual Device and my devices
It's simple project for my learning development.
Sorry for my english.
java:
cylindr = (TextView)findViewById(R.id.cylindr);
na = (TextView)findViewById(R.id.na);
litry = (TextView)findViewById(R.id.litry);
spalanie = (TextView)findViewById(R.id.spalanie);
obrot = (TextView)findViewById(R.id.obrot);
ok = (Button)findViewById(R.id.ok);
cylindr = (EditText)findViewById(R.id.cylindr);
obroty = (EditText)findViewById(R.id.obroty);
paliwo = (EditText)findViewById(R.id.paliwo);
xml:
<TextView
android:id="#+id/spalanie"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/paliwo"
android:layout_alignParentTop="true"
android:layout_marginTop="24dp"
android:text="#string/Spalanie" />
<EditText
android:id="#+id/paliwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/spalanie"
android:layout_marginLeft="18dp"
android:layout_marginTop="22dp"
android:ems="10"
android:inputType="number"
android:labelFor="#+id/paliwo"
android:singleLine="true" />
<TextView
android:id="#+id/litry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/paliwo"
android:layout_alignBottom="#+id/paliwo"
android:layout_toRightOf="#+id/paliwo"
android:text="#string/litrow" />
<TextView
android:id="#+id/obrot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/paliwo"
android:layout_below="#+id/paliwo"
android:layout_marginTop="28dp"
android:text="#string/obroty" />
<EditText
android:id="#+id/obroty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/obrot"
android:layout_marginTop="24dp"
android:layout_toLeftOf="#+id/litry"
android:ems="10"
android:inputType="number"
android:labelFor="#id/obroty" />
<TextView
android:id="#+id/na"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/obroty"
android:layout_alignBottom="#+id/obroty"
android:layout_toRightOf="#+id/obroty"
android:text="#string/na" />
<TextView
android:id="#+id/cylindr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/obrot"
android:layout_below="#+id/obroty"
android:layout_marginTop="36dp"
android:text="#string/cylindry" />
<EditText
android:id="#+id/cylindry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/cylindr"
android:layout_below="#+id/obroty"
android:layout_marginTop="65dp"
android:ems="10"
android:inputType="number"
android:labelFor="#id/cylindry" />
<Button
android:id="#+id/dalej"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/cylindry"
android:layout_marginTop="31dp"
android:layout_toRightOf="#+id/spalanie"
android:text="#string/ok" />
</RelativeLayout>
Please help me!
cylindr = (EditText)findViewById(R.id.cylindr);
Should be:
cylindr = (EditText)findViewById(R.id.cylindry);
You're currently using R.id.cylindr twice, along with a duplicate variable. You should use:
cylindr = (TextView)findViewById(R.id.cylindr);
na = (TextView)findViewById(R.id.na);
litry = (TextView)findViewById(R.id.litry);
spalanie = (TextView)findViewById(R.id.spalanie);
obrot = (TextView)findViewById(R.id.obrot);
ok = (Button)findViewById(R.id.ok);
cylindry = (EditText)findViewById(R.id.cylindry); //this is the line that was wrong
obroty = (EditText)findViewById(R.id.obroty);
paliwo = (EditText)findViewById(R.id.paliwo);

Cannot show first Data from Date

i have some question about showing data from database sqlite.
if i want to show data from date like startdate and enddate. example from between 01/01/2014 and 03/01/2014. why the data only show from 02/01/2014 - 03/01/2014? where data from 01/01/2014?
and if want to show data from between 01/01/2014 and 01/01/2014, the data cannot show?
Here the program that i have :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/cover" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:layout_marginLeft="10dp" >
<TextView
android:id="#+id/no_id"
android:layout_width="30dip"
android:text="#string/id_label"
android:textStyle="bold" />
<TextView
android:id="#+id/date"
android:layout_width="70dip"
android:text="#string/date_label"
android:textStyle="bold" />
<TextView
android:id="#+id/time"
android:layout_width="90dip"
android:text="#string/time_label"
android:textStyle="bold" />
<TextView
android:id="#+id/status"
android:layout_width="100dip"
android:text="#string/status_label"
android:textStyle="bold" />
</TableRow>
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="170dp"
android:layout_marginBottom="40dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableLayout
android:id="#+id/tabel_log"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp">
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp" >
<View
android:id="#+id/no_id1"
android:layout_width="30dip" />
<View
android:id="#+id/date1"
android:layout_width="70dip" />
<View
android:id="#+id/time1"
android:layout_width="90dip" />
<View
android:id="#+id/status1"
android:layout_width="100dip" />
</TableRow>
</TableLayout>
</LinearLayout>
</ScrollView>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="#string/log"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="12pt"
android:textStyle="bold" />
<Button
android:id="#+id/btnSetDate1"
style="?android:attr/buttonStyleSmall"
android:layout_width="140dp"
android:layout_height="35dp"
android:layout_alignLeft="#+id/tableRow1"
android:layout_below="#+id/textView1"
android:text="#string/firstdate"
android:textStyle="bold" />
<Button
android:id="#+id/btnSetDate2"
style="?android:attr/buttonStyleSmall"
android:layout_width="140dp"
android:layout_height="35dp"
android:layout_alignLeft="#+id/btnSetDate1"
android:layout_below="#+id/btnSetDate1"
android:text="#string/enddate"
android:textStyle="bold" />
<Button
android:id="#+id/btnShow"
style="?android:attr/buttonStyleSmall"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_alignRight="#+id/tableRow1"
android:layout_below="#+id/btnSetDate2"
android:text="#string/show"
android:textStyle="bold" />
<TextView
android:id="#+id/txtStartDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btnSetDate1"
android:layout_alignBottom="#+id/btnSetDate1"
android:layout_alignRight="#+id/btnShow"
android:layout_marginRight="21dp"
android:text="#string/blank"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<TextView
android:id="#+id/txtEndDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btnSetDate2"
android:layout_alignBottom="#+id/btnSetDate2"
android:layout_alignLeft="#+id/txtStartDate"
android:text="#string/blank"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<Button
android:id="#+id/btnClear"
style="?android:attr/buttonStyleSmall"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_alignBaseline="#+id/btnShow"
android:layout_alignBottom="#+id/btnShow"
android:layout_alignRight="#+id/btnSetDate2"
android:text="#string/clear"
android:textStyle="bold" />
</RelativeLayout>
and this the database adapter :
public ArrayList<ArrayList<Object>> ambilBarisDate(String startDate, String endDate){
ArrayList<ArrayList<Object>> dataArray = new
ArrayList<ArrayList<Object>>();
Cursor cur;
try{
cur = db.rawQuery("SELECT * FROM LOGSTAT WHERE DATE BETWEEN '"+startDate+"' AND '"+endDate+"'", null);//DIPERBAIKI
cur.moveToFirst();
if(!cur.isAfterLast()){
do{
ArrayList<Object> dataList = new ArrayList<Object>();
dataList.add(cur.getLong(0));
dataList.add(cur.getString(1));
dataList.add(cur.getString(2));
dataList.add(cur.getString(3));
dataArray.add(dataList);
}while (cur.moveToNext());
}
}catch (Exception e){
e.printStackTrace();
Log.e("DB ERROR", e.toString());
Toast.makeText(context, "Gagal Ambil Date Baris: "+e.toString(), Toast.LENGTH_SHORT).show();
}
return dataArray;
}
and this java program :
protected void updateTable(){
while (table4data.getChildCount() > 1){
table4data.removeViewAt(1);
}
String startDate = DateText1.getText().toString();
String endDate = DateText2.getText().toString();
ArrayList<ArrayList<Object>> data = logstatsdatabase.ambilBarisDate(startDate, endDate);
for (int posisi = 0; posisi < data.size(); posisi++){
TableRow tabelBaris = new TableRow(this);
ArrayList<Object> baris = data.get(posisi);
TextView idTxt = new TextView(this);
idTxt.setText(baris.get(0).toString());
tabelBaris.addView(idTxt);
TextView dateTxt = new TextView(this);
dateTxt.setText(baris.get(1).toString());
tabelBaris.addView(dateTxt);
TextView timeTxt = new TextView(this);
timeTxt.setText(baris.get(2).toString());
tabelBaris.addView(timeTxt);
TextView statusTxt = new TextView(this);
statusTxt.setText(baris.get(3).toString());
tabelBaris.addView(statusTxt);
table4data.addView(tabelBaris);
}
}
Let you have a set of number 1-100. And then you say between(30-60), it will return you 31,32....58,59. If you want to include 30 and 60 in your range, you should call between(29,61). I think this answers the 1st part of your query.
The second part, between(1,1) will always be null. So in your case, if yo wish to fetch some events for a particular day between 01/01/2014 and 01/01/2014:
You can ommit the use of between operator and use equals operator in this case
Or else store your date in a LONG foramt using Calendar.getInstance().getTimeInMilis(). Then you can use between operation more precisely to include the time range as well.

Multiple Radio group in a single Activity

I'm Developing an android app in which the Questionnaire activity contains radio buttons for every question.And I've designed the activity in Linear layout.So now i wanted to check whether the user as answered all Questions.The problem I'm facing is in the public void OnClick(View v)help me in modifying the java code.
<RadioGroup
android:id="#+id/Mquestion1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/MQ1_1_rb1" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/MQ1_1_rb2" />
<RadioButton
android:id="#+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/MQ1_1_rb3" />
</RadioGroup>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/MQ1_2_view"
android:textAppearance="?android:attr/textAppearanceSmall" />
<RadioGroup
android:id="#+id/Mquestion2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/MQ1_2_rb1" />
<RadioButton
android:id="#+id/radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/MQ1_2_rb2" />
<RadioButton
android:id="#+id/radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/MQ1_2_rb3" />
</RadioGroup>
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/MQ1_3_view"
android:textAppearance="?android:attr/textAppearanceSmall" />
<RadioGroup
android:id="#+id/Mquestion3"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/radioButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/MQ1_3_rb1" />
<RadioButton
android:id="#+id/radioButton8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/MQ1_3_rb2" />
<RadioButton
android:id="#+id/radioButton9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/MQ1_3_rb3" />
</RadioGroup>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="44dp" >
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:onClick="OnClickListener"
android:text="#string/MQ1_next" />
</RelativeLayout>
</LinearLayout>
Here is the java code
public class ManagerQuestionnaire1 extends Activity
{
Button next;
RadioGroup rg1;
RadioGroup rg2;
RadioGroup rg3;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manager_questionnaire1);
final RadioGroup rg1=(RadioGroup)findViewById(R.id.Mquestion1);
final RadioGroup rg2=(RadioGroup)findViewById(R.id.Mquestion2);
final RadioGroup rg3=(RadioGroup)findViewById(R.id.Mquestion3);
Button next=(Button)findViewById(R.id.button1);
next.setOnClickListener(new View.OnClickListener()
{
public void OnClick(View v) \\The error I'm getting here is REMOVE method'OnClick'
{
if((rg1.getCheckedRadioButtonId()!=R.id.radioButton1 || rg1.getCheckedRadioButtonId()!=R.id.radioButton2 || rg1.getCheckedRadioButtonId()!=R.id.radioButton3)||(rg2.getCheckedRadioButtonId()!=R.id.radioButton4 || rg2.getCheckedRadioButtonId()!=R.id.radioButton5 || rg2.getCheckedRadioButtonId()!=R.id.radioButton6)||(rg3.getCheckedRadioButtonId()!=R.id.radioButton7 || rg3.getCheckedRadioButtonId()!=R.id.radioButton8 || rg3.getCheckedRadioButtonId()!=R.id.radioButton9))
{
AlertDialog alert= new AlertDialog.Builder(ManagerQuestionnaire1.this).create();
alert.setTitle("Exception:Complete the Questions");
alert.setMessage("Please ensure all Questions are answered");
}
else
{
Intent intent = new Intent(ManagerQuestionnaire1.this, ManagerQuestionnaire2.class);
startActivity(intent);
}
}
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
}
});
}
It is always recommended to user radiobuttons inside a radioGroup
like this :
<RadioGroup
android:id="#+id/question1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="RadioButton" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
</RadioGroup>
<RadioGroup
android:id="#+id/question2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<RadioButton
android:id="#+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="RadioButton" />
<RadioButton
android:id="#+id/radio4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
<RadioButton
android:id="#+id/radio5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
</RadioGroup>
Now whenever you want to see which answer is selected corrosponding to which radio group you can do something like this :
RadioGroup question1 = (RadioGroup) findViewById(R.id.question1);
String answer1 = ((RadioButton) findViewById(question1.getCheckedRadioButtonId())).getText().toString();
Congrates ! you got the answer here
change your xml file here
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RadioGroup
android:id="#+id/radio"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MQ1_1_view"
android:textAppearance="?android:attr/textAppearanceSmall" />
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MQ1_1_rb1" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MQ1_1_rb2" />
<RadioButton
android:id="#+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MQ1_1_rb3" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MQ1_2_view"
android:textAppearance="?android:attr/textAppearanceSmall" />
<RadioButton
android:id="#+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MQ1_2_rb1" />
<RadioButton
android:id="#+id/radioButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MQ1_2_rb2" />
<RadioButton
android:id="#+id/radioButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MQ1_2_rb3" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MQ1_3_view"
android:textAppearance="?android:attr/textAppearanceSmall" />
<RadioButton
android:id="#+id/radioButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MQ1_3_rb1" />
<RadioButton
android:id="#+id/radioButton8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MQ1_3_rb2" />
<RadioButton
android:id="#+id/radioButton9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MQ1_3_rb3" />
</RadioGroup>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="44dp" >
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="MQ1_next" />
</RelativeLayout>
and in your java class define id for radiogroups.

Force close error in Android app debugging

I get no compilation errors or even significant warnings, yet when I run the program, it force closes and the log tells me there's some kind of error on a line. It succeeded once, but I don't know how or when.
onClick method:
public TextView equation;
public TextView answer11;
public TextView answer12;
public TextView or1;
public TextView or2;
public void SolveOnClick(View view){
EditText a = (EditText)findViewById(R.id.aValue);
EditText b = (EditText)findViewById(R.id.bValue); //screws up somewhere here
EditText c = (EditText)findViewById(R.id.cValue);
equation = (TextView)findViewById(R.id.equationText);
answer11 = (TextView)findViewById(R.id.answer1);
answer12 = (TextView)findViewById(R.id.answer2);
or1 = (TextView)findViewById(R.id.or1);
or2 = (TextView)findViewById(R.id.or2);
double A = Double.parseDouble(a.getText().toString());
double B = Double.parseDouble(b.getText().toString());
double C = Double.parseDouble(c.getText().toString());
Formula(A,B,C);
}
My main graphical layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:onClick="SolveOnClick"
android:text="#string/solveButtonText" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="#string/quadFormText"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView4"
android:layout_marginTop="17dp"
android:layout_toLeftOf="#+id/textView4"
android:text="#string/A"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/aValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView4"
android:layout_alignTop="#+id/textView1"
android:layout_marginLeft="15dp"
android:ems="10"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/bValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/aValue"
android:layout_below="#+id/aValue"
android:layout_marginTop="14dp"
android:ems="10"
android:inputType="numberDecimal" />
<EditText
android:id="#+id/cValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/bValue"
android:layout_below="#+id/bValue"
android:layout_marginTop="14dp"
android:ems="10"
android:inputType="numberDecimal" />
<TextView
android:id="#+id/answer1Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="#string/equals1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/answer2Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/answer1Text"
android:layout_below="#+id/answer1Text"
android:layout_marginTop="88dp"
android:text="#string/equals2"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/answer1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/answer1Text"
android:layout_toRightOf="#+id/answer1Text"
android:text="#string/answer11"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/or1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/answer1Text"
android:layout_below="#+id/answer1Text"
android:layout_marginTop="25dp"
android:text="#string/or1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/answer2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/answer2Text"
android:layout_toRightOf="#+id/answer1Text"
android:text="#string/answer12"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/or2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/answer2Text"
android:layout_below="#+id/answer2Text"
android:layout_marginTop="24dp"
android:text="#string/or2"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/equationText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/cValue"
android:layout_marginTop="14dp"
android:text="#string/equation"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/cValue"
android:layout_toLeftOf="#+id/textView4"
android:text="#string/C"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/bValue"
android:layout_alignLeft="#+id/textView3"
android:text="#string/B"
android:textAppearance="?android:attr/textAppearanceLarge" />
onCreate method:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
One of the logs when the program fails:
04-26 20:39:33.174: E/AndroidRuntime(16092): Caused by: java.lang.ClassCastException: android.widget.TextView
04-26 20:39:33.174: E/AndroidRuntime(16092): at calc.ulater.QuadCalcActivity.SolveOnClick(QuadCalcActivity.java:31)
I believe this has something to do with the decimal text boxes. The program worked well before I made them only take decimal numbers, but it also worked a time or two after. I'd rather not show the rest of my code, unless you guys are sure the problem lies there.
If this is the line that's screwing up:
EditText b = (EditText)findViewById(R.id.bValue);
Then its saying you're trying to cast a TextView to an EditText.
What's line 31? That one?
EditText a = (EditText)findViewById(R.id.aValue);
EditText b = (EditText)findViewById(R.id.bValue); //screws up somewhere here
EditText c = (EditText)findViewById(R.id.cValue);
equation = (TextView)findViewById(R.id.equationText);
answer11 = (TextView)findViewById(R.id.answer1);
answer12 = (TextView)findViewById(R.id.answer2);
or1 = (TextView)findViewById(R.id.or1);
or2 = (TextView)findViewById(R.id.or2);
all the initialization must be done in onCreate() and not in the constructor
Updated::
EditText a = (EditText)view.findViewById(R.id.aValue);
EditText b = (EditText)view.findViewById(R.id.bValue); //screws up somewhere here
EditText c = (EditText)view.findViewById(R.id.cValue);
equation = (TextView)view.findViewById(R.id.equationText);
answer11 = (TextView)view.findViewById(R.id.answer1);
answer12 = (TextView)view.findViewById(R.id.answer2);
or1 = (TextView)view.findViewById(R.id.or1);
or2 = (TextView)view.findViewById(R.id.or2);
if view is your parent view

Categories