Wrote a simple application calculating a function depending on the entered value (the so-called piecewise-defined function).

The problem is that when compiling and running on a device (a tablet running android 4.4, the project, respectively, was also created for Android 4.4), the described error gets out with reference to xml which describes the application form. The application was rewritten from scratch, re-created the project with a copy-paste content, cleaned and rebuilt, does not help.

The code is attached: xml:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" 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="com.example.lab02a2andr_.MainActivity"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentTop="true" android:layout_alignParentStart="true" android:weightSum="1"> <TextView android:text="введите Х:" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textView3" android:layout_weight="0.03" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPersonName" android:text="0.0" android:ems="10" android:id="@+id/editText" /> <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content"> <RadioButton android:text="sin(x^2-3) x<=-1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/radioButton1" /> <RadioButton android:text="(x+4)*x^2-3 -1<x<1" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/radioButton3" /> <RadioButton android:text="cos(sqrt(x)) x>=1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/radioButton2" /> </RadioGroup> <CheckBox android:text="Удвоить" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/checkBox" android:layout_weight="0.03" /> <Button android:text="Посчитать" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/button" android:layout_weight="0.03" /> <TextView android:text="Ответ:" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.03" android:id="@+id/textView2" /> </LinearLayout> 

mainActivity:

 package com.example.lab02a2andr_; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.RadioButton; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private CheckBox cb; private TextView tv2; private RadioButton rb1,rb2,rb3; private EditText et1; private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); cb=(CheckBox) findViewById(R.id.checkBox); tv2=(TextView) findViewById(R.id.textView2); rb1=(RadioButton) findViewById(R.id.radioButton1); rb2=(RadioButton) findViewById(R.id.radioButton2); rb3=(RadioButton) findViewById(R.id.radioButton3); et1=(EditText) findViewById(R.id.editText); button = (Button) findViewById(R.id.button); et1.setOnFocusChangeListener(new View.OnFocusChangeListener(){ public void onFocusChange(View v, boolean hasFocus){ if(!hasFocus){ Calculate(); } } }); rb1.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ Calculate(); } }); rb2.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ Calculate(); } }); rb3.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ Calculate(); } }); cb.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ Calculate(); } }); button.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ Calculate(); } }); } public void Calculate(){ String s1; double x,y=0; int var; s1=et1.getText().toString(); x=Double.parseDouble(s1); if(rb1.isChecked()==true){ var=1; }else if(rb2.isChecked()==true){ var=2; }else if(rb3.isChecked()==true){ var=3; }else{ if(x<=-1) { var = 1; rb1.setChecked(true); }else if(x>=1){ var=3; rb3.setChecked(true); }else{ var=2; rb2.setChecked(true); } } switch (var){ case 1: y=Math.sin(x*x-3); break; case 2: y=(x+4)*x*x-3; break; case 3: y=Math.cos(Math.sqrt(x)); break; } if(cb.isChecked()==true){ y*=2; } s1="Ответ: "+Double.toString(y); } } 

    2 answers 2

    Most likely problems are caused by < and > characters < Try writing text from your RadioButton in a string file, and in the RadioButton specify a link to @string

      You forgot to specify the root tag in the markup this line

       xmlns:android="http://schemas.android.com/apk/res/android" 

      This is the android namespace.

      • I did not forget, he is there, he just incorrectly formatted the text in a block of code, now he fixed it - Alexander