无法在android模拟器上运行应用程序,应用程序不断停止


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此处的图像描述计算器,PID:5565java.lang.RuntimeException:无法启动活动ComponentInfo{com.example.android.enterestcalculator/com.example.andro.interestcalculator.MainActivity}:java.lang.NullPointerException:尝试在android.app.ActivityThread.performLaunchActivity的空对象引用上调用虚拟方法"void android.view.view.setOnClickListener(android.view.view$OnClickListen(">(ActivityThread.java:2646(在android.app.ActivityThread.handleLaunchActivity(ActivityThreads.java:2707(在android.app.ActivityThread.-wrap12(ActivityThreads.java(在android.app.ActivityThread$H.handleMessage(ActivityThreads.java:1460(在android.os.Handler.dispatchMessage(Handler.java:102(在android.os.Looper.loop(Looper.java:154(在android.app.ActivityThread.main(ActivityThreads.java:6077(位于java.lang.reflect.Method.ioke(本机方法(网址:com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866(网址:com.android.internal.os.ZygoteInit.main(ZygoteNit.java:756(引起原因:java.lang.NullPointerException:试图在null对象引用上调用虚拟方法"void android.view.view.setOnClickListener(android.vview.view$OnClickListen(">在com.example.android.enterestcalculator.MainActivity.onCreate(MainActivity.java:25(在android.app.Activity.performCreate(Activity.java:6662(在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118(在android.app.ActivityThread.performLaunchActivity(ActivityThreads.java:2599(在android.app.ActivityThread.handleLaunchActivity(ActivityThreads.java:2707(在android.app.ActivityThread.-wrap12(ActivityThreads.java(在android.app.ActivityThread$H.handleMessage(ActivityThreads.java:1460(在android.os.Handler.dispatchMessage(Handler.java:102(在android.os.Looper.loop(Looper.java:154(在android.app.ActivityThread.main(ActivityThreads.java:6077(位于java.lang.reflect.Method.ioke(本机方法(网址:com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866(网址:com.android.internal.os.ZygoteInit.main(ZygoteNit.java:756(**

您应该为按钮计算按钮findViewById

findViews()中设置此代码

calculateButton = findViewById(R.id.calculateButton);

相关内容

  • 没有找到相关文章

最新更新