Hi i'm a beginner android developer trying to make a basic calculator app
Here is my layout code from the content_main.xml folder:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main" tools:context=".MainActivity">
<TextView
android:id="#+id/displayer"
android:text="Hello World!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="113dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:id="#+id/minus"
android:onClick="subtract"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/displayer"
android:layout_toStartOf="#+id/displayer"
android:layout_marginRight="39dp"
android:layout_marginEnd="39dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="add"
android:text="+"
android:id="#+id/plus"
android:layout_below="#+id/minus"
android:layout_alignLeft="#+id/minus"
android:layout_alignStart="#+id/minus" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:onClick="setNumber"
android:tag="1"
android:id="#+id/one"
android:layout_above="#+id/plus"
android:layout_toRightOf="#+id/plus"
android:layout_toEndOf="#+id/plus" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="setNumber"
android:text="4"
android:id="#+id/four"
android:tag="4"
android:layout_alignTop="#+id/plus"
android:layout_toRightOf="#+id/plus"
android:layout_toEndOf="#+id/plus" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="setNumber"
android:text="2"
android:id="#+id/two"
android:tag="2"
android:layout_above="#+id/four"
android:layout_toRightOf="#+id/one"
android:layout_toEndOf="#+id/one" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="setNumber"
android:text="3"
android:tag="3"
android:id="#+id/three"
android:layout_above="#+id/five"
android:layout_toRightOf="#+id/two"
android:layout_toEndOf="#+id/two" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="setNumber"
android:text="5"
android:tag="5"
android:id="#+id/five"
android:layout_below="#+id/two"
android:layout_alignLeft="#+id/two"
android:layout_alignStart="#+id/two" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="setNumber"
android:text="6"
android:tag="6"
android:id="#+id/six"
android:layout_above="#+id/nine"
android:layout_toRightOf="#+id/five"
android:layout_toEndOf="#+id/five" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="setNumber"
android:text="7"
android:tag="7"
android:id="#+id/seven"
android:layout_below="#+id/plus"
android:layout_toRightOf="#+id/plus"
android:layout_toEndOf="#+id/plus" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="setNumber"
android:text="8"
android:tag="8"
android:id="#+id/eight"
android:layout_below="#+id/five"
android:layout_alignLeft="#+id/five"
android:layout_alignStart="#+id/five" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="setNumber"
android:text="9"
android:tag="9"
android:id="#+id/nine"
android:layout_below="#+id/five"
android:layout_alignLeft="#+id/six"
android:layout_alignStart="#+id/six" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="="
android:id="#+id/equal"
android:onClick="equal"
android:layout_below="#+id/plus"
android:layout_alignLeft="#+id/plus"
android:layout_alignStart="#+id/plus" />
</RelativeLayout>
Here is my Activity from the MainActivity folder:
package com.example.android.calculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
private int firstNumber = -1;
private int secondNumber = -1;
private int operation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private void equal(View view){
TextView displayerTextView=(TextView)
findViewById(R.id.displayer);
if (operation == 0) {
displayerTextView.setText(firstNumber+secondNumber);
}else if (operation == 1){
displayerTextView.setText(firstNumber-secondNumber);
}else{
displayerTextView.setText("error");
}
}
private void add(View view){
operation = 0;
}
private void subtract(View view){
operation = 1;
}
private void setNumber(View view){
if (firstNumber == -1) {
firstNumber = Integer.parseInt((String)view.getTag());
}else{
secondNumber = Integer.parseInt((String)view.getTag());
}
}
}
When I click on the 3 button in my calculator app this error appears here is the error:
java.lang.IllegalStateException: Could not find a method setNumber(View) in the activity class com.example.android.calculator.MainActivity for onClick handler on view class android.widget.Button with id 'three'
Because you method is private. Change on public.
More you can find here http://developer.android.com/reference/android/widget/Button.html
It should be
public void setNumber(View view){
For the issue mentioned, change
private void setNumber(View view){
if (firstNumber == -1) {
firstNumber = Integer.parseInt((String)view.getTag());
}else{
secondNumber = Integer.parseInt((String)view.getTag());
}
}
to
public void setNumber(View view){
if (firstNumber == -1) {
firstNumber = Integer.parseInt((String)view.getTag());
}else{
secondNumber = Integer.parseInt((String)view.getTag());
}
}
To avoid similar errors in your code with add and subtract, change those functions also from private to public.
Related
java
there is problem in total.I have already tried try and catch.It always
show 0 at t17.looks like it does not set the total value.
It does not total the value given by total=i1+i2+i3+i4+i5;
t17.setText(String.valueOf(total));it always show 0 at t17
package com.example.myproject1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
public class Bill extends AppCompatActivity {
TextView t,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15,t16,t17;
Button b,b1;
int total;
int i1,i2,i3,i4,i5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bill);
t=findViewById(R.id.textView);
t1=findViewById(R.id.textView5);
t2=findViewById(R.id.textView10);
t3=findViewById(R.id.textView15);
t4=findViewById(R.id.textView6);
t5=findViewById(R.id.textView11);
t6=findViewById(R.id.textView16);
t7=findViewById(R.id.textView7);
t8=findViewById(R.id.textView12);
t9=findViewById(R.id.textView17);
t10=findViewById(R.id.textView8);
t11=findViewById(R.id.textView13);
t12=findViewById(R.id.textView18);
t13=findViewById(R.id.textView9);
t14=findViewById(R.id.textView14);
t15=findViewById(R.id.textView19);
t16=findViewById(R.id.textView20);
t17=findViewById(R.id.textView21);
b=findViewById(R.id.button22);
b1=findViewById(R.id.button23);
Intent obj2 = getIntent();
String r = obj2.getStringExtra("k");
t.setText(r);
String res = obj2.getStringExtra("key");
String res1 = obj2.getStringExtra("key2");
String res2 = obj2.getStringExtra("key3");
String res3 = obj2.getStringExtra("key4");
String res4 = obj2.getStringExtra("key5");
String res5 = obj2.getStringExtra("key6");
String res6 = obj2.getStringExtra("key7");
String res7 = obj2.getStringExtra("key8");
String res8 = obj2.getStringExtra("key9");
String res9 = obj2.getStringExtra("key10");
String res10 = obj2.getStringExtra("key11");
String res11 = obj2.getStringExtra("key12");
String res12 = obj2.getStringExtra("key13");
String res13 = obj2.getStringExtra("key14");
String res14 = obj2.getStringExtra("key15");
t1.setText(res);
t2.setText(res1);
t3.setText(res2);
t4.setText(res3);
t5.setText(res4);
t6.setText(res5);
t7.setText(res6);
t8.setText(res7);
t9.setText(res8);
t10.setText(res9);
t11.setText(res10);
t12.setText(res11);
t13.setText(res12);
t14.setText(res13);
t15.setText(res14);
try{
if (res2 != null) {
i1 = Integer.parseInt(res2);
} else {
i1 = 0;
}
if (res5 != null) {
i2 = Integer.parseInt(res5);
} else {
i2 = 0;
}
if (res8 != null) {
i3 = Integer.parseInt(res8);
} else {
i3 = 0;
}
if (res11 != null) {
i4 = Integer.parseInt(res11);
} else {
i4 = 0;
}
if (res14 != null) {
i5 = Integer.parseInt(res14);
} else {
i5 = 0;
}
}catch (NumberFormatException e){
}
t16.setText("Total Price:");
total=i1+i2+i3+i4+i5;
t17.setText(String.valueOf(total));
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent obj = new Intent(Bill.this, Menu.class);
startActivity(obj);
}
});
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent obj1 = new Intent(Bill.this, MainActivity.class);
startActivity(obj1);
}
});
}
}
XML
the xml of my code is given as below though it has nothing to do with
my coding part.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/bill4"
tools:context=".Bill">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="224dp"
android:layout_marginTop="5dp"
android:background="#92278F"
android:gravity="center"
android:text="user"
android:textColor="#0066FF"
android:textSize="24sp"
android:textStyle="bold"
app:fontFamily="#font/merienda_one"
tools:layout_editor_absoluteX="161dp"
tools:layout_editor_absoluteY="64dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="#CD81DB"
android:fontFamily="#font/joti_one"
android:text="Bill"
android:textColor="#111111"
android:textSize="#android:dimen/app_icon_size"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:layout_marginTop="60dp"
android:background="#92278F"
android:gravity="center"
android:paddingLeft="25dp"
android:paddingRight="25dp"
android:text="Item"
android:textColor="#0066FF"
android:textSize="24dp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="13dp"
android:layout_marginTop="60dp"
android:background="#92278F"
android:gravity="center"
android:paddingLeft="25dp"
android:paddingRight="25dp"
android:text="Quantity"
android:textColor="#0066FF"
android:textSize="24dp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="60dp"
android:background="#92278F"
android:gravity="center"
android:paddingLeft="25dp"
android:paddingRight="25dp"
android:text="Price"
android:textColor="#0066FF"
android:textSize="24dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:orientation="vertical">
<TextView
android:id="#+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:orientation="vertical">
<TextView
android:id="#+id/textView10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView12"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView13"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView14"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="120dp"
android:orientation="vertical">
<TextView
android:id="#+id/textView15"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView16"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView17"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView18"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView19"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/textView20"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginTop="20dp"
android:textColor="#111111"
android:text=""
android:textSize="14dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView21"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="200dp"
android:layout_marginTop="20dp"
android:textColor="#111111"
android:textSize="14dp"
android:textStyle="bold"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginTop="20dp"
android:background="#92278F"
android:textColor="#0066FF"
android:textStyle="bold"
app:fontFamily="#font/merienda_one"
android:text="Cancel" />
<Button
android:id="#+id/button23"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="165dp"
android:layout_marginTop="20dp"
android:background="#92278F"
android:textColor="#0066FF"
android:textStyle="bold"
app:fontFamily="#font/merienda_one"
android:text="Order" />
</LinearLayout>
</LinearLayout>
I have used Sharedpreferences instead of intent however it is possible
from both.
Data recieving class
public class class_name extends AppCompatActivity {
SharedPreferences sharedPref;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bill);
sharedPref=this.getSharedPreferences("calis",Context.MODE_PRIVATE);
String res = sharedPref.getString("k1",null);
String res1 = sharedPref.getString("k2",null);
String res2 = sharedPref.getString("k3",null);
Integer res15 = sharedPref.getInt("k4",0);
Integer vres15 = sharedPref.getInt("k5",0);
int total2 = res15+vres15;
t17.setText(String.valueOf(total2));
}
}
Data sending class
public class class_name2 extends Fragment {
SharedPreferences sp;
sp=this.getActivity().getSharedPreferences("calis", Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sp.edit();
editor.putString("k1",value1);
editor.putInt("k4", value2);
editor.commit();
}
This question already has answers here:
What is a stack trace, and how can I use it to debug my application errors?
(7 answers)
Closed 4 years ago.
This is the code MainActivity.java file. I think there should be no mistake in this code. But it shows the message
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference.
I don't know why it is showing up there? Please help me to figure out.
package com.example.android.practiceset2;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.*;
import android.view.*;`
public class MainActivity extends AppCompatActivity {
int score=0,wickets=0;
String value=null,number=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Button b1=(Button)findViewById(R.id.b_i1);//b_i1 stands for increase in 1 run taken by team B and similarly other names are to the ids of different button.
Button b2=(Button)findViewById(R.id.b_i2);
Button b3=(Button)findViewById(R.id.b_i3);
Button b4=(Button)findViewById(R.id.b_i4);
Button b6=(Button)findViewById(R.id.b_i6);
Button bw=(Button)findViewById(R.id.b_w);
Button bwide=(Button)findViewById(R.id.b_wide);
Button a1=(Button)findViewById(R.id.a_i1);
Button a2=(Button)findViewById(R.id.a_i2);
Button a3=(Button)findViewById(R.id.a_i3);
Button a4=(Button)findViewById(R.id.a_i4);
Button a6=(Button)findViewById(R.id.a_i6);
Button aw=(Button)findViewById(R.id.a_w);
Button awide=(Button)findViewById(R.id.a_wide);
TextView ta= (TextView)findViewById(R.id.a);
TextView tb= (TextView)findViewById(R.id.b);
public void aorb(View view{
if(getResources().getResourceEntryName((view.getId())).startsWith("a"))
{ value="a";}
else{value="b";}
number=getResources().getResourceEntryName((view.getId())).substring(2);
score();}
public void a_enabled(Boolean b)
{
a1.setEnabled(b);
a2.setEnabled(b);
a3.setEnabled(b);
a4.setEnabled(b);
a6.setEnabled(b);
aw.setEnabled(b);
awide.setEnabled(b);
}
public void b_enabled(Boolean b)
{
b1.setEnabled(b);
b2.setEnabled(b);
b3.setEnabled(b);
b4.setEnabled(b);
b6.setEnabled(b);
bw.setEnabled(b);
bwide.setEnabled(b);
}
public void display(String s){
if(value.equals("a"))
{
ta.setText(s);
b_enabled(false);
a_enabled(true);
}
else {
tb.setText(s);
a_enabled(false);
b_enabled(true);
}
}
public void score(){
int i=0;
if(number.equals("i1"))
i=1;
if(number.equals("i2"))
i=2;
if(number.equals("i3"))
i=3;
if(number.equals("i4"))
i=4;
if(number.equals("i6"))
i=6;
if(number.equals("wide"))
i=1;
score+=i;
if(number.equals("w"))
wickets+=1;
if(wickets==10)//if a team loses all its 10 wickets , the score is finalised and next team is invited for play
{
display(score+"/"+wickets);
if(value.equals("a"))
{
value="b";
}
else{value="a";}
score=0;
wickets=0;
display(score+"/"+wickets);
}
display(score+"/"+wickets);
}
public void reset(View view){
ta.setText("0/0");
tb.setText("0/0");
a_enabled(true);
b_enabled(true);
}}
This is the code for my content_main.xml file.
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#FFFFFF">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Team A"
android:textAlignment="center"
android:fontFamily="sans-serif-medium"
android:textSize="20sp"
android:textColor="#616161"
android:layout_margin="16dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:text="0/0"
android:textAlignment="center"
android:fontFamily="sans-serif-light"
android:textSize="56dp"
android:textColor="#000000"
android:id="#+id/a"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:onClick="aorb"
android:id="#+id/a_i1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:onClick="aorb"
android:id="#+id/a_i3"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:onClick="aorb"
android:id="#+id/a_i6"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:onClick="aorb"
android:id="#+id/a_i2"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:onClick="aorb"
android:id="#+id/a_i4"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Out"
android:onClick="aorb"
android:id="#+id/a_w"
/>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:text="Wide"
android:onClick="aorb"
android:id="#+id/a_wide"/>
</RelativeLayout>
</LinearLayout>
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"></View>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Team B"
android:textAlignment="center"
android:fontFamily="sans-serif-medium"
android:textSize="20sp"
android:textColor="#616161"
android:layout_margin="16dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:text="0/0"
android:textAlignment="center"
android:fontFamily="sans-serif-light"
android:textSize="56dp"
android:textColor="#000000"
android:id="#+id/b"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:onClick="aorb"
android:id="#+id/b_i1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:onClick="aorb"
android:id="#+id/b_i3"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:onClick="aorb"
android:id="#+id/b_i6"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:onClick="aorb"
android:id="#+id/b_i2"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:onClick="aorb"
android:id="#+id/b_i4"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Out"
android:onClick="aorb"
android:id="#+id/b_w"
/>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:text="Wide"
android:onClick="aorb"
android:id="#+id/b_wide"
/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:onClick="reset"
android:layout_marginBottom="32dp"
/>
</RelativeLayout>
Here is the image of it should look like it is the screenshot the preview
All these findViewById must be included in your onCreate() method. You can keep the declarations out of onCreate(). Your views do not exist before setContentView(R.layout.activity_main);
I mean before onCreate() you can have Button b1;
and after setContentView(R.layout.activity_main); in onCreate() you must have b1=(Button)findViewById(R.id.b_i1);
for every view.
I have made the corrections in your and now the app is not crashing. Try using the below code snippet
MainActivity
public class MainActivity extends AppCompatActivity {
int score = 0, wickets = 0;
String value = null, number = null;
Button b1;
Button b2;
Button b3;
Button b4;
Button b6;
Button bw;
Button bwide;
Button a1;
Button a2;
Button a3;
Button a4;
Button a6;
Button aw;
Button awide;
TextView ta;
TextView tb;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
b1 = (Button) findViewById(R.id.b_i1);//b_i1 stands for increase in 1 run taken by team B and similarly other names are to the ids of different button.
b2 = (Button) findViewById(R.id.b_i2);
b3 = (Button) findViewById(R.id.b_i3);
b4 = (Button) findViewById(R.id.b_i4);
b6 = (Button) findViewById(R.id.b_i6);
bw = (Button) findViewById(R.id.b_w);
bwide = (Button) findViewById(R.id.b_wide);
a1 = (Button) findViewById(R.id.a_i1);
a2 = (Button) findViewById(R.id.a_i2);
a3 = (Button) findViewById(R.id.a_i3);
a4 = (Button) findViewById(R.id.a_i4);
a6 = (Button) findViewById(R.id.a_i6);
aw = (Button) findViewById(R.id.a_w);
awide = (Button) findViewById(R.id.a_wide);
ta = (TextView) findViewById(R.id.a);
tb = (TextView) findViewById(R.id.b);
}
public void aorb(View view) {
if (getResources().getResourceEntryName((view.getId())).startsWith("a")) {
value = "a";
} else {
value = "b";
}
number = getResources().getResourceEntryName((view.getId())).substring(2);
score();
}
public void a_enabled(Boolean b) {
a1.setEnabled(b);
a2.setEnabled(b);
a3.setEnabled(b);
a4.setEnabled(b);
a6.setEnabled(b);
aw.setEnabled(b);
awide.setEnabled(b);
}
public void b_enabled(Boolean b) {
b1.setEnabled(b);
b2.setEnabled(b);
b3.setEnabled(b);
b4.setEnabled(b);
b6.setEnabled(b);
bw.setEnabled(b);
bwide.setEnabled(b);
}
public void display(String s) {
if (value.equals("a")) {
ta.setText(s);
b_enabled(false);
a_enabled(true);
} else {
tb.setText(s);
a_enabled(false);
b_enabled(true);
}
}
public void score() {
int i = 0;
if (number.equals("i1"))
i = 1;
if (number.equals("i2"))
i = 2;
if (number.equals("i3"))
i = 3;
if (number.equals("i4"))
i = 4;
if (number.equals("i6"))
i = 6;
if (number.equals("wide"))
i = 1;
score += i;
if (number.equals("w"))
wickets += 1;
if (wickets == 10)//if a team loses all its 10 wickets , the score is finalised and next team is invited for play
{
display(score + "/" + wickets);
if (value.equals("a")) {
value = "b";
} else {
value = "a";
}
score = 0;
wickets = 0;
display(score + "/" + wickets);
}
display(score + "/" + wickets);
}
public void reset(View view) {
ta.setText("0/0");
tb.setText("0/0");
a_enabled(true);
b_enabled(true);
}
}
content_main.xml
<?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="#FFFFFF"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:fontFamily="sans-serif-medium"
android:text="Team A"
android:textAlignment="center"
android:textColor="#616161"
android:textSize="20sp" />
<TextView
android:id="#+id/a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:fontFamily="sans-serif-light"
android:text="0/0"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="56dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="#+id/a_i1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="1" />
<Button
android:id="#+id/a_i3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="3" />
<Button
android:id="#+id/a_i6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="6" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="#+id/a_i2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="2" />
<Button
android:id="#+id/a_i4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="4" />
<Button
android:id="#+id/a_w"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="Out" />
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/a_wide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:onClick="aorb"
android:text="Wide"
android:textAlignment="center" />
</RelativeLayout>
</LinearLayout>
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"></View>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:fontFamily="sans-serif-medium"
android:text="Team B"
android:textAlignment="center"
android:textColor="#616161"
android:textSize="20sp" />
<TextView
android:id="#+id/b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:fontFamily="sans-serif-light"
android:text="0/0"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="56dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="#+id/b_i1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="1" />
<Button
android:id="#+id/b_i3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="3" />
<Button
android:id="#+id/b_i6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="6" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="#+id/b_i2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="2" />
<Button
android:id="#+id/b_i4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="4" />
<Button
android:id="#+id/b_w"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="Out" />
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/b_wide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:onClick="aorb"
android:text="Wide"
android:textAlignment="center" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="32dp"
android:onClick="reset"
android:text="Reset"
android:textAlignment="center" />
</RelativeLayout>
</RelativeLayout>
Hope this helps.
I'm a beginner in Android. Just started working on a Calculator app and don't know why the app keeps crashing all the time.
I have Two Activities... MainActivity and CalculatorActivity
MainActivity:
public class MainActivity extends AppCompatActivity
{
Button calcubtn;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
calcubtn = (Button) findViewById(R.id.calculatorbtn);
calcubtn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
Intent calculator = new Intent(MainActivity.this, CalculatorActivity.class);
startActivity(calculator);
}
});
}
}
MainActivity Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.rfkha.myfirstapp.MainActivity">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="13dp"
android:textSize="27sp"
android:textColor="#android:color/holo_red_light"
android:text="#string/functions_text" />
<Button
android:id="#+id/calculatorbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/textView"
android:layout_marginStart="43dp"
android:layout_marginTop="70dp"
android:text="#string/calcu_string"
/>
</RelativeLayout>
CalculatorActivity: (i.e child activity)
Here in the activity class, I've added implements View.OnclickListener . I found it in stackoverflow somewhere. I think the problem is from this thing. Also it required to used the keyword abstractto use it with the activity class, so i used that too.
public abstract class CalculatorActivity extends AppCompatActivity implements View.OnClickListener {
Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, zerobtn, dotbtn, equalbtn, minusbtn, sumbtn, divbtn, mulbtn;
double num1, num2;
boolean sum, minus, div, mul;
EditText textarea= (EditText) findViewById(R.id.textarea);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(this);
(((Same Code for buttons upto 9 and SUM, MINUS, DIVISION and MULtiplication Buttons)))
}
#Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.btn1: {
textarea.setText(textarea.getText()+"1");
break;
}
case R.id.btn2: {
textarea.setText(textarea.getText()+"2");
break;
}
case R.id.btn3:
((((( And So on... Upto Button 9 ))))))
case R.id.zerobtn: {
textarea.setText(textarea.getText()+"0");
break;
}
case R.id.dotbtn: {
textarea.setText(textarea.getText()+".");
break;
}
case R.id.sumbtn: {
num1=Double.parseDouble(textarea.getText().toString());
sum=true;
textarea.setText(null);
break;
}
(((((Likewise for Minus, Division and Multiplication buttons))))))))))
case R.id.equalbtn:
{
if (num1!=0)
{
num2=Double.parseDouble(textarea.getText().toString());
if (sum) {
textarea.setText(num1+num2+"");
sum=false;
}
else if(mul) {
textarea.setText(num1*num2+"");
mul=false;
}
else if(div) {
textarea.setText(num1/num2+"");
div=false;
}
else if(minus) {
textarea.setText(num1-num2+"");
minus=false;
}
}
else{
Toast.makeText(getApplicationContext(), "First select a number!!", Toast.LENGTH_SHORT);
}
break;
}
}
}
}
Calculator Activity Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1"
tools:context="com.example.rfkha.myfirstapp.CalculatorActivity">
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="13dp"
android:layout_marginTop="8dp"
android:gravity="center_horizontal"
android:text="#string/calculatortxt"
android:textSize="25sp" />
<EditText
android:id="#+id/textarea"
android:layout_width="351dp"
android:layout_height="154dp"
android:layout_marginStart="16dp"
android:ems="10"
android:hint="#string/resultsString"
android:textSize="33sp"
android:textAlignment="textEnd"
android:padding="11dp"
android:inputType="textPersonName" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:weightSum="1">
<Button
android:id="#+id/clearbt"
android:layout_width="293dp"
android:layout_height="51dp"
android:text="#string/cetxt"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="#+id/btn7"
android:layout_width="68dp"
android:layout_height="51dp"
android:text="7"
android:textSize="24sp" />
<Button
android:id="#+id/btn8"
android:layout_width="68dp"
android:layout_height="51dp"
android:text="8"
android:textSize="24sp" />
<Button
android:id="#+id/btn9"
android:layout_width="68dp"
android:layout_height="51dp"
android:text="9"
android:textSize="24sp" />
<Button
android:id="#+id/mulbtn"
android:layout_width="68dp"
android:layout_height="51dp"
android:layout_marginStart="27dp"
android:text="x"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="#+id/btn4"
android:layout_width="68dp"
android:layout_height="51dp"
android:text="4"
android:textSize="24sp" />
<Button
android:id="#+id/btn5"
android:layout_width="68dp"
android:layout_height="51dp"
android:text="5"
android:textSize="24sp" />
<Button
android:id="#+id/btn6"
android:layout_width="68dp"
android:layout_height="51dp"
android:text="6"
android:textSize="24sp" />
<Button
android:id="#+id/minusbtn"
android:layout_width="68dp"
android:layout_height="51dp"
android:layout_marginStart="27dp"
android:text="-"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="#+id/btn1"
android:layout_width="68dp"
android:layout_height="51dp"
android:text="1"
android:textSize="24sp" />
<Button
android:id="#+id/btn2"
android:layout_width="68dp"
android:layout_height="51dp"
android:text="2"
android:textSize="24sp" />
<Button
android:id="#+id/btn3"
android:layout_width="68dp"
android:layout_height="51dp"
android:text="3"
android:textSize="24sp" />
<Button
android:id="#+id/sumbtn"
android:layout_width="68dp"
android:layout_height="51dp"
android:layout_marginStart="27dp"
android:text="+"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="#+id/dotbtn"
android:layout_width="68dp"
android:layout_height="51dp"
android:text="."
android:textSize="24sp" />
<Button
android:id="#+id/zerobtn"
android:layout_width="68dp"
android:layout_height="51dp"
android:text="0"
android:textSize="24sp" />
<Button
android:id="#+id/equalbtn"
android:layout_width="68dp"
android:layout_height="51dp"
android:text="="
android:textSize="24sp" />
<Button
android:id="#+id/divbtn"
android:layout_width="68dp"
android:layout_height="51dp"
android:layout_marginStart="27dp"
android:text="/"
android:textSize="24sp" />
</LinearLayout>
</LinearLayout>
Logcat: (Errors)
Link to Logcat: https://pastebin.com/hbwZVUZg
Delete the abstract keyword. then the line textarea= (EditText) findViewById(R.id.textarea); change for EditText textarea; and last put this on the onCreate() textarea = (EditText)findViewById(R.id.textarea);
Remember that you cannot instantiate an abstract class. here
And check how to debug here
public class CalculatorActivity extends AppCompatActivity implements View.OnClickListener {
Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, zerobtn, dotbtn, equalbtn, minusbtn, sumbtn, divbtn, mulbtn;
double num1, num2;
boolean sum, minus, div, mul;
EditText textarea;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
textarea = (EditText) findViewById(R.id.textarea);
Put this code in onCreate:
#Override
protected void onCreate(Bundle savedInstanceState)
{
...
textarea = (EditText)findViewById(R.id.textarea);
...
}
Check whether you are using dependencies of same version or not. There should not be any version conflict among dependencies.
I am very new to coding so there could be all sorts of issues with this but after doing a fair bit of searching, I can't find whats wrong with it, I don't get any errors or crashes, the buttons just do nothing?
The aim is to have the orange arrows increase and decrease the number in the middle by 1.
App Screenshot:
I don't know what code to show you so here is most of it:
T3_TrampetFragment.java
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class T3_TrampetFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.t3_trampetlayout, container, false);
}
}
t3_trampetlayout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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=".T3_TrampetFragment">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:id="#+id/tblrow_title"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/txt_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="Tariff Calculator"
android:textAlignment="center"
android:textColor="#color/colorPrimaryDark"
android:textSize="36sp"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="#+id/tblrow_rotations"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<TextView
android:id="#+id/txt_rotations"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="Rotations"
android:textAlignment="center"
android:textColor="#color/colorPrimary"
android:textSize="24sp" />
<ImageButton
android:id="#+id/btn_r_minus"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:backgroundTint="?android:attr/colorBackground"
android:clickable="true"
android:foreground="#android:drawable/ic_media_play"
android:foregroundTint="#color/colorSecondary"
android:hapticFeedbackEnabled="true"
android:rotation="180" />
<Space
android:layout_width="10dp"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/Rnum"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:background="?android:attr/colorBackground"
android:focusable="false"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="#string/sRnum"
android:textAlignment="center"
android:textColor="?attr/colorControlNormal"
android:textSize="24sp"
android:textStyle="bold" />
<Space
android:layout_width="10dp"
android:layout_height="wrap_content" />
<ImageButton
android:id="#+id/btn_r_plus"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:backgroundTint="?android:attr/colorBackground"
android:clickable="true"
android:foreground="#android:drawable/ic_media_play"
android:foregroundTint="#color/colorSecondary"
android:hapticFeedbackEnabled="true" />
<Space
android:layout_width="20dp"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:id="#+id/tblrow_pike"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<TextView
android:id="#+id/txt_pike"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="Pike Bonus"
android:textAlignment="center"
android:textColor="#color/colorPrimary"
android:textSize="24sp" />
<ImageButton
android:id="#+id/btn_p_minus"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:backgroundTint="?android:attr/colorBackground"
android:clickable="true"
android:foreground="#android:drawable/ic_media_play"
android:foregroundTint="#color/colorSecondary"
android:hapticFeedbackEnabled="true"
android:rotation="180" />
<Space
android:layout_width="10dp"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/Pnum"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:background="?android:attr/colorBackground"
android:focusable="false"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="#string/sPnum"
android:textAlignment="center"
android:textColor="?attr/colorControlNormal"
android:textSize="24sp"
android:textStyle="bold" />
<Space
android:layout_width="10dp"
android:layout_height="wrap_content" />
<ImageButton
android:id="#+id/btn_p_plus"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:backgroundTint="?android:attr/colorBackground"
android:clickable="true"
android:foreground="#android:drawable/ic_media_play"
android:foregroundTint="#color/colorSecondary"
android:hapticFeedbackEnabled="true" />
<Space
android:layout_width="20dp"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:id="#+id/tblrow_straight"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<TextView
android:id="#+id/txt_straight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="Straight Bonus"
android:textAlignment="center"
android:textColor="#color/colorPrimary"
android:textSize="24sp" />
<ImageButton
android:id="#+id/btn_s_minus"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:backgroundTint="?android:attr/colorBackground"
android:clickable="true"
android:foreground="#android:drawable/ic_media_play"
android:foregroundTint="#color/colorSecondary"
android:hapticFeedbackEnabled="true"
android:rotation="180" />
<Space
android:layout_width="10dp"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/Snum"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:background="?android:attr/colorBackground"
android:focusable="false"
android:inputType="number"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="#string/sSnum"
android:textAlignment="center"
android:textColor="?attr/colorControlNormal"
android:textSize="24sp"
android:textStyle="bold" />
<Space
android:layout_width="10dp"
android:layout_height="wrap_content" />
<ImageButton
android:id="#+id/btn_s_plus"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:backgroundTint="?android:attr/colorBackground"
android:clickable="true"
android:foreground="#android:drawable/ic_media_play"
android:foregroundTint="#color/colorSecondary"
android:hapticFeedbackEnabled="true" />
<Space
android:layout_width="20dp"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:id="#+id/tblrow_twist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<TextView
android:id="#+id/txt_twist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="Twists"
android:textAlignment="center"
android:textColor="#color/colorPrimary"
android:textSize="24sp" />
<ImageButton
android:id="#+id/btn_t_minus"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:backgroundTint="?android:attr/colorBackground"
android:clickable="true"
android:foreground="#android:drawable/ic_media_play"
android:foregroundTint="#color/colorSecondary"
android:hapticFeedbackEnabled="true"
android:rotation="180" />
<Space
android:layout_width="10dp"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/Tnum"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:background="?android:attr/colorBackground"
android:focusable="false"
android:inputType="numberDecimal"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="#string/sTnum"
android:textAlignment="center"
android:textColor="?attr/colorControlNormal"
android:textSize="24sp"
android:textStyle="bold" />
<Space
android:layout_width="10dp"
android:layout_height="wrap_content" />
<ImageButton
android:id="#+id/btn_t_plus"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:backgroundTint="?android:attr/colorBackground"
android:clickable="true"
android:foreground="#android:drawable/ic_media_play"
android:foregroundTint="#color/colorSecondary"
android:hapticFeedbackEnabled="true" />
<Space
android:layout_width="20dp"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:id="#+id/tblrow_result"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="20dp"
android:paddingTop="20dp">
<TextView
android:id="#+id/txt_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="Difficulty"
android:textAlignment="center"
android:textColor="#color/colorPrimary"
android:textSize="24sp"
android:textStyle="bold" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/_space"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:rotation="180"
android:visibility="invisible"
app:backgroundTint="#color/colorSecondary"
app:fabSize="mini"
app:srcCompat="#android:drawable/ic_media_play" />
<Space
android:layout_width="0dp"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/Resultnum"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:background="?android:attr/colorBackground"
android:focusable="false"
android:inputType="numberDecimal"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="#string/sResultnum"
android:textAlignment="center"
android:textColor="?attr/colorControlNormal"
android:textSize="24sp"
android:textStyle="bold" />
<Space
android:layout_width="0dp"
android:layout_height="wrap_content" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/_space2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:visibility="invisible"
app:backgroundTint="#color/colorSecondary"
app:fabSize="mini"
app:srcCompat="#android:drawable/ic_media_play" />
<Space
android:layout_width="20dp"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
</RelativeLayout>
T3_TrampetActivity.java
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.EditText;
public class T3_TrampetActivity extends Activity implements View.OnClickListener {
ImageButton BtnRMinus, BtnRPlus, BtnPMinus, BtnPPlus, BtnSMinus, BtnSPlus, BtnTMinus, BtnTPlus;
EditText R_num, P_num, S_num, T_num, Result_num;
int R_counter = (1);
int P_counter, S_counter = (0);
double T_counter = (0.0);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.t3_trampetlayout);
R_num = (EditText) findViewById(R.id.Rnum);
P_num = (EditText) findViewById(R.id.Pnum);
S_num = (EditText) findViewById(R.id.Snum);
T_num = (EditText) findViewById(R.id.Tnum);
Result_num = (EditText) findViewById(R.id.Resultnum);
BtnRMinus = (ImageButton) findViewById(R.id.btn_r_minus);
BtnRPlus = (ImageButton) findViewById(R.id.btn_r_plus);
BtnPMinus = (ImageButton) findViewById(R.id.btn_p_minus);
BtnPPlus = (ImageButton) findViewById(R.id.btn_p_plus);
BtnSMinus = (ImageButton) findViewById(R.id.btn_s_minus);
BtnSPlus = (ImageButton) findViewById(R.id.btn_s_plus);
BtnTMinus = (ImageButton) findViewById(R.id.btn_t_minus);
BtnTPlus = (ImageButton) findViewById(R.id.btn_t_plus);
BtnRMinus.setOnClickListener(this);
BtnRPlus.setOnClickListener(this);
BtnPMinus.setOnClickListener(this);
BtnPPlus.setOnClickListener(this);
BtnSMinus.setOnClickListener(this);
BtnSPlus.setOnClickListener(this);
BtnTMinus.setOnClickListener(this);
BtnTPlus.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_r_minus:
if (R_counter > 1) {
--R_counter;
R_num.setText(String.valueOf(R_counter));
}
break;
case R.id.btn_r_plus:
if (R_counter < 3) {
++R_counter;
R_num.setText(String.valueOf(R_counter));
}
break;
case R.id.btn_p_minus:
if (P_counter > 0) {
--P_counter;
P_num.setText(String.valueOf(P_counter));
}
break;
case R.id.btn_p_plus:
if (P_counter < 3) {
++P_counter;
P_num.setText(String.valueOf(P_counter));
}
break;
case R.id.btn_s_minus:
if (S_counter > 0) {
--S_counter;
S_num.setText(String.valueOf(S_counter));
}
break;
case R.id.btn_s_plus:
if (S_counter < 3) {
++S_counter;
S_num.setText(String.valueOf(S_counter));
}
break;
case R.id.btn_t_minus:
if (T_counter > 0) {
T_counter = (T_counter - 0.5);
T_num.setText(String.valueOf(T_counter));
}
break;
case R.id.btn_t_plus:
if (T_counter < 5.5) {
T_counter = (T_counter + 0.5);
T_num.setText(String.valueOf(T_counter));
}
break;
default:
break;
}
}
}
strings.xml
<resources>
<string name="app_name">TG Mobile</string>
<string name="in_app_name">TeamGym Mobile</string>
<string name="sRnum">1</string>
<string name="sPnum">0</string>
<string name="sSnum">0</string>
<string name="sTnum">0.0</string>
<string name="sResultnum">0.00</string>
</resources>
Create T3_TrampetActivity like this And Call Your Fragment from Activity:
public class T3_TrampetActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment fragment = new T3_TrampetFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_frame, fragment, fragment.getClass().getSimpleName()).addToBackStack(null).commit();
}
}
Create activity_main.xml as like this:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Update T3_TrampetFragment.java
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class T3_TrampetFragment extends Fragment implements View.OnClickListener {
ImageButton BtnRMinus, BtnRPlus, BtnPMinus, BtnPPlus, BtnSMinus, BtnSPlus, BtnTMinus, BtnTPlus;
EditText R_num, P_num, S_num, T_num, Result_num;
int R_counter = (1);
int P_counter, S_counter = (0);
double T_counter = (0.0);
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.t3_trampetlayout, container, false);
R_num = (EditText) view.findViewById(R.id.Rnum);
P_num = (EditText) view.findViewById(R.id.Pnum);
S_num = (EditText) view.findViewById(R.id.Snum);
T_num = (EditText) view.findViewById(R.id.Tnum);
Result_num = (EditText) view.findViewById(R.id.Resultnum);
BtnRMinus = (ImageButton) view.findViewById(R.id.btn_r_minus);
BtnRPlus = (ImageButton)view.findViewById(R.id.btn_r_plus);
BtnPMinus = (ImageButton) view.findViewById(R.id.btn_p_minus);
BtnPPlus = (ImageButton) view.findViewById(R.id.btn_p_plus);
BtnSMinus = (ImageButton) view.findViewById(R.id.btn_s_minus);
BtnSPlus = (ImageButton) view.findViewById(R.id.btn_s_plus);
BtnTMinus = (ImageButton) view.findViewById(R.id.btn_t_minus);
BtnTPlus = (ImageButton) view.findViewById(R.id.btn_t_plus);
BtnRMinus.setOnClickListener(this);
BtnRPlus.setOnClickListener(this);
BtnPMinus.setOnClickListener(this);
BtnPPlus.setOnClickListener(this);
BtnSMinus.setOnClickListener(this);
BtnSPlus.setOnClickListener(this);
BtnTMinus.setOnClickListener(this);
BtnTPlus.setOnClickListener(this);
return view;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_r_minus:
if (R_counter > 1) {
--R_counter;
R_num.setText(String.valueOf(R_counter));
}
break;
case R.id.btn_r_plus:
if (R_counter < 3) {
++R_counter;
R_num.setText(String.valueOf(R_counter));
}
break;
case R.id.btn_p_minus:
if (P_counter > 0) {
--P_counter;
P_num.setText(String.valueOf(P_counter));
}
break;
case R.id.btn_p_plus:
if (P_counter < 3) {
++P_counter;
P_num.setText(String.valueOf(P_counter));
}
break;
case R.id.btn_s_minus:
if (S_counter > 0) {
--S_counter;
S_num.setText(String.valueOf(S_counter));
}
break;
case R.id.btn_s_plus:
if (S_counter < 3) {
++S_counter;
S_num.setText(String.valueOf(S_counter));
}
break;
case R.id.btn_t_minus:
if (T_counter > 0) {
T_counter = (T_counter - 0.5);
T_num.setText(String.valueOf(T_counter));
}
break;
case R.id.btn_t_plus:
if (T_counter < 5.5) {
T_counter = (T_counter + 0.5);
T_num.setText(String.valueOf(T_counter));
}
break;
default:
break;
}
}
}
t3_trampetlayout.xml Use same layou for Fragment.
Sorry if this is a very noob question, but the code below is not giving an output and I the logcat view isn't clear so I can't tell how far it is getting when I run the emulator.
Have I got the radio buttons coded correctly?
Are the variable statements coded correctly?
Can you see any other errors?
Layout xml is as follows:
<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=".MainActivity" >
<EditText
android:id="#+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText3"
android:layout_below="#+id/editText3"
android:layout_marginTop="27dp"
android:ems="10" />
<EditText
android:id="#+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText2"
android:layout_below="#+id/editText2"
android:layout_marginTop="19dp"
android:ems="10" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText1"
android:layout_below="#+id/editText1"
android:layout_marginTop="16dp"
android:ems="10" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="21dp"
android:ems="10" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText1"
android:layout_toRightOf="#+id/editText1"
android:text="#string/pressu"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/editText3"
android:layout_toRightOf="#+id/editText2"
android:text="#string/volu"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText3"
android:layout_alignLeft="#+id/textView2"
android:text="#string/molu"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editText4"
android:layout_alignLeft="#+id/textView3"
android:text="#string/tempu"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText4"
android:layout_below="#+id/editText4"
android:layout_marginTop="35dp"
android:text="#string/calc" />
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/button1"
android:layout_marginRight="33dp" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/pressure" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/vol" />
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/mol" />
<RadioButton
android:id="#+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/temp" />
</RadioGroup>
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_below="#+id/button1"
android:layout_marginTop="22dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
The Java for the main activity is here
package com.minichanic.idealgas;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
private EditText pressure;
private EditText volume;
private EditText moles;
private EditText temperature;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pressure = (EditText) findViewById(R.id.editText1);
volume = (EditText) findViewById(R.id.editText2);
moles = (EditText) findViewById(R.id.editText3);
temperature= (EditText) findViewById(R.id.editText4);
}
public void onClick(View view) {
//set values to the input
float pressuren = Float.parseFloat(pressure.getText().toString());
float volumen = Float.parseFloat(volume.getText().toString());
float molesn = Float.parseFloat(moles.getText().toString());
float temperaturen = Float.parseFloat(temperature.getText().toString());
//change values to standard conditions if blank
if (pressuren == 0) {pressuren = 100000;
}
if (volumen == 0) {volumen = (float)0.0247;
}
if (molesn == 0) {molesn= 1;
}
if (temperaturen == 0) {temperaturen = 298;
}
//set input values as answers for set text later
float pressans = (float) pressuren;
float volans = (float) volumen;
float moleans = (float) molesn;
float temperaturans = (float) temperaturen;
//set calculation depending on radio button
switch (view.getId()) {
case R.id.radio0:
pressans = (float) (molesn * 8.31 * temperaturen ) / volumen;
break;
case R.id.radio1:
volans = (float) (molesn * 8.31 * temperaturen ) / pressuren;
break;
case R.id.radio2:
moleans = (float) ((float) (pressuren * volumen)/(8.31 * temperaturen));
break;
case R.id.radio3:
temperaturans = (float) ((float) (pressuren * volumen)/(8.31 * molesn));
break;
}
//print values in the EditText fields
pressure.setText(""+pressans);
volume.setText(""+volans);
moles.setText(""+moleans);
temperature.setText(""+temperaturans);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
The logCat i am getting for the package is:
Error opening trace file: no such file or directory
Any ideas will be greatly appreciated.
Its a system level error log and it does not affect execution of your application, so you don't have to worry about it.
Generally Its occurs Jelly Bean (android 4.1).And mainly its because of some SD-card issue.