Simple calc, For every button I must define a value, can i get it direct from button ?
Its a simple app, but when i want to test it, I get error "app stopped". I'm using one listener for all buttons, I think it causes an error.
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
Button buttOne, buttTwo, buttThree, buttFour, buttFive, buttSix, buttSeven, buttEight, buttNine, buttZero;
Button buttSum, buttSubstr;
Double result, buffer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttOne = (Button) findViewById(R.id.one);
buttTwo = (Button) findViewById(R.id.two);
buttThree = (Button) findViewById(R.id.three);
buttFour = (Button) findViewById(R.id.four);
buttFive = (Button) findViewById(R.id.five);
buttSix = (Button) findViewById(R.id.six);
buttSeven = (Button) findViewById(R.id.seven);
buttEight = (Button) findViewById(R.id.eight);
buttNine = (Button) findViewById(R.id.nine);
buttZero = (Button) findViewById(R.id.zero);
buttSum = (Button) findViewById(R.id.sum);
buttSubstr = (Button) findViewById(R.id.substr);
buttOne.setOnClickListener(this);
buttTwo.setOnClickListener(this);
buttThree.setOnClickListener(this);
buttFour.setOnClickListener(this);
buttFive.setOnClickListener(this);
buttSix.setOnClickListener(this);
buttSeven.setOnClickListener(this);
buttEight.setOnClickListener(this);
buttNine.setOnClickListener(this);
buttZero.setOnClickListener(this);
buttSum.setOnClickListener(this);
buttSubstr.setOnClickListener(this);
result = 0.0;
}
/*
* #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; }
*/
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.one:
buffer = 1.0;
break;
case R.id.two:
buffer = 2.0;
break;
case R.id.three:
buffer = 3.0;
break;
case R.id.four:
buffer = 4.0;
break;
case R.id.five:
buffer = 5.0;
break;
case R.id.six:
buffer = 6.0;
break;
case R.id.seven:
buffer = 7.0;
break;
case R.id.eight:
buffer = 8.0;
break;
case R.id.nine:
buffer = 9.0;
break;
case R.id.zero:
buffer = 0.0;
break;
case R.id.substr:
result = -buffer;
break;
case R.id.sum:
result = +buffer;
}
}
}
layout:
<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:background="#style/AppTheme"
android:gravity="center_horizontal"
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" >
<Button
android:id="#+id/seven"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/seven" />
<Button
android:id="#+id/eight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/seven"
android:layout_alignBottom="#+id/seven"
android:layout_toRightOf="#+id/seven"
android:text="#string/eight" />
<Button
android:id="#+id/nine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/eight"
android:layout_alignBottom="#+id/eight"
android:layout_toRightOf="#+id/eight"
android:text="#string/nine" />
<Button
android:id="#+id/four"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/seven"
android:layout_below="#+id/seven"
android:text="#string/four" />
<Button
android:id="#+id/five"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/four"
android:layout_alignBottom="#+id/four"
android:layout_toRightOf="#+id/four"
android:text="#string/five" />
<Button
android:id="#+id/six"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/nine"
android:layout_below="#+id/nine"
android:text="#string/six" />
<Button
android:id="#+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/four"
android:layout_below="#+id/four"
android:text="#string/one" />
<Button
android:id="#+id/two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/five"
android:layout_below="#+id/five"
android:text="#string/two" />
<Button
android:id="#+id/three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/six"
android:layout_below="#+id/six"
android:text="#string/three" />
<Button
android:id="#+id/zero"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/one"
android:layout_toLeftOf="#+id/three"
android:text="#string/zero" />
<Button
android:id="#+id/division"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/nine"
android:text="#string/division" />
<Button
android:id="#+id/multiple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/six"
android:layout_alignBottom="#+id/six"
android:layout_toRightOf="#+id/six"
android:text="#string/multiple" />
<Button
android:id="#+id/substr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/six"
android:layout_toRightOf="#+id/six"
android:text="#string/subst" />
<Button
android:id="#+id/sum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/three"
android:layout_toRightOf="#+id/three"
android:text="#string/sum" />
<Button
android:id="#+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/sum"
android:layout_alignBottom="#+id/sum"
android:layout_toLeftOf="#+id/sum"
android:text="#string/result" />
<Button
android:id="#+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/one"
android:layout_below="#+id/one"
android:text="#string/clear" />
<EditText
android:id="#+id/output"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/eight"
android:layout_alignRight="#+id/division"
android:ems="10"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/eight"
android:layout_alignRight="#+id/division"
android:ems="10"
android:inputType="numberDecimal" />
console:
[2014-01-16 15:42:41 - ddmlib] An established connection was aborted by the software installed on the host computer java.io.IOException: An established connection was aborted by the software installed on the host computer
at sun.nio.ch.SocketDispatcher.write0(Native Method)
at sun.nio.ch.SocketDispatcher.write(Unknown Source)
at sun.nio.ch.IOUtil.writeFromNativeBuffer(Unknown Source)
at sun.nio.ch.IOUtil.write(Unknown Source)
at sun.nio.ch.SocketChannelImpl.write(Unknown Source)
at com.android.ddmlib.JdwpPacket.writeAndConsume(JdwpPacket.java:213)
at com.android.ddmlib.Client.sendAndConsume(Client.java:642)
at com.android.ddmlib.HandleHeap.sendREAQ(HandleHeap.java:348)
at com.android.ddmlib.Client.requestAllocationStatus(Client.java:488)
at com.android.ddmlib.DeviceMonitor.createClient(DeviceMonitor.java:835)
at com.android.ddmlib.DeviceMonitor.openClient(DeviceMonitor.java:803)
at com.android.ddmlib.DeviceMonitor.processIncomingJdwpData(DeviceMonitor.java:763)
at com.android.ddmlib.DeviceMonitor.deviceClientMonitorLoop(DeviceMonitor.java:652)
at com.android.ddmlib.DeviceMonitor.access$100(DeviceMonitor.java:44)
at com.android.ddmlib.DeviceMonitor$3.run(DeviceMonitor.java:580)
You need to add the onClickListeners.
buttOne.setOnClickListener(this);
and so on ... :)
Maybe this is for you. Use the android:tag like proposed in OcuS' answer.
Related
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.
I'm doing a simple calculator in android studio, my code looks like this:
MainActivity.java:
package com.example.ja.calculator;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
String total="";
double v1, v2;
String sign="";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
public void onClick(View v){
Button button = (Button)v;
String str=button.getText().toString();
total+=str;
EditText edit=(EditText)findViewById(R.id.editText);
edit.setText(total);
}
public void OnAdd(View v){
v1 = Double.parseDouble(total);
total = "";
Button button = (Button)v;
String str = button.getText().toString();
sign = str;
EditText edit = (EditText)findViewById(R.id.editText);
edit.setText("");
}
public void OnCalculate(View v){
EditText edit = (EditText)findViewById(R.id.editText);
String str2 = edit.getText().toString();
v2 = Double.parseDouble(str2);
double grand_total = 0;
if(sign.equals("+")){
grand_total = v1 + v2;
}
else if(sign.equals("-")){
grand_total = v1 - v2;
}
else if(sign.equals("x")){
grand_total = v1 * v2;
}
else if(sign.equals(":")){
grand_total = v1 / v2;
}
edit.setText(grand_total+"");
}
public void OnClear(View v){
EditText edit = (EditText)findViewById(R.id.editText);
edit.setText("");
total = "";
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
content main:
<?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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.ja.calculator.MainActivity"
tools:showIn="#layout/activity_main">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:height="80dp"
android:onClick="onClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:id="#+id/button2"
android:layout_alignTop="#+id/button"
android:layout_toRightOf="#+id/button"
android:layout_toEndOf="#+id/button"
android:height="80dp"
android:onClick="onClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:id="#+id/button3"
android:layout_alignTop="#+id/button2"
android:layout_toRightOf="#+id/button2"
android:layout_toEndOf="#+id/button2"
android:height="80dp"
android:onClick="onClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:id="#+id/button4"
android:layout_above="#+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:height="80dp"
android:onClick="onClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:id="#+id/button5"
android:layout_above="#+id/button2"
android:layout_toLeftOf="#+id/button3"
android:layout_toStartOf="#+id/button3"
android:height="80dp"
android:onClick="onClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:id="#+id/button6"
android:layout_above="#+id/button3"
android:layout_alignLeft="#+id/button3"
android:layout_alignStart="#+id/button3"
android:height="80dp"
android:onClick="onClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
android:id="#+id/button7"
android:layout_above="#+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:height="80dp"
android:onClick="onClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8"
android:id="#+id/button8"
android:layout_alignTop="#+id/button7"
android:layout_toRightOf="#+id/button4"
android:layout_toEndOf="#+id/button4"
android:height="80dp"
android:onClick="onClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9"
android:id="#+id/button9"
android:layout_alignTop="#+id/button8"
android:layout_toRightOf="#+id/button5"
android:layout_toEndOf="#+id/button5"
android:height="80dp"
android:onClick="onClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:id="#+id/button0"
android:layout_below="#+id/button"
android:layout_toRightOf="#+id/button"
android:layout_toEndOf="#+id/button"
android:height="80dp"
android:onClick="onClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="."
android:id="#+id/buttonDot"
android:layout_below="#+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:height="80dp"
android:onClick="onClick" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="="
android:id="#+id/buttonEQ"
android:layout_below="#+id/button3"
android:layout_alignLeft="#+id/button3"
android:layout_alignStart="#+id/button3"
android:height="80dp"
android:onClick="OnCalculate" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:id="#+id/buttonAdd"
android:height="64dp"
android:layout_alignBottom="#+id/buttonEQ"
android:layout_alignLeft="#+id/buttonSub"
android:layout_alignStart="#+id/buttonSub"
android:onClick="OnAdd" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:id="#+id/buttonSub"
android:height="64dp"
android:layout_above="#+id/buttonAdd"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="OnAdd" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="x"
android:id="#+id/buttonMul"
android:height="64dp"
android:layout_above="#+id/buttonSub"
android:layout_alignLeft="#+id/buttonSub"
android:layout_alignStart="#+id/buttonSub"
android:onClick="OnAdd" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=":"
android:id="#+id/buttonDiv"
android:layout_above="#+id/buttonMul"
android:layout_alignLeft="#+id/buttonMul"
android:layout_alignStart="#+id/buttonMul"
android:height="64dp"
android:onClick="OnAdd" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CE"
android:id="#+id/buttonCE"
android:height="64dp"
android:layout_above="#+id/buttonDiv"
android:layout_toRightOf="#+id/button9"
android:layout_toEndOf="#+id/button9"
android:onClick="OnClear" />
The problem is, when I click some number and after that I click the operation (+,-,/,*) the number clicked before disappears, how to fix it, so I can see full operation, for example 5*5 before clicking "=" and after that changing just for result?
public void OnAdd(View v) {
v1 = Double.parseDouble(total);
total = "";
Button button = (Button) v;
String str = button.getText().toString();
sign = str;
EditText edit = (EditText) findViewById(R.id.editText); ** edit.setText("");
}
The reason for number clicked before gets cleared is that you set text as "". (see **)
You should do the following changes to your code if your calculator to work in the correct way.
public void OnAdd(View v) {
v1 = Double.parseDouble(total);
Button button = (Button) v;
String str = button.getText().toString();
sign = str;
total += str;
EditText edit = (EditText) findViewById(R.id.editText);
edit.setText(total);
}
Now the sign will be appended to the total string. And it will be written back to the editText.
I was searching solution for my problem, but I didn't find any.
I'm creating android application and one activity layout has several activities:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".EkranGlowny"
android:id="#+id/aaa"
android:background="#ffff8000"
android:orientation="horizontal">
<LinearLayout
android:orientation="vertical"
android:layout_width="130dp"
android:layout_height="540dp"
android:background="#ffff8000"
android:layout_gravity="right"
android:baselineAligned="false"
android:clickable="false"
android:focusable="false"
android:weightSum="1">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Krystian Pruchnik"
android:id="#+id/waiter_name"
android:textSize="20sp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ID: 180881"
android:id="#+id/waiter_id"
android:textSize="20sp"
android:layout_marginLeft="5dp"
android:layout_marginBottom="30dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Zamówienie"
android:textSize="20sp"
android:id="#+id/order"
android:layout_marginBottom="10dp" />
<TextView
android:layout_width="130dp"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/viewOrder"
android:layout_marginRight="5dp"
android:background="#ffffffff" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="58dp"
android:layout_height="540dp"
android:background="#69ff8300"
android:weightSum="1"
android:baselineAligned="false">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="1"
android:id="#+id/button1" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="2"
android:id="#+id/button2" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="3"
android:id="#+id/button3" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="4"
android:id="#+id/button4" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="5"
android:id="#+id/button5" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="6"
android:id="#+id/button6" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="7"
android:id="#+id/button7" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="8"
android:id="#+id/button8" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="9"
android:id="#+id/button9" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="0"
android:id="#+id/button0" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="49dp"
android:text="#string/clear_button"
android:id="#+id/clear_button"
android:layout_gravity="top"
android:background="#ffff0000"
android:textColor="#ffffffff"
android:textSize="11sp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="5dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="540dp"
android:background="#69ff8300"
android:weightSum="1"
android:baselineAligned="false">
<ScrollView
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="480dp"
android:background="#ffff8000">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow
android:id="#+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dip" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Zestawy"
android:id="#+id/zestawy"
android:textAlignment="center"
android:textIsSelectable="false"
android:layout_span="3"/>
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dip">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa1"
android:id="#+id/meal1"
android:layout_column="0"
android:layout_weight="1"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa2"
android:id="#+id/meal2"
android:layout_column="1"
android:layout_weight="1"/>
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dip" >
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa3"
android:id="#+id/meal3"
android:layout_column="0"
android:layout_weight="1"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa4"
android:id="#+id/meal4"
android:layout_column="1"
android:layout_weight="1"/>
</TableRow>
<TableRow
android:id="#+id/tableRow4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dip" >
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa5"
android:id="#+id/meal5"
android:layout_column="0"
android:layout_weight="1"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa6"
android:id="#+id/meal6"
android:layout_column="1"
android:layout_weight="1"/>
</TableRow>
<TableRow
android:id="#+id/tableRow5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dip" >
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa7"
android:id="#+id/meal7"
android:layout_column="0"
android:layout_weight="1"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa8"
android:id="#+id/meal8"
android:layout_column="1"
android:layout_weight="1"/>
</TableRow>
<TableRow
android:id="#+id/tableRow6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dip" >
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa9"
android:id="#+id/meal9"
android:layout_column="0"
android:layout_weight="1"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa10"
android:id="#+id/meal10"
android:layout_column="1"
android:layout_weight="1"/>
</TableRow>
<TableRow
android:id="#+id/tableRow7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dip" >
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa11"
android:id="#+id/meal11"
android:layout_column="0"
android:layout_weight="1"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa12"
android:id="#+id/meal12"
android:layout_column="1"
android:layout_weight="1"/>
</TableRow>
<TableRow
android:id="#+id/tableRow8"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dip" >
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa13"
android:id="#+id/meal13"
android:layout_column="0"
android:layout_weight="1"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/potrawa14"
android:id="#+id/meal14"
android:layout_column="1"
android:layout_weight="1"/>
</TableRow>
</TableLayout>
</ScrollView>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#69ff8300"
android:weightSum="1"
android:baselineAligned="false">
<Button
android:layout_width="48dp"
android:layout_height="34dp"
android:text="VOID"
android:id="#+id/anuluj"
android:background="#ffff2800" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rozlicz"
android:id="#+id/rozlicz"
android:layout_weight="1.18" />
</LinearLayout>
</LinearLayout>
I want that if I click some button (from second and third linear layout) it'll put text in TextView (id: viewOrder in first linear layout). Whenever I try this, it's showing message: "Unfortunately WaiterApp is stopped" and application crashes.
My Main Activity in java:
public class EkranGlowny extends ActionBarActivity implements OnClickListener{
Button summary;
Context context;
Button but0;
Button but1;
Button but2;
Button but3;
Button but4;
Button but5;
Button but6;
Button but7;
Button but8;
Button but9;
Button butClr;
Button voiD;
Button meal1;
Button meal2;
Button meal3;
Button meal4;
Button meal5;
Button meal6;
Button meal7;
Button meal8;
Button meal9;
Button meal10;
Button meal11;
Button meal12;
Button meal13;
Button meal14;
TextView viewOrder;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ekran_glowny);
setupVariables();
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.rozlicz:
context = getApplicationContext();
Intent intent = new Intent(context, ResumeScreen.class);
startActivity(intent);
break;
/*case R.id.button0:
viewOrder.append(but0.getText().toString());
break;
case R.id.button1:
viewOrder.append("1");
break;
case R.id.button2:
viewOrder.append("2");
break;*/
default:
viewOrder.append(((Button) v).getText().toString());
break;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
//implementacja zmiennych
private void setupVariables() {
summary = (Button) findViewById(R.id.rozlicz);
summary.setOnClickListener(this);
but0 = (Button) findViewById(R.id.button0);
but0.setOnClickListener(this);
but1 = (Button) findViewById(R.id.button1);
but1.setOnClickListener(this);
but2 = (Button) findViewById(R.id.button2);
but2.setOnClickListener(this);
but3 = (Button) findViewById(R.id.button3);
but3.setOnClickListener(this);
but4 = (Button) findViewById(R.id.button4);
but4.setOnClickListener(this);
but5 = (Button) findViewById(R.id.button5);
but5.setOnClickListener(this);
but6 = (Button) findViewById(R.id.button6);
but6.setOnClickListener(this);
but7 = (Button) findViewById(R.id.button7);
but7.setOnClickListener(this);
but8 = (Button) findViewById(R.id.button8);
but8.setOnClickListener(this);
but9 = (Button) findViewById(R.id.button9);
but9.setOnClickListener(this);
butClr = (Button) findViewById(R.id.clear_button);
butClr.setOnClickListener(this);
meal1 = (Button) findViewById(R.id.meal1);
meal1.setOnClickListener(this);
meal2 = (Button) findViewById(R.id.meal2);
meal2.setOnClickListener(this);
meal3 = (Button) findViewById(R.id.meal3);
meal3.setOnClickListener(this);
meal4 = (Button) findViewById(R.id.meal4);
meal4.setOnClickListener(this);
meal5 = (Button) findViewById(R.id.meal5);
meal5.setOnClickListener(this);
meal6 = (Button) findViewById(R.id.meal6);
meal6.setOnClickListener(this);
meal7 = (Button) findViewById(R.id.meal7);
meal7.setOnClickListener(this);
meal8 = (Button) findViewById(R.id.meal8);
meal8.setOnClickListener(this);
meal9 = (Button) findViewById(R.id.meal9);
meal9.setOnClickListener(this);
meal10 = (Button) findViewById(R.id.meal10);
meal10.setOnClickListener(this);
meal11 = (Button) findViewById(R.id.meal11);
meal11.setOnClickListener(this);
meal12 = (Button) findViewById(R.id.meal12);
meal12.setOnClickListener(this);
meal13 = (Button) findViewById(R.id.meal13);
meal13.setOnClickListener(this);
meal14 = (Button) findViewById(R.id.meal14);
meal14.setOnClickListener(this);
voiD = (Button) findViewById(R.id.anuluj);
voiD.setOnClickListener(this);
viewOrder = (TextView) findViewById(R.id.viewOrder);
}
}
I tried viewOrder.setText and viewOrder.append and nothing is working.
Is the problem is that TextView and buttons are in different layouts?
Could you tell me what I'm doing wrong?
edit:
In logcat I get:
06-07 14:17:40.290 7755-7755/com.example.krystian.waiterapp W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40da51f8)
06-07 14:17:40.290 7755-7755/com.example.krystian.waiterapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.krystian.waiterapp.EkranGlowny.onClick(EkranGlowny.java:78)
at android.view.View.performClick(View.java:3524)
at android.view.View$PerformClick.run(View.java:14194)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4476)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:816)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:583)
at dalvik.system.NativeStart.main(Native Method)
Check your xml file name. It should be ekran_glowny.
My guess is you showed xml code from different xml file.
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.