为什么我的应用程序在实现按钮时崩溃了?

时间:2017-06-17 05:17:38

标签: java android crash

以下是代码......每当我在手机上启动应用程序时,它就会崩溃。我是android编程的新手,所以请帮助我。在做了一些测试后,问题就出来了,当我将值分配给Value1和Value2时(只是一个猜测)。请帮帮我。提前致谢。 MainActivity.java

package com.example.android.add;


import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    EditText num1;
    EditText num2;
    Button add;
    TextView ans;
    int Value1;
    int Value2;
    int result;

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

        num1 = (EditText) findViewById(R.id.et1);
        num2 = (EditText) findViewById(R.id.et2);
        Value1 = Integer.parseInt(num1.getText().toString());
        Value2 = Integer.parseInt(num2.getText().toString());
        add = (Button) findViewById(R.id.addBtn);
        ans = (TextView) findViewById(R.id.tv);

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                result = Value1 + Value2;

                ans.setText(""+result);


            }

        });
    }

} 

和activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.android.add.MainActivity">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:hint="Number 1"
        android:textSize="40dp"
        android:id="@+id/et1"
        android:inputType="number"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:layout_below="@+id/et1"
        android:hint="Number 2"
        android:textSize="40dp"
        android:id="@+id/et2"
        android:inputType="number"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/addBtn"
        android:layout_below="@+id/et2"
        android:text="Add"
        android:layout_marginLeft="80dp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Your Answer"
        android:textSize="40dp"
        android:layout_margin="40dp"
        android:layout_below="@+id/addBtn"
        android:id="@+id/tv"/>

</RelativeLayout>

7 个答案:

答案 0 :(得分:1)

在插入之前获得值,当用户单击“添加”按钮并获取onClickListner内的value1和value2时,必须获取值

根据这个改变

package com.example.android.add;


import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
EditText num1;
EditText num2;
Button add;
TextView ans;
int Value1;
int Value2;
int result;

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

    num1 = (EditText) findViewById(R.id.et1);
    num2 = (EditText) findViewById(R.id.et2);

    add = (Button) findViewById(R.id.addBtn);
    ans = (TextView) findViewById(R.id.tv);

    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Value1 = Integer.parseInt(num1.getText().toString());
            Value2 = Integer.parseInt(num2.getText().toString());

            result = Value1 + Value2;

            ans.setText(""+result);


        }

    });
   }
 }

答案 1 :(得分:1)

Value1 = Integer.parseInt(num1.getText().toString());
Value2 = Integer.parseInt(num2.getText().toString());

此代码写入onClick() method.remove from onCreate() method。

答案 2 :(得分:1)

编辑您的代码,它应该运行。问题是您在EditText s中过早地访问了值,甚至在您输入任何值之前。您需要在onClick()按钮

add中访问这些值
package com.example.android.add;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

    EditText num1;
    EditText num2;
    Button add;
    TextView ans;
    int Value1;
    int Value2;
    int result;

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

        num1 = (EditText) findViewById(R.id.et1);
        num2 = (EditText) findViewById(R.id.et2);

        // If you get value from edit texts here it will cause an error 
        // since onCreate is called at the very beginning and you have not 
        // entered any value in num1 and num2.

        add = (Button) findViewById(R.id.addBtn);
        ans = (TextView) findViewById(R.id.tv);

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            // Instead put them here so you have already inserted values 
            // into the fields before they are retrieved:

            Value1 = Integer.parseInt(num1.getText().toString());
            Value2 = Integer.parseInt(num2.getText().toString());

            result = Value1 + Value2;

            ans.setText(""+result);

            }

        });
    }

} 

答案 3 :(得分:0)

不鼓励使用OnClickListeners按钮。它已被XML属性替换:android:onClick = "functionName"

此功能将在附加布局文件的Activity中实现。因此,在MainActivity.java

public void funcitonName(View v){ // your code here }

答案 4 :(得分:0)

可能存在许多字符串转换错误

Value1 = 0;

try {
    Value1 = Integer.parseInt(num1.getText().toString());
} catch(NumberFormatException nfe) {
   System.out.println("Could not parse " + nfe);
} 

答案 5 :(得分:0)

逻辑是:在Button的点击监听器上获取编辑文本值

 add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
         try{
        Value1 = Integer.parseInt(num1.getText().toString());
        Value2 = Integer.parseInt(num2.getText().toString());
        result = Value1 + Value2;
            result = Value1 + Value2;

            ans.setText(""+result);
        }
     catch(Exception e)
     {
           Log.e("Exc",e.toString());      
     }


        }

    });

答案 6 :(得分:0)

由于这两行

,您的应用程序崩溃了
Value1 = Integer.parseInt(num1.getText().toString());
Value2 = Integer.parseInt(num2.getText().toString());

这里你试图将字符串转换为整数,其中string为null如果editText没有ant值,则在typeCasting时会出现这种类型的错误,所以在typeCasting之前添加验证,如下所示

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        num1 = (EditText) findViewById(R.id.et1);
        num2 = (EditText) findViewById(R.id.et2);
        add = (Button) findViewById(R.id.addBtn);
        ans = (TextView) findViewById(R.id.tv);

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(num1.getText() != null && num2.getText() != null) {
                    Value1 = Integer.parseInt(num1.getText().toString());
                    Value2 = Integer.parseInt(num2.getText().toString());
                    result = Value1 + Value2;

                    ans.setText("" + result);
                }


            }

        });
    }