应用程序在android中意外停止 [英] the application has stopped unexpectedly in android

查看:104
本文介绍了应用程序在android中意外停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的计算器应用程序。

当我执行应用程序时,我收到错误

I have created a simple calculator application.
When I execute the application, I am getting an error

The application has stopped unexpectedly in android' Please try again later.





我的XML文件:



My XML file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent">
<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:id="@+id/linearLayout1"

android:layout_marginLeft="10pt"

android:layout_marginRight="10pt"

android:layout_marginTop="3pt">
<EditText

android:layout_weight="1"

android:layout_height="wrap_content"

android:layout_marginRight="5pt"

android:id="@+id/etNum1"

android:layout_width="match_parent"

android:inputType="numberDecimal">
</EditText>
<EditText

android:layout_height="wrap_content"

android:layout_weight="1"

android:layout_marginLeft="5pt"

android:id="@+id/etNum2"

android:layout_width="match_parent"

android:inputType="numberDecimal">
</EditText>
</LinearLayout>
<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:id="@+id/linearLayout2"

android:layout_marginTop="3pt"

android:layout_marginLeft="5pt"

android:layout_marginRight="5pt">
<Button

android:layout_height="wrap_content"

android:layout_width="match_parent"

android:layout_weight="1"

android:text="+"

android:textSize="8pt"

android:id="@+id/btnAdd">
</Button>
<Button

android:layout_height="wrap_content"

android:layout_width="match_parent"

android:layout_weight="1"

android:text="-"

android:textSize="8pt"

android:id="@+id/btnSub">
</Button>
<Button

android:layout_height="wrap_content"

android:layout_width="match_parent"

android:layout_weight="1"

android:text="*"

android:textSize="8pt"

android:id="@+id/btnMult">
</Button>
<Button

android:layout_height="wrap_content"

android:layout_width="match_parent"

android:layout_weight="1"

android:text="/"

android:textSize="8pt"

android:id="@+id/btnDiv">
</Button>
</LinearLayout>
<TextView

android:layout_height="wrap_content"

android:layout_width="match_parent"

android:layout_marginLeft="5pt"

android:layout_marginRight="5pt"

android:textSize="12pt"

android:layout_marginTop="3pt"

android:id="@+id/tvResult"

android:gravity="center_horizontal">
</TextView>
</LinearLayout>





我的Java文件:



My Java file:

package com.example.calculator;

import android.os.Bundle;
import android.app.Activity;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	  EditText etNum1;
	  EditText etNum2;

	  Button btnAdd;
	  Button btnSub;
	  Button btnMult;
	  Button btnDiv;

	  TextView tvResult;

	  String oper = "";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		// find the elements
	    etNum1 = (EditText) findViewById(R.id.etNum1);
	    etNum2 = (EditText) findViewById(R.id.etNum2);

	    btnAdd = (Button) findViewById(R.id.btnAdd);
	    btnSub = (Button) findViewById(R.id.btnSub);
	    btnMult = (Button) findViewById(R.id.btnMult);
	    btnDiv = (Button) findViewById(R.id.btnDiv);

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

	    // set a listener
	    btnAdd.setOnClickListener((OnClickListener) this);
	    btnSub.setOnClickListener((OnClickListener) this);
	    btnMult.setOnClickListener((OnClickListener) this);
	    btnDiv.setOnClickListener((OnClickListener) this);
	}

	@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;
	}
	
	
	  public void onClick(View v) {
		  
	    // TODO Auto-generated method stub
	    float num1 = 0;
	    float num2 = 0;
	    float result = 0;

	    // check if the fields are empty
	    if (TextUtils.isEmpty(etNum1.getText().toString())
	        || TextUtils.isEmpty(etNum2.getText().toString())) {
	      return;
	    }

	    // read EditText and fill variables with numbers
	    num1 = Float.parseFloat(etNum1.getText().toString());
	    num2 = Float.parseFloat(etNum2.getText().toString());

	    // defines the button that has been clicked and performs the corresponding operation
	    // write operation into oper, we will use it later for output
	    switch (v.getId()) {
	    case R.id.btnAdd:
	      oper = "+";
	      result = num1 + num2;
	      break;
	    case R.id.btnSub:
	      oper = "-";
	      result = num1 - num2;
	      break;
	    case R.id.btnMult:
	      oper = "*";
	      result = num1 * num2;
	      break;
	    case R.id.btnDiv:
	      oper = "/";
	      result = num1 / num2;
	      break;
	    default:
	      break;
	    }

	    // form the output line
	    tvResult.setText(num1 + " " + oper + " " + num2 + " = " + result);
	  
	  }
}





请帮帮我。在此先感谢



Please help me. Thanks in advance

推荐答案

你几乎就在那里。但有两种补救措施:

1.将android:onClick =onClick添加到xml布局中的所有Button元素中,如下所示:

You are almost there. But two remedies:
1. Add "android:onClick="onClick" to all the Button elements in the xml layout, like this:
android:textSize="8pt"
android:onClick="onClick"
android:id="@+id/btnAdd">



2.在MainActivity中注释掉这些:


2. Comment out these in the MainActivity:

//btnAdd.setOnClickListener((OnClickListener) this);
//btnSub.setOnClickListener((OnClickListener) this);
//btnMult.setOnClickListener((OnClickListener) this);
//btnDiv.setOnClickListener((OnClickListener) this);



你很顺利... Bon Voyage。


You are well on your way... Bon Voyage.


如果你想通过代码设置你的监听器,你必须实现接口

if you want to set your listener by code, you have to implement the interface
extends Activity implements OnClickListener



Eclipse将自动执行omatically生成覆盖方法:


Eclipse will automatically generate the override method:

@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}





然后你的代码可以工作:



Then you code can work:

btnAdd.setOnClickListener(this);
        btnSub.setOnClickListener(this);
        btnMult.setOnClickListener(this);
        btnDiv.setOnClickListener((this);


这篇关于应用程序在android中意外停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆