2014-11-12 2 views
-4

저는 Android에서 매우 간단한 계산기를 만들고 있습니다. 이클립스에서 오류를 표시하지 않지만 안드로이드 장치에서 실행할 때 버튼을 클릭하면 작동하지 않습니다.이 코드가 작동하지 않는 이유는 무엇입니까?

<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>

가 여기 내 자바 코드입니다 :

여기 내 XML입니까?

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

내가 할 수있는 한 가지 문제점은받을 수있는 NumberFormatException입니다. 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에게 감사하지만, 나를 도와 줄 수 있니? 앞으로 나아갈 대안을 제안 .... –

관련 문제