2014-11-12 4 views
-4

Я делаю очень простой калькулятор на Android. Он не показывает ошибки в eclipse, но когда я запускаю его на своем Android-устройстве, он перестает работать при нажатии кнопок.Почему этот код не работает?

Вот мой XML:

<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="com.example.simplecalculator.MainActivity" > 
 

 
    <EditText 
 
     android:id="@+id/editText1" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="wrap_content" 
 
     android:layout_alignParentTop="true" 
 
     android:layout_centerHorizontal="true" 
 
     android:layout_marginTop="36dp" 
 
     android:ems="10" 
 
     android:hint="@string/enter_a_no" 
 
     android:inputType="numberSigned|numberDecimal" 
 
     /> 
 

 
     <requestFocus /> 
 
    
 

 
    <Button 
 
     android:id="@+id/minus" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_alignBaseline="@+id/plus" 
 
     android:layout_alignBottom="@+id/plus" 
 
     android:layout_toRightOf="@+id/plus" 
 
     android:text="@string/minus" 
 
     android:onClick="minusClick" /> 
 

 
    <Button 
 
     android:id="@+id/multiply" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_alignLeft="@+id/plus" 
 
     android:layout_below="@+id/plus" 
 
     android:text="@string/multiply" 
 
     android:onClick="multiplyClick"/> 
 

 
    <Button 
 
     android:id="@+id/divide" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_alignBaseline="@+id/multiply" 
 
     android:layout_alignBottom="@+id/multiply" 
 
     android:layout_alignLeft="@+id/minus" 
 
     android:text="@string/divide" 
 
     android:onClick="divideClick" /> 
 

 
    <Button 
 
     android:id="@+id/equals" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_above="@+id/divide" 
 
     android:layout_alignTop="@+id/minus" 
 
     android:layout_toRightOf="@+id/minus" 
 
     android:onClick="equalClick" 
 
     android:text="@string/equals" /> 
 

 
    <Button 
 
     android:id="@+id/_1" 
 
     style="?android:attr/buttonStyleSmall" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_alignLeft="@+id/plus" 
 
     android:layout_below="@+id/editText1" 
 
     android:layout_marginTop="14dp" 
 
     android:text="@string/_1" 
 
     android:onClick="_1click"/> 
 

 
    <Button 
 
     android:id="@+id/_2" 
 
     style="?android:attr/buttonStyleSmall" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_alignBaseline="@+id/_1" 
 
     android:layout_alignBottom="@+id/_1" 
 
     android:layout_toRightOf="@+id/_1" 
 
     android:text="@string/_2" 
 
     android:onClick="_2click"/> 
 

 
    <Button 
 
     android:id="@+id/clear" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_alignBaseline="@+id/divide" 
 
     android:layout_alignBottom="@+id/divide" 
 
     android:layout_toRightOf="@+id/divide" 
 
     android:onClick="clearClick" 
 
     android:text="@string/ce" /> 
 

 
    <Button 
 
     android:id="@+id/plus" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_alignLeft="@+id/editText1" 
 
     android:layout_below="@+id/_1" 
 
     android:layout_marginLeft="33dp" 
 
     android:layout_marginTop="127dp" 
 
     android:hint="@string/plus" 
 
     android:onClick="plusClick" 
 
     android:text="@string/plus" /> 
 

 
    <Button 
 
     android:id="@+id/_4" 
 
     style="?android:attr/buttonStyleSmall" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_below="@+id/_1" 
 
     android:layout_toLeftOf="@+id/_2" 
 
     android:text="@string/_4" 
 
     android:onClick="_4click"/> 
 

 
    <Button 
 
     android:id="@+id/_5" 
 
     style="?android:attr/buttonStyleSmall" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_alignBaseline="@+id/_4" 
 
     android:layout_alignBottom="@+id/_4" 
 
     android:layout_alignLeft="@+id/_2" 
 
     android:text="@string/_5" 
 
     android:onClick="_5click" /> 
 

 
    <Button 
 
     android:id="@+id/_6" 
 
     style="?android:attr/buttonStyleSmall" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_alignBaseline="@+id/_5" 
 
     android:layout_alignBottom="@+id/_5" 
 
     android:layout_toRightOf="@+id/_5" 
 
     android:text="@string/_6" 
 
     android:onClick="_6click"/> 
 

 
    <Button 
 
     android:id="@+id/_7" 
 
     style="?android:attr/buttonStyleSmall" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_below="@+id/_4" 
 
     android:layout_toLeftOf="@+id/_5" 
 
     android:text="@string/_7" 
 
     android:onClick="_7click"/> 
 

 
    <Button 
 
     android:id="@+id/_8" 
 
     style="?android:attr/buttonStyleSmall" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_below="@+id/_5" 
 
     android:layout_toLeftOf="@+id/_6" 
 
     android:text="@string/_8" 
 
     android:onClick="_8click"/> 
 

 
    <Button 
 
     android:id="@+id/_9" 
 
     style="?android:attr/buttonStyleSmall" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_below="@+id/_5" 
 
     android:layout_toRightOf="@+id/_5" 
 
     android:text="@string/_9" 
 
     android:onClick="_9click"/> 
 

 
    <Button 
 
     android:id="@+id/_3" 
 
     style="?android:attr/buttonStyleSmall" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_above="@+id/_5" 
 
     android:layout_toRightOf="@+id/_2" 
 
     android:text="@string/_3" 
 
     android:onClick="_3click"/> 
 

 
    <Button 
 
     android:id="@+id/_0" 
 
     style="?android:attr/buttonStyleSmall" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:layout_below="@+id/_6" 
 
     android:layout_toRightOf="@+id/_6" 
 
     android:text="@string/_0" 
 
     android:onClick="_0click"/> 
 

 
</RelativeLayout>

Вот мой Java-код:

package com.example.simplecalculator; 
 

 
import android.support.v7.app.ActionBarActivity; 
 
import android.os.Bundle; 
 
import android.view.Menu; 
 
import android.view.MenuItem; 
 
import android.view.View; 
 

 
import android.widget.Button; 
 
import android.widget.EditText; 
 

 

 
public class MainActivity extends ActionBarActivity { 
 
    private double vrble; 
 
    private double rslt; 
 
    private double vrble2; 
 
    String text; 
 
    EditText etext; 
 
    Button plus; 
 
    Button minus; 
 
    Button multiply; 
 
    Button divide; 
 
    Button equal; 
 

 
    @ 
 
    Override 
 
    protected void onCreate(Bundle savedInstanceState) { 
 
    super.onCreate(savedInstanceState); 
 
    setContentView(R.layout.activity_main); 
 
    if (savedInstanceState == null) { 
 
     vrble = 0; 
 
     rslt = 0; 
 
     etext = (EditText) findViewById(R.id.editText1); 
 
     plus = (Button) findViewById(R.id.plus); 
 
     minus = (Button) findViewById(R.id.minus); 
 
     multiply = (Button) findViewById(R.id.multiply); 
 
     divide = (Button) findViewById(R.id.divide); 
 
     equal = (Button) findViewById(R.id.equals); 
 
    } else { 
 
     vrble = 0; 
 
     rslt = 0; 
 
     vrble2 = 0; 
 
     etext = (EditText) findViewById(R.id.editText1); 
 
     plus = (Button) findViewById(R.id.plus); 
 
     minus = (Button) findViewById(R.id.minus); 
 
     multiply = (Button) findViewById(R.id.multiply); 
 
     divide = (Button) findViewById(R.id.divide); 
 
     equal = (Button) findViewById(R.id.equals); 
 

 

 

 
    } 
 

 
    } 
 

 
    public void _0click(View v) { 
 
    text = etext.getText().toString(); 
 
    etext.setText(text + "0"); 
 
    } 
 
    public void _1click(View v) { 
 
    text = etext.getText().toString(); 
 
    etext.setText(text + "1"); 
 
    } 
 
    public void _2click(View v) { 
 
    text = etext.getText().toString(); 
 
    etext.setText(text + "2"); 
 
    } 
 
    public void _3click(View v) { 
 
    text = etext.getText().toString(); 
 
    etext.setText(text + "3"); 
 
    } 
 
    public void _4click(View v) { 
 
    text = etext.getText().toString(); 
 
    etext.setText(text + "4"); 
 
    } 
 
    public void _5click(View v) { 
 
    text = etext.getText().toString(); 
 
    etext.setText(text + "5"); 
 
    } 
 
    public void _6click(View v) { 
 
    text = etext.getText().toString(); 
 
    etext.setText(text + "6"); 
 
    } 
 
    public void _7click(View v) { 
 
    text = etext.getText().toString(); 
 
    etext.setText(text + "7"); 
 
    } 
 
    public void _8click(View v) { 
 
    text = etext.getText().toString(); 
 
    etext.setText(text + "8"); 
 
    } 
 
    public void _9click(View v) { 
 
    text = etext.getText().toString(); 
 
    etext.setText(text + "9"); 
 
    } 
 

 
    public void plusClick(View v) { 
 
    vrble = Double.parseDouble(etext.getText().toString()); 
 
    etext.setText(""); 
 
    vrble2 = Double.parseDouble(etext.getText().toString()); 
 
    rslt = vrble + vrble2; 
 
    } 
 
    public void minusClick(View v) { 
 
    vrble = Double.parseDouble(etext.getText().toString()); 
 
    etext.setText(""); 
 
    vrble2 = Double.parseDouble(etext.getText().toString()); 
 
    rslt = vrble - vrble2; 
 
    } 
 
    public void multiplyClick(View v) { 
 
    vrble = Double.parseDouble(etext.getText().toString()); 
 
    etext.setText(""); 
 
    vrble2 = Double.parseDouble(etext.getText().toString()); 
 
    rslt = vrble * vrble2; 
 
    } 
 
    public void divideClick(View v) { 
 
    vrble = Double.parseDouble(etext.getText().toString()); 
 
    etext.setText(""); 
 
    vrble2 = Double.parseDouble(etext.getText().toString()); 
 
    rslt = vrble/vrble2; 
 
    } 
 
    public void clearClick(View v) { 
 
    vrble = 0; 
 
    rslt = 0; 
 
    etext.setText("0"); 
 
    } 
 
    public void equalClick(View v) { 
 
    etext.setText("" + rslt); 
 
    }@ 
 
    Override 
 
    public boolean onCreateOptionsMenu(Menu menu) { 
 

 
    getMenuInflater().inflate(R.menu.main, menu); 
 
    return true; 
 
    } 
 

 
    @ 
 
    Override 
 
    public boolean onOptionsItemSelected(MenuItem item) { 
 

 
    int id = item.getItemId(); 
 
    if (id == R.id.action_settings) { 
 
     return true; 
 
    } 
 
    return super.onOptionsItemSelected(item); 
 
    } 
 
}

Что я должен делать? !!!

+6

Посмотрите на logcat, чтобы получить подробную информацию. Должно быть достаточно ясно, чтобы вы знали, что вызывает проблему. – Distwo

+0

Было бы полезно разместить ваши ошибки LogCat. – Galax

+0

@blackbelt, что он делает с +, правильно. Он получает текущий текст edittext, а затем объединяет число, которое было нажато в конце строки в edittext. Он не делает это как расчет – Boardy

ответ

0

Одна из проблем, которые я могу использовать, - это , которые вы можете получить. Смотрите эти строки в любой из вашего метода, как plusClick():

vrble = Double.parseDouble(etext.getText().toString()); 
etext.setText(""); //setting EditText to empty 
vrble2 = Double.parseDouble(etext.getText().toString()); // parsing the empty string to Double 

Вы устанавливаете eText в "", а затем вы разборе его Double в следующей строке. Это вызывает ошибку. Возможно, вам потребуется очистить свой логин для всех действий, таких как плюс минус и т. Д.

Для получения дополнительной информации о вашем стеке будет полезно.

+0

Спасибо @Shobhit Puri, но вы можете мне помочь предлагая альтернативу движению вперед .... :) –

Смежные вопросы