无法在 android 模拟器上 运行 应用程序,应用程序一直停止
unable to run app on android emulator, App keeps stopping
package com.example.android.interestcalculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText amountEditText;
EditText rupeePerHundred;
Button calculateButton;
TextView resultTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
//error here calculateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String amountString = amountEditText.getText().toString();
String rupeePerHundredString = rupeePerHundred.getText().toString();
if (amountString.isEmpty() & rupeePerHundredString.isEmpty()) {
Toast.makeText(MainActivity.this, " input a value", Toast.LENGTH_SHORT).show();
} else {
int result = calculateInterest(amountString, rupeePerHundredString);
displayResult(result);
}
}
});
}
private void displayResult(int result) {
resultTextView.setText(result);
}
private int calculateInterest(String amountString, String rupeePerHundredString) {
int amount = Integer.parseInt(amountString);
int rupees = Integer.parseInt(rupeePerHundredString);
return amount / 100 * rupees;
}
private void findViews() {
amountEditText = findViewById(R.id.edit_text_amount);
rupeePerHundred = findViewById(R.id.edit_text_rupee_per_hundred);
resultTextView = findViewById(R.id.text_view_result);
}
}
LOGCAT:
2021-07-18 18:01:16.942 5655-5655/com.example.android.interestcalculator E/AndroidRuntime:致命异常:main
过程:com.example.android.interestcalenter image description hereculator,PID:5655
java.lang.RuntimeException: 无法启动 activity ComponentInfo{com.example.android.interestcalculator/com.example.android.interestcalculator.MainActivity}: java.lang.NullPointerException: 尝试调用虚拟空对象引用 上的方法 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)'
在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
在 android.app.ActivityThread.-wrap12(ActivityThread.java)
在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
在 android.os.Handler.dispatchMessage(Handler.java:102)
在 android.os.Looper.loop(Looper.java:154)
在 android.app.ActivityThread.main(ActivityThread.java:6077)
在 java.lang.reflect.Method.invoke(本机方法)
在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
由以下原因引起:java.lang.NullPointerException:尝试在空对象引用上调用虚方法 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)'
在com.example.android.interestcalculator.MainActivity.onCreate(MainActivity.java:25)
在 android.app.Activity.performCreate(Activity.java:6662)
在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
在 android.app.ActivityThread.-wrap12(ActivityThread.java)
在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
在 android.os.Handler.dispatchMessage(Handler.java:102)
在 android.os.Looper.loop(Looper.java:154)
在 android.app.ActivityThread.main(ActivityThread.java:6077)
在 java.lang.reflect.Method.invoke(本机方法)
在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) **
你应该 findViewById
为你的 Button calculateButton
在 findViews()
中设置此代码
calculateButton = findViewById(R.id.calculateButton);
package com.example.android.interestcalculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText amountEditText;
EditText rupeePerHundred;
Button calculateButton;
TextView resultTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
//error here calculateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String amountString = amountEditText.getText().toString();
String rupeePerHundredString = rupeePerHundred.getText().toString();
if (amountString.isEmpty() & rupeePerHundredString.isEmpty()) {
Toast.makeText(MainActivity.this, " input a value", Toast.LENGTH_SHORT).show();
} else {
int result = calculateInterest(amountString, rupeePerHundredString);
displayResult(result);
}
}
});
}
private void displayResult(int result) {
resultTextView.setText(result);
}
private int calculateInterest(String amountString, String rupeePerHundredString) {
int amount = Integer.parseInt(amountString);
int rupees = Integer.parseInt(rupeePerHundredString);
return amount / 100 * rupees;
}
private void findViews() {
amountEditText = findViewById(R.id.edit_text_amount);
rupeePerHundred = findViewById(R.id.edit_text_rupee_per_hundred);
resultTextView = findViewById(R.id.text_view_result);
}
}
LOGCAT:
2021-07-18 18:01:16.942 5655-5655/com.example.android.interestcalculator E/AndroidRuntime:致命异常:main 过程:com.example.android.interestcalenter image description hereculator,PID:5655 java.lang.RuntimeException: 无法启动 activity ComponentInfo{com.example.android.interestcalculator/com.example.android.interestcalculator.MainActivity}: java.lang.NullPointerException: 尝试调用虚拟空对象引用 上的方法 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 在 android.app.ActivityThread.-wrap12(ActivityThread.java) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:154) 在 android.app.ActivityThread.main(ActivityThread.java:6077) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 由以下原因引起:java.lang.NullPointerException:尝试在空对象引用上调用虚方法 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' 在com.example.android.interestcalculator.MainActivity.onCreate(MainActivity.java:25) 在 android.app.Activity.performCreate(Activity.java:6662) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 在 android.app.ActivityThread.-wrap12(ActivityThread.java) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:154) 在 android.app.ActivityThread.main(ActivityThread.java:6077) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) **
你应该 findViewById
为你的 Button calculateButton
在 findViews()
calculateButton = findViewById(R.id.calculateButton);