2014-02-13 72 views
0

我是新來的應用程序製作,並且正在製作一個簡單的計算器。當我按下我製作的應用程序上的一個按鈕時,應用程序關閉,它說不幸的是應用程序已關閉

每當我點擊一個按鈕,我就會收到消息「不幸的是,計算器已經停止。」任何幫助將不勝感激。

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=".Calculator" > 

<EditText 
    android:id="@+id/editText1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginLeft="70dp" 
    android:layout_marginTop="25dp" 
    android:ems="10" > 

    <requestFocus /> 
</EditText> 

<EditText 
    android:id="@+id/editText2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/editText1" 
    android:layout_marginLeft="70dp" 
    android:layout_marginTop="26dp" 
    android:ems="10" /> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_centerVertical="true" 
    android:layout_marginLeft="20dp" 
    android:text="Add" 
    android:onClick="DoAdd" /> 

<Button 
    android:id="@+id/button2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_centerVertical="true" 
    android:layout_marginLeft="90dp" 
    android:text="Subtract" 
    android:onClick="DoMinus"/> 

<Button 
    android:id="@+id/button3" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_centerVertical="true" 
    android:layout_toRightOf="@+id/button2" 
    android:onClick="DoTimes" 
    android:text="Multiply" /> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/button2" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="94dp" 
    android:text="TextView" /> 

Java文件:

package com.example.calculator; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.TextView; 

public class Calculator extends Activity { 

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

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

public void doAdd(View v) 
{ 
    EditText firstNumber = (EditText)findViewById(R.id.editText1); 
    EditText secondNumber = (EditText)findViewById(R.id.editText2); 

    double x = Double.parseDouble(firstNumber.getText().toString()); 
    double y = Double.parseDouble(secondNumber.getText().toString()); 

    double total = x + y; 

    TextView answerSpace = (TextView)findViewById(R.id.textView1); 
    answerSpace.setText(Double.toString(total)); 
} 
} 

三江源。

+0

堆棧跟蹤在哪裏? – nKn

+0

android:onClick =「DoAdd」給你的錯誤,因爲它不存在於你的java代碼中,小心上/下殼android:onClick =「doAdd」 – Aiapaec

回答

3

更改XML onClick屬性正好在你的java代碼的方法名稱相匹配:

<Button 
android:id="@+id/button1" 
... 
android:onClick="doAdd" /> 

注意開頭小寫d

如果這兩者不匹配,則應用會在Android嘗試調用您指定的onClick方法時崩潰。

+0

Thankyou,that worked :) great help :) – user3307598

0
FD_ already pointed out the mistake from your code. 

But for better coding approach you can follow this. If you wish. 

In your xml file, define single method name for onClick on each button ie for 
addition,substration,division,multiplication. 

What i mean to say...? 

// For Addition.... 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_centerVertical="true" 
    android:layout_marginLeft="20dp" 
    android:text="Add" 
    android:onClick="calculate" /> 

//Define android:onClick="calculate" for remaining 3 buttons 

ie say for subtraction 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_centerVertical="true" 
    android:layout_marginLeft="20dp" 
    android:text="Subract" 
    android:onClick="calculate" /> 

do similarly for division and multiplication 


Now, in Java File 

1)Define Variables globally 
2)Find their ids in onCreateMethod(), ie when the activity created. 

How to do this? 


public class Calculator extends Activity 
{ 
    EditText firstNumber,secondNumber; 

    onCreate() 
    { 
    firstNumber = (EditText)findViewById(R.id.editText1); 
    secondNumber = (EditText)findViewById(R.id.editText2); 
    } 

    public void calculate(View v) 
    { 

    switch(v.getId()) 
     { 

     case R.id.button1: 
       // Do coding here addition.... 
       break; 
      case R.id.button2: 
       // Do coding here substraction.... 
       break; 
      case R.id.button3: 
       // Do coding here multiplication.... 
       break; 
      case R.id.button4: 
       // Do coding here division.... 
       break; 

     } 
    } 


} 
相關問題