1. Download our Official Android App: Forums for Android!

Support Unable to compile my code for MyCalculator APP

Discussion in 'Android Help' started by Samarth Kejriwal, Jun 25, 2016.

?

I am unable to find the bug in my code ..It is not able to compile while i run my code.

  1. Correct

    0 vote(s)
    0.0%
  2. Incorrect

    0 vote(s)
    0.0%
Multiple votes are allowed.
  1. Samarth Kejriwal

    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    1
    Joined:
    Jun 25, 2016

    Jun 25, 2016
    1
    0
    5
    Male
    JAVA CODE
    Code (Java):
    1. package com.example.android.mycalculator;
    2.  
    3. import android.app.Activity;
    4. import android.os.Bundle;
    5. import android.view.View;
    6. import android.widget.AdapterView;
    7. import android.widget.ArrayAdapter;
    8. import android.widget.EditText;
    9. import android.widget.Spinner;
    10. import android.widget.TextView;
    11.  
    12. public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener {
    13.  
    14.     Spinner spinner;
    15.     TextView result;
    16.     private EditText text1;
    17.     private EditText text2;
    18.     double l1,l2;
    19.     private String[] operations = {"Add","Subtract","Multiply","Divide"};
    20.     @Override
    21.     protected void onCreate(Bundle savedInstanceState) {
    22.         super.onCreate(savedInstanceState);
    23.         setContentView(R.layout.activity_main);
    24.  
    25.         spinner = (Spinner) findViewById(R.id.spinner);
    26.         result=(TextView) findViewById(R.id.result);
    27.         spinner.setOnItemSelectedListener(this);
    28.  
    29.         text1 = (EditText) findViewById(R.id.number1);
    30.         text2 = (EditText) findViewById(R.id.number2);
    31.         l1 = Double.parseDouble(text1.getText().toString());
    32.         l2=Double.parseDouble(text2.getText().toString());
    33.  
    34.  
    35.         // Spinner Drop down elements
    36.  
    37.         // Creating adapter for spinner
    38.         ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, operations);
    39.  
    40.         // Drop down layout style - list view with radio button
    41.         dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    42.  
    43.         // attaching data adapter to spinner
    44.         spinner.setAdapter(dataAdapter);
    45.     }
    46.  
    47.  
    48.     public String add() {
    49.         double result=l1+l2;
    50.         return Double.toString(result);
    51.     }
    52.     public String sub() {
    53.         double result=l1-l2;
    54.         return Double.toString(result);
    55.     }
    56.     public String mul() {
    57.         double result=l1*l2;
    58.         return Double.toString(result);
    59.     }
    60.  
    61.     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    62.         // On selecting a spinner item
    63.         String item = parent.getItemAtPosition(position).toString();
    64.         String selected = (String) spinner.getSelectedItem();
    65.         if(selected.equals(operations[0])) result.setText(add());
    66.         if(selected.equals(operations[1])) result.setText(sub());
    67.         if(selected.equals(operations[2])) result.setText(mul());
    68.  
    69.     }
    70.     public void onNothingSelected(AdapterView<?> arg0) {
    71.         // TODO Auto-generated method stub
    72.     }
    73.  
    74. }
    75.  
    Code (Text):
    1. XML CODE-
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    4.     xmlns:tools="http://schemas.android.com/tools"
    5.     android:layout_width="match_parent"
    6.     android:layout_height="match_parent"
    7.     android:paddingBottom="@dimen/activity_vertical_margin"
    8.     android:paddingLeft="@dimen/activity_horizontal_margin"
    9.     android:paddingRight="@dimen/activity_horizontal_margin"
    10.     android:paddingTop="@dimen/activity_vertical_margin"
    11.     tools:context="com.example.android.mycalculator.MainActivity">
    12.  
    13.     <TextView
    14.         android:layout_width="wrap_content"
    15.         android:layout_height="wrap_content"
    16.         android:text="@string/Operation"
    17.         android:textSize="20sp"/>
    18.     <Spinner
    19.         android:id="@+id/spinner"
    20.         android:layout_width="fill_parent"
    21.         android:layout_height="wrap_content"
    22.         android:prompt="@string/spinner_title"
    23.         android:layout_marginTop="25dp" />
    24.     <EditText
    25.         android:id="@+id/number1"
    26.         android:layout_width="wrap_content"
    27.         android:layout_height="wrap_content"
    28.         android:hint="Enter Number"
    29.         android:inputType="number"
    30.         android:layout_below="@+id/spinner"/>
    31.     <EditText
    32.         android:id="@+id/number2"
    33.         android:layout_width="wrap_content"
    34.         android:layout_height="wrap_content"
    35.         android:hint="Enter Number"
    36.         android:inputType="number"
    37.         android:layout_below="@+id/number1"/>
    38.     <TextView
    39.         android:id="@+id/result"
    40.         android:layout_width="wrap_content"
    41.         android:layout_height="wrap_content"
    42.         android:layout_marginTop="5dp"
    43.         android:paddingLeft="8dp"
    44.         android:paddingRight="8dp"
    45.         android:text="RESULT"
    46.         android:textColor="#000000"
    47.         android:textSize="20sp"
    48.         android:layout_below="@id/number2"/>
    49.     <View
    50.         android:layout_width="150dp"
    51.         android:layout_height="2dp"
    52.         android:layout_below="@+id/result"
    53.         android:background="@android:color/darker_gray" />
    54.  
    55.  
    56. </RelativeLayout>
    57.  
     

    Advertisement

Share This Page

Loading...