[英]Why does my app crashes while implementing button?
這是代碼...每當我在手機上啟動應用程序時,它就會崩潰。 我是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>
您需要在插入之前獲取值,當用戶單擊添加按鈕並在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);
}
});
}
}
Value1 = Integer.parseInt(num1.getText().toString());
Value2 = Integer.parseInt(num2.getText().toString());
這段代碼寫入onClick()
onCreate()
方法。從onCreate()
方法中刪除。
編輯您的代碼,它應該運行。 問題是您訪問EditText
的值還為時過早,甚至沒有在其中輸入任何值。 您需要在add
按鈕的onClick()
中訪問這些值
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);
}
});
}
}
不建議將OnClickListeners用於按鈕。 它已替換為XML屬性: android:onClick = "functionName"
。
並且此功能將在布局文件所附加到的活動中實現。 因此,在MainActivity.java
:
public void funcitonName(View v){ // your code here }
可能存在許多字符串轉換錯誤
Value1 = 0;
try {
Value1 = Integer.parseInt(num1.getText().toString());
} catch(NumberFormatException nfe) {
System.out.println("Could not parse " + nfe);
}
邏輯是:在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());
}
}
});
由於這兩行,您的應用程序崩潰了
Value1 = Integer.parseInt(num1.getText().toString());
Value2 = Integer.parseInt(num2.getText().toString());
在這里,您嘗試將字符串轉換為整數,如果editText沒有ant值,則字符串為null,這些類型的錯誤在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);
}
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.