Cannot be resolved or it's not a field - java

I am getting this error at lines "** cannot be resolved or it's not a field".
mainactivity.java
Integer num1=Integer.parseInt(number1text.getText().toString());
num2=Integer.parseInt(number2text.getText().toString()),
num3=Integer.parseInt(number3text.getText().toString());
Integer del= num2*num2+4*num1*num3 ;
Integer x1=-num2-del/2*num1 ;
Integer x2=-num2+del/2*num1 ;
TextView delta=(TextView)findViewById(R.id.deltatxt);
TextView x1=(TextView)findViewById(R.id.x1txt);
TextView x2=(TextView)findViewById(R.id.x2txt);
delta.setText("Delta:"+del.toString());
x1.setText("X1:"+x1.toString());
x2.setText("X2:"+x2.toString());
fragment.xml:
<TextView
android:id="#+id/num1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Numarul2(b)" />
<EditText
android:id="#+id/num1text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberSigned" />
<TextView
android:id="#+id/num2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Numarul3(c)" />
<EditText
android:id="#+id/num2text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberSigned" />
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/calc"
android:OnClick="calculate"/>
So I have added that ids but they are not recognised in maina-ctivity. Thanks in advance!

This is written bad:
android:OnClick="calculate" />
should be:
android:onClick="calculate" />
R won't compile when you misspell something.

Related

getText() returns empty string (android studio)

I have a little question here about android development.
I already searched on internet, but i still can't find the solution.
I want to getText() from a view, but it just returns an empty ("") string.
Here's my .java code:
public void kotakCentang(View view)
{
RelativeLayout vwParentRow = (RelativeLayout)view.getParent();
TextView id_test = (TextView)vwParentRow.getChildAt(0);
TextView nama_test = (TextView)vwParentRow.getChildAt(1);
tugas_status = (TextView)vwParentRow.getChildAt(5);
tanggalKasih = (EditText)vwParentRow.getChildAt(6);
tanggalKumpul = (EditText)vwParentRow.getChildAt(8);
waktuKasih = (EditText)vwParentRow.getChildAt(7);
waktuKumpul = (EditText)vwParentRow.getChildAt(9);
tugasKompleksitas = (EditText)vwParentRow.getChildAt(10);
Tugas tugas = new Tugas();
TugasRepo tugass = new TugasRepo(this);
tugas.nama = nama_test.getText().toString();
tugas.waktuDikasih = waktuKasih.getText().toString();
tugas.waktuDikumpul = waktuKumpul.getText().toString();
tugas.tanggalDikasih = tanggalKasih.getText().toString();
tugas.tanggalDikumpul = tanggalKumpul.getText().toString();
tugas.kompleksitas=Integer.parseInt(tugasKompleksitas.getText().toString());
tugasKompleksitas.getText().toString();
}
The getText() which returns empty string is just the tugas.waktuDikasih, and tugas.waktuDikumpul .
Thank you for your help.
----- UPDATE XML ----
here's my xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tugas_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/judul_tugas"
android:visibility="gone"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="judul tugas"
android:id="#+id/tugas_judul"
android:textSize="20dp"
android:layout_marginStart="60dp"
android:layout_marginTop="17dp"
android:layout_below="#+id/tugas_id"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/status_kerja"
android:checked="false"
android:textSize="10dp"
android:layout_alignTop="#+id/tugas_judul"
android:layout_toStartOf="#+id/button"
android:onClick="kotakCentang"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorPrimaryDark"
android:layout_below="#+id/status_kerja"
android:layout_alignParentStart="true"
android:layout_marginTop="19dp"
android:id="#+id/textView2">
</View>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Detil"
android:id="#+id/button"
android:layout_alignParentEnd="true"
android:textSize="10sp"
android:layout_marginEnd="16dp"
android:layout_alignTop="#+id/status_kerja"
android:layout_above="#+id/textView2"
android:onClick="detilTugas"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="status"
android:id="#+id/status_tugas"
android:layout_alignBottom="#+id/tugas_judul"
android:layout_toEndOf="#+id/tugas_id" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="date"
android:ems="10"
android:id="#+id/tanggal_diberikan"
android:text="Tanggal Diberikan"
android:textColor="#000000"
android:visibility="gone"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="time"
android:ems="10"
android:id="#+id/waktu_diberikan"
android:layout_below="#+id/tanggal_diberikan"
android:layout_alignStart="#+id/tanggal_diberikan"
android:layout_alignEnd="#+id/tanggal_diberikan"
android:text="Waktu diberikan"
android:textColor="#000000"
android:visibility="gone"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="date"
android:ems="10"
android:id="#+id/tanggal_kumpul"
android:text="Tanggal dikumpulkan"
android:layout_below="#+id/tugas_judul"
android:layout_alignStart="#+id/waktu_diberikan"
android:layout_alignEnd="#+id/waktu_diberikan"
android:textColor="#000000"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="time"
android:ems="10"
android:id="#+id/waktu_dikumpulkan"
android:layout_below="#+id/tanggal_kumpul"
android:layout_alignStart="#+id/tanggal_kumpul"
android:layout_alignEnd="#+id/tanggal_kumpul"
android:text="Waktu dikumpulkan"
android:textColor="#000000"
android:visibility="gone"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="time"
android:ems="10"
android:id="#+id/isi_kompleksitas"
android:text="Kompleksitas"
android:textColor="#000000"
android:editable="false"
android:visibility="gone"/>
<!--<TextView-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="id_tugas"-->
<!--android:id="#+id/tugas_id"-->
<!--android:layout_alignBottom="#+id/status_kerja"-->
<!--android:layout_alignEnd="#+id/tugas_judul"-->
<!--android:layout_marginEnd="117dp" />-->
</RelativeLayout>

Crash's trying to find view

I have a slight problem that I have been stuck on. I have narrowed it down and it seems that my application is crashing on create when i am trying to find a text view. I do not understand why, maybe i have been looking at this to long and dont see the obvious.
Any help would be appreciated.
Thanks.
public class FinalActivity extends Activity {
EditText fullName;
EditText serialNumber;
EditText numberOfTicket;
EditText fullCost;
EditText discount;
EditText totalCostOfTickets;
EditText dinnerYesNo;
EditText numberForDinner;
EditText dinnerCost;
EditText vatTv;
EditText totalCost;
SharedPreferences thePrefs;// Shared preferences variable
SharedPreferences.Editor editor;// New editor to add to shared preferences
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_final);
thePrefs = getSharedPreferences(LoginActivity.FIRSTTIMEPREFS, 0);
editor = thePrefs.edit();
fullName = (EditText)findViewById(R.id.personsNameTv);
/*serialNumber = (EditText)findViewById(R.id.serialNumberTv);
numberOfTicket = (EditText)findViewById(R.id.numberOfTicketTv);
fullCost = (EditText)findViewById(R.id.fullCostBeforeTv);
discount = (EditText)findViewById(R.id.discountAmountTV);
totalCostOfTickets = (EditText)findViewById(R.id.totalTicketCostEt);
dinnerYesNo = (EditText)findViewById(R.id.dinnerAvailedTv);
numberForDinner = (EditText)findViewById(R.id.numberForDinnerTv);
dinnerCost = (EditText)findViewById(R.id.dinnerCostTv);
vatTv = (EditText)findViewById(R.id.vatTv);
totalCost = (EditText)findViewById(R.id.totalCostTv);*/
//fullName.setText (thePrefs.getString(PersonalInfoActivity.FULLNAME, ""));
}
}
<LinearLayout 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:background="#404040"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:id="#+id/mainTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:text="#string/ticketSummary"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/promptTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:text="#string/confirmCorrectDetails"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<ScrollView
android:id="#+id/containerLayout"
android:layout_width="200dp"
android:layout_height="300dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dp"
android:background="#drawable/redborder" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:paddingBottom="30dp"
android:paddingLeft="15dp"
android:paddingRight="10dp"
android:paddingTop="15dp" >
<TextView
android:id="#+id/personsNameTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/fullName"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/serialNumberTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/personsNameTv"
android:layout_marginTop="10dp"
android:text="#string/serialNumber"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/numberOfTicketTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/serialNumberTv"
android:layout_marginTop="10dp"
android:text="#string/numberOfTickets"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/fullCostBeforeTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/numberOfTicketTv"
android:layout_marginTop="10dp"
android:text="#string/fullCostBeforeDiscount"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/discountAmountTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/fullCostBeforeTv"
android:layout_marginTop="10dp"
android:text="#string/amountOfDisount"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/totalTicketCostEt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/discountAmountTV"
android:layout_marginTop="10dp"
android:text="#string/totalTicketCost"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/dinnerAvailedTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/totalTicketCostEt"
android:layout_marginTop="10dp"
android:text="#string/dinnerAvailed"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/numberForDinnerTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/dinnerAvailedTv"
android:layout_marginTop="10dp"
android:text="#string/numberForDinner"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/dinnerCostTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/numberForDinnerTv"
android:layout_marginTop="10dp"
android:text="#string/dinnerCost"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/vatTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/dinnerCostTv"
android:layout_marginTop="10dp"
android:text="#string/vat"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/totalCostTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/vatTv"
android:layout_marginTop="10dp"
android:text="#string/totalCost"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:textSize="15sp" />
</RelativeLayout>
</ScrollView>
<Button
android:id="#+id/confirmBtn"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="#+id/containerLayout"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dp"
android:background="#drawable/buttonshape"
android:onClick="confirmTicketOptions"
android:text="#string/confirm"
android:textStyle="bold" />
</LinearLayout>
There are two possible things possible. Either you want all Textviews or all EditText.
If you want all Textview then replace all your EditTextin java file with Textview.
Textview fullName;
fullName = (Textview )findViewById(R.id.personsNameTv);
If you want all EditText then replace all Textview in xml with EditText.
<EditText
android:id="#+id/personsNameTv"
.../>
In XML you are using Text-view and in java file you define it Edit-text
<TextView
android:id="#+id/personsNameTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/fullName"
android:textColor="#FFFFFF"
android:textSize="15sp" />
Replace with
<EditText
android:id="#+id/personsNameTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/fullName"
android:textColor="#FFFFFF"
android:textSize="15sp" />

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);

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