2014-01-16 2 views
-2

Простой calc, для каждой кнопки я должен определить значение, могу ли я получить его прямо от кнопки? Его простое приложение, но когда я хочу его протестировать, я получаю сообщение об ошибке «приложение остановлено». Я использую один прослушиватель для всех кнопок, я думаю, что это вызывает ошибку.Простой calc остановился, как я могу сделать это лучше?

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

} 

расположение:

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

консоль:

[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) 
+5

Сообщите свой логарифм. – 323go

+1

где 'buttOne.setOnClickListener (this)' для всех ваших кнопок? – Raghunandan

+0

Опубликовать xml layout alos coz findViewById может возвращать null – Raghunandan

ответ

0

Вы должны добавить onClickListeners.

buttOne.setOnClickListener(this); 

и так далее ... :)

+0

Я хочу добавить это после изменения, я установил всех слушателей, когда у меня была ошибка, я обновляю код – kxyz

+0

Вы уверены, что не закрыли ваш RelativeLayout? –

+0

Да, закрыто kxyz

0

this Может быть, это для вас. Используйте андроид: тег, как предлагается в ответе OcuS.

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