2011-04-10 2 views
0

보기 및 레이아웃에 대해 배울 올바른 방향으로 지적한 후 나는 꽤 멋지게 보인다고 생각하는 계산기를 만들 수있었습니다. 나는 아주 기본적인 코드를 추가했는데 그 코드는 우아하지 않지만 간단한 앱을 만들 때 내 피트가 젖어 가고 싶었다. 지금까지는 프로세스가 중단되었다고 말하면서 만 다시 시도합니다. 나는 이것이 나의 코드이고 100 % 긍정적 인 것이라고 생각한다. 나는 1 버튼으로 기본 버튼 카운터를 만들 수 있었지만 지금은 15 버튼으로 내가 망쳤다고 생각한다. 여기 프로세스가 중지되었습니다. 안드로이드 계산기 trouples

는 계산기라는 hellocats에 대한 코드 (i가 기본 튜토리얼을 다음과 추가에 보관했다) 여기
package com.hellocats; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.*; 
public class hellokatz extends Activity { 
    /** Called when the activity is first created. */ 



int numbermover = 0; 
int[] numbers ={0,0}; 

String sign = "+"; 
int answer; 




@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    final Button button0 = (Button)findViewById(R.id.button0); 
    final Button button1 = (Button)findViewById(R.id.button1); 
    final Button button2 = (Button)findViewById(R.id.button2); 
    final Button button3 = (Button)findViewById(R.id.button3); 
    final Button button4 = (Button)findViewById(R.id.button4); 
    final Button button5 = (Button)findViewById(R.id.button5); 
    final Button button6 = (Button)findViewById(R.id.button6); 
    final Button button7 = (Button)findViewById(R.id.button7); 
    final Button button8 = (Button)findViewById(R.id.button8); 
    final Button button9 = (Button)findViewById(R.id.button9); 

    final Button addbutton = (Button)findViewById(R.id.buttonadd); 
    final Button subbutton = (Button)findViewById(R.id.buttonsub); 
    final Button mulbutton = (Button)findViewById(R.id.buttonmul); 
    final Button divbutton = (Button)findViewById(R.id.buttondiv); 
    final Button equalsbutton = (Button)findViewById(R.id.buttonequals); 

    final EditText num1 = (EditText)findViewById(R.id.num1); 
    final EditText mysign = (EditText)findViewById(R.id.sign); 
    final EditText num2 = (EditText)findViewById(R.id.num2); 
    final EditText answer = (EditText)findViewById(R.id.answer); 


    num1.setText(numbers[0]); 
    num2.setText(numbers[1]); 
    mysign.setText(sign); 


    button0.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      numbers[numbermover] = 0; 

     } 
    }); 
     button1.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      numbers[numbermover] = 1; 

     } 
    }); 
     button2.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      numbers[numbermover] = 2; 

     } 
    }); 
     button3.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      numbers[numbermover] = 3; 

     } 
    }); 
     button4.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      numbers[numbermover] = 4; 

     } 
    }); 
     button5.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      numbers[numbermover] = 5; 

     } 
    }); 
     button6.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      numbers[numbermover] = 6; 

     } 
    }); 
     button7.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      numbers[numbermover] = 7; 

     } 
    }); 
     button8.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      numbers[numbermover] = 8; 

     } 
    }); 
     button9.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      numbers[numbermover] = 9; 

     } 
     }); 


} 
} 

레이아웃 파일 main.xml에입니다

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" android:orientation="vertical"> 
    <TableRow android:id="@+id/tableRow1" android:layout_height="wrap_content" android:layout_width="match_parent"> 
     <Button android:text=" 1 " android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
     <Button android:text=" 2 " android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
     <Button android:text=" 3 " android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
     <Button android:text=" 4 " android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
     <Button android:text=" 5 " android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
     <Button android:text=" 6 " android:id="@+id/button6" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
     <Button android:text=" 7 " android:id="@+id/button7" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
    </TableRow> 
    <TableRow android:id="@+id/tableRow2" android:layout_height="wrap_content" android:layout_width="match_parent"> 
     <Button android:text=" 8 " android:id="@+id/button8" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
     <Button android:text=" 9 " android:id="@+id/button9" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
     <Button android:text=" 0 " android:id="@+id/button0" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
    </TableRow> 
    <TableRow android:id="@+id/tableRow3" android:layout_height="wrap_content" android:layout_width="match_parent"> 
     <Button android:text=" + " android:id="@+id/buttonadd" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
     <Button android:text=" - " android:id="@+id/buttonsub" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
     <Button android:text=" X " android:id="@+id/buttonmul" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
     <Button android:text="/ " android:id="@+id/buttondiv" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
     <Button android:text=" = " android:id="@+id/buttonequals" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
    </TableRow> 
    <TableRow android:id="@+id/tableRow4" android:layout_height="wrap_content" android:layout_width="match_parent"> 
     <EditText android:text="  " android:id="@+id/num1" android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText> 
     <EditText android:text="  " android:id="@+id/sign" android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText> 
     <EditText android:text="  " android:id="@+id/num2" android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText> 
     <EditText android:text=" = " android:id="@+id/equals" android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText> 
     <EditText android:text="  " android:id="@+id/answer" android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText> 
    </TableRow> 
</LinearLayout> 

내가 무엇을했는지 내 Java 파일에서 잘못 수행하고 어떻게 수정하고이를 통해 배울 수 있습니까?

고마워요. 저는 웹 사이트를 처음 접해 왔으며, 지금까지 main.xml 파일을 게시하는 방법을 알지 못합니다. 나는 CTRL + K와 CTRL + V를 지나쳐서 시도해 보았습니다.

+0

검사 (그 라인들이 프로세스가 중지 이유에 대한 정보를 제공, 스택 추적있다) – bigstones

+0

또한, 코드의 형식을 당신이 그것을 선택해야합니다 (모든 줄이 코드 블록이라면), 편집기 맨 위의'{}'버튼을 클릭하십시오. – bigstones

+0

DDMS/로그 캣 로그를 게시 할 수 있습니까? 그리고'TableLayout'없이 'LinearLayout'에서 직접'TableRow'를 사용하는 이유는 무엇입니까? – GrAnd

답변

3

배열 int[] numbers ={0,0};int입니다.

num1.setText(numbers[0]); 

같은 것을 봅니다 (작동하는 경우, 다음 num2mysign을 업데이트) :

num1.setText(Integer.toString(numbers[0])); 

EditTextCharSequence 그래서 내 생각 엔이 아래 줄에서 오류가 발생한다는 것입니다 받아

그림에서 알 수 있듯이 setTextresourceId을 나타내는 int을 사용할 수 있습니다. ID가 0 인 문자열을 찾을 수 없습니다. 오류가 발생했습니다. 일부 붉은 선 당신의 로그 캣에서 그들을 포함하도록 게시물을 수정하기위한

+3

동의하지만 설명이 약간 다릅니다. 'TextView'로부터 상속받은 두 번째'setText'가 있는데, 정수를'resourceID'로 받아들입니다. 그리고 확실히'id = 0'을 가진 자원을 찾을 수 없었습니다. 그리고 자바는 정수를 허용하는 메소드의 존재 때문에'CharSequence'에 자동으로 캐스팅하지 않았습니다. – GrAnd

+0

아하, 내가 보았을 때'public final void setText (int resid)'메소드를 보지 못했습니다. 그것을 지적 주셔서 감사합니다. – ccheneson

+0

도움을 주셔서 감사합니다. 이제 실제로 내가 만든 것을 보여줍니다. 이제 텍스트가 초기 값 이외의 다른 숫자로 설정되지 않는 논리 오류를 수정해야합니다. – user700508

0
package com.example.showoff; 

import java.util.ArrayList; 
import android.os.Bundle; 
import android.app.Activity; 
import android.text.method.DigitsKeyListener; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 


import android.widget.TextView; 

public class MainActivity extends Activity { 

     TextView edittest1; 

     Button one, two, three, four, five, six, seven, eight, nine, zero, add, sub, mul, div, cancel, equal; 

     ArrayList<Float> math = new ArrayList<Float>(); 
     float m1; 
     float m2; 
     float temp; 

     int currentOperation = 0; 
     int nextOperation; 

     final static int ADD = 1; 
     final static int SUBTRACT = 2; 
     final static int MULTIPLY =3; 
     final static int DIVISION = 4; 
     final static int EQUALS = 0; 
     final static int CLEAR = 1; 
     final static int DONT_CLEAR = 0; 
     int clearDisplay = 0; 




    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

      edittest1 = (TextView) findViewById(R.id.edittest1); 



      one =(Button) findViewById(R.id.btnNum1Id); 
      two =(Button) findViewById(R.id.btnNum2Id); 
      three =(Button) findViewById(R.id.btnNum3Id); 
      four =(Button) findViewById(R.id.btnNum4Id); 
      five =(Button) findViewById(R.id.btnNum5Id); 
      six =(Button) findViewById(R.id.btnNum6Id); 
      seven =(Button) findViewById(R.id.btnNum7Id); 
      eight =(Button) findViewById(R.id.btnNum8Id); 
      nine =(Button) findViewById(R.id.btnNum9Id); 
      zero =(Button) findViewById(R.id.btnNum0Id); 
      add =(Button) findViewById(R.id.btnNumAddId); 
      sub =(Button) findViewById(R.id.btnNumSubId); 
      mul =(Button) findViewById(R.id.btnNumMulId); 
      div =(Button) findViewById(R.id.btnNumDivId); 
      cancel =(Button) findViewById(R.id.btnNumClearId); 
      equal =(Button) findViewById(R.id.btnNumEqualId); 

      edittest1.setKeyListener(DigitsKeyListener.getInstance(true,true)); 

      registerListeners(); 


    } 

    private void registerListeners() { 
     // TODO Auto-generated method stub 

     one.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       if (clearDisplay == CLEAR) { 
        edittest1.setText(""); 
       } 
       clearDisplay = DONT_CLEAR; 
       edittest1.append("1"); 

      } 
     }); 

     two.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       if (clearDisplay == CLEAR) { 
        edittest1.setText(""); 
       } 
       clearDisplay = DONT_CLEAR; 
       edittest1.append("2"); 

      } 
     }); 

     three.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       if (clearDisplay == CLEAR) { 
        edittest1.setText(""); 
       } 
       clearDisplay = DONT_CLEAR; 
       edittest1.append("3"); 

      } 
     }); 
     four.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       if (clearDisplay == CLEAR) { 
        edittest1.setText(""); 
       } 
       clearDisplay = DONT_CLEAR; 
       edittest1.append("4"); 

      } 
     }); 

     five.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       if (clearDisplay == CLEAR) { 
        edittest1.setText(""); 
       } 
       clearDisplay = DONT_CLEAR; 
       edittest1.append("5"); 

      } 
     }); 

     six.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       if (clearDisplay == CLEAR) { 
        edittest1.setText(""); 
       } 
       clearDisplay = DONT_CLEAR; 
       edittest1.append("6"); 

      } 
     }); 
     seven.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       if (clearDisplay == CLEAR) { 
        edittest1.setText(""); 
       } 
       clearDisplay = DONT_CLEAR; 
       edittest1.append("7"); 

      } 
     }); 

     eight.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       if (clearDisplay == CLEAR) { 
        edittest1.setText(""); 
       } 
       clearDisplay = DONT_CLEAR; 
       edittest1.append("8"); 

      } 
     }); 

     nine.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       if (clearDisplay == CLEAR) { 
        edittest1.setText(""); 
       } 
       clearDisplay = DONT_CLEAR; 
       edittest1.append("9"); 

      } 
     }); 
     zero.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (clearDisplay == CLEAR) { 
        edittest1.setText(""); 
       } 
       clearDisplay = DONT_CLEAR; 
       edittest1.append("0"); 

      } 
     }); 
     add.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 


       calcLogic(ADD); 
       } 
      } 
     ); 

     sub.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       calcLogic(SUBTRACT); 
      }    
     }); 
     mul.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       calcLogic(MULTIPLY); 

      } 
     }); 
     div.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       calcLogic(DIVISION);      
      } 
     }); 
     equal.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       calcLogic(EQUALS); 

      } 
     }); 
     cancel.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       edittest1.setText("0"); 
       m1 = 0; 
       m2 = 0; 
       math.removeAll(math); 
       currentOperation = 0; 
       nextOperation = 0;  

      } 
     }); 


} 
    private void calcLogic(int operator){ 
     math.add(Float.parseFloat(edittest1.getText().toString())); 

      if (operator != EQUALS) { 
       nextOperation = operator; 
      } 
      else if (operator == EQUALS){ 
       nextOperation = 0; 
       //operator=' '; 
      } 



      switch (currentOperation) { 
      case ADD:    
       m1 = math.get(0); 
       m2 = math.get(1); 

       math.removeAll(math); 

       math.add(m1 + m2); 


       edittest1.setText(String.format("%.3f", math.get(0))); 



       break; 
      case SUBTRACT: 
       m1 = math.get(0); 
       m2 = math.get(1); 

       math.removeAll(math); 

       math.add(m1 - m2); 

       edittest1.setText(String.format("%.3f", math.get(0))); 
       break; 
      case MULTIPLY: 
       m1 = math.get(0); 
       m2 = math.get(1); 

       math.removeAll(math); 

       math.add(m1 * m2); 

       edittest1.setText(String.format("%.3f", math.get(0))); 
       break; 
      case DIVISION: 
       m1 = math.get(0); 
       m2 = math.get(1); 

       math.removeAll(math); 

       math.add(m1/m2); 

       edittest1.setText(String.format("%.3f", math.get(0))); 
       break; 
      } 

      clearDisplay = CLEAR; 
      currentOperation = nextOperation; 
      if (operator == EQUALS) { 
       m1 = 0; 
       m2 = 0; 
       math.removeAll(math); 
      } 

     } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 

}