Android 当我点击一个按钮而不给出输入时,应用程序会立即关闭

Android 当我点击一个按钮而不给出输入时,应用程序会立即关闭,android,Android,当我在没有任何输入的情况下按下按钮时,就会看到这一点。有没有什么方法可以读取值。需要一些建议来改进我的代码。还有我的xml文件。顺便说一句,我是一个初学者。所以请帮助我,这样我可以提高我的编码技能。抱歉这么长的帖子 您正在尝试从空字符串解析整数。将onClick方法修改为如下内容: package com.example.simplecalculator; import android.app.Activity; import android.os.Bundle; import android

当我在没有任何输入的情况下按下按钮时,就会看到这一点。有没有什么方法可以读取值。需要一些建议来改进我的代码。还有我的xml文件。顺便说一句,我是一个初学者。所以请帮助我,这样我可以提高我的编码技能。抱歉这么长的帖子


您正在尝试从空字符串解析整数。将
onClick
方法修改为如下内容:

package com.example.simplecalculator;

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


  public class MainActivity extends Activity {
  Button add,subtract,multiply,divide;
  EditText firstValue,secondValue;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    firstValue=(EditText) findViewById(R.id.editText1);
    secondValue=(EditText) findViewById(R.id.editText2);
    add=(Button)findViewById(R.id.add);

    add.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            final int val1 =    Integer.parseInt(firstValue.getText().toString() );
            final int val2 = Integer.parseInt(secondValue.getText().toString() );
            TextView output= (TextView) findViewById(R.id.answer);
            output.setText(" "+(val1+val2));

}
});
subtract=(Button)findViewById(R.id.subtract);

subtract.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
    final int val1 = Integer.parseInt( firstValue.getText().toString() );
    final int val2 = Integer.parseInt(secondValue.getText().toString() );
    TextView output= (TextView) findViewById(R.id.answer);
    output.setText(" "+(val1-val2));

}
});
multiply=(Button) findViewById(R.id.multiply);
multiply.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
final float val1=Integer.parseInt(firstValue.getText().toString());
final float val2=Integer.parseInt(secondValue.getText().toString());
TextView output=(TextView) findViewById(R.id.answer);
output.setText(""+(val1*val2));
}
});
divide=(Button) findViewById(R.id.divide);
divide.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
final float val1=Integer.parseInt(firstValue.getText().toString());
final float val2=Integer.parseInt(secondValue.getText().toString());
TextView output=(TextView) findViewById(R.id.answer);
output.setText(""+(val1/val2));
        }
    });
}
}

您正在尝试从空字符串解析整数。将
onClick
方法修改为如下内容:

package com.example.simplecalculator;

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


  public class MainActivity extends Activity {
  Button add,subtract,multiply,divide;
  EditText firstValue,secondValue;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    firstValue=(EditText) findViewById(R.id.editText1);
    secondValue=(EditText) findViewById(R.id.editText2);
    add=(Button)findViewById(R.id.add);

    add.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            final int val1 =    Integer.parseInt(firstValue.getText().toString() );
            final int val2 = Integer.parseInt(secondValue.getText().toString() );
            TextView output= (TextView) findViewById(R.id.answer);
            output.setText(" "+(val1+val2));

}
});
subtract=(Button)findViewById(R.id.subtract);

subtract.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
    final int val1 = Integer.parseInt( firstValue.getText().toString() );
    final int val2 = Integer.parseInt(secondValue.getText().toString() );
    TextView output= (TextView) findViewById(R.id.answer);
    output.setText(" "+(val1-val2));

}
});
multiply=(Button) findViewById(R.id.multiply);
multiply.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
final float val1=Integer.parseInt(firstValue.getText().toString());
final float val2=Integer.parseInt(secondValue.getText().toString());
TextView output=(TextView) findViewById(R.id.answer);
output.setText(""+(val1*val2));
}
});
divide=(Button) findViewById(R.id.divide);
divide.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
final float val1=Integer.parseInt(firstValue.getText().toString());
final float val2=Integer.parseInt(secondValue.getText().toString());
TextView output=(TextView) findViewById(R.id.answer);
output.setText(""+(val1/val2));
        }
    });
}
}

在进行任何操作之前,请检查编辑文本是否为空。你可以这样做

public void onClick(View v){
    if(firstValue.getText().toString().isEmpty() || 
            secondValue.getText().toString().isEmpty())
        return;

    final int val1 = Integer.parseInt(firstValue.getText().toString() );
    final int val2 = Integer.parseInt(secondValue.getText().toString() );
    TextView output= (TextView) findViewById(R.id.answer);
    output.setText(" "+(val1-val2));
}
例如,您的
add.setOnClickListener()
应该是

if(!firstValue.getText().toString().matches(""))
{
     //Enters if not empty
     //Perform operations here
}

在进行任何操作之前,请检查编辑文本是否为空。你可以这样做

public void onClick(View v){
    if(firstValue.getText().toString().isEmpty() || 
            secondValue.getText().toString().isEmpty())
        return;

    final int val1 = Integer.parseInt(firstValue.getText().toString() );
    final int val2 = Integer.parseInt(secondValue.getText().toString() );
    TextView output= (TextView) findViewById(R.id.answer);
    output.setText(" "+(val1-val2));
}
例如,您的
add.setOnClickListener()
应该是

if(!firstValue.getText().toString().matches(""))
{
     //Enters if not empty
     //Perform operations here
}

我认为您的logcat抛出
Nullpointer异常。
所以,当您单击按钮而未进行适当验证时,它会崩溃


因此,您需要添加适当的验证。使用isEmpty()来避免此问题我认为您的logcat会引发
空指针异常。
所以,当您单击按钮而未进行适当验证时,它会崩溃


因此,您需要添加适当的验证。请使用isEmpty()避免此问题

请发布logcat输出。不过,我的第一个猜测是,对
Integer.parseInt()
的调用在试图解析空字符串时抛出了一个
NumberFormatException
。另外请注意,在这些情况下,通常从监视logcat输出开始。如果您正在使用IDE,则可以直接从IDE访问它。在命令行中,可以使用
adb logcat
。日志中的异常信息通常会帮助您识别问题。请发布logcat输出。不过,我的第一个猜测是,对
Integer.parseInt()
的调用在试图解析空字符串时抛出了一个
NumberFormatException
。另外请注意,在这些情况下,通常从监视logcat输出开始。如果您正在使用IDE,则可以直接从IDE访问它。在命令行中,可以使用
adb logcat
。日志中的异常信息通常会帮助您识别问题。明白了。我再一次怀疑。我们如何处理android中的异常。我们可以使用相同的try-catch块来处理异常吗是的,是的..你可以像Java一样使用try-catch块。明白了。我再一次怀疑。我们如何处理android中的异常。我们可以使用相同的try-catch块来处理异常吗是的,是的..您可以像Java一样使用try-catch块。谢谢。它帮助了我。但是为什么要在output.setText()中添加空白呢。如果我想使用System.out.println(),可以吗?对不起。作为一个初学者,我对android有很多疑问。其中还有公共静态void main(String[]args){}。执行将从这个位置开始,对吗?在Android上永远不要使用
System.out.println()
,而是使用Log:对于第二个问题,您应该真正了解Android的生命周期状态:Android使用Java,但在运行时做了一些不同的事情。应用程序的起点在Android清单中指定。我在这里找到了Android代码。他在这里使用“System.out.println()”。你能给我解释一下我不知道他为什么那样做,但你真的应该用LogThank。它帮助了我。但是为什么要在output.setText()中添加空白呢。如果我想使用System.out.println(),可以吗?对不起。作为一个初学者,我对android有很多疑问。其中还有公共静态void main(String[]args){}。执行将从这个位置开始,对吗?在Android上永远不要使用
System.out.println()
,而是使用Log:对于第二个问题,您应该真正了解Android的生命周期状态:Android使用Java,但在运行时做了一些不同的事情。应用程序的起点在Android清单中指定。我在这里找到了Android代码。他在这里使用“System.out.println()”。你能给我解释一下我不知道他为什么这样做,但你真的应该用Log