【问题标题】:unable to run app on android emulator, App keeps stopping无法在android模拟器上运行应用程序,应用程序不断停止
【发布时间】:2021-09-26 10:59:05
【问题描述】:
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: FATAL EXCEPTION: main 进程:com.example.android.interestcalenter image description hereculator,PID:5655 java.lang.RuntimeException:无法启动活动 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) **

【问题讨论】:

  • 你还没有给calculateButton赋值,所以它是null。大多数关于 Android 应用程序开发的书籍和课程都展示了如何设置 UI,包括如何填充像 calculateButton 这样的字段。例如,here is a free older copy 我的一本书涵盖了这一点,以及其他主题。

标签: android nullpointerexception


【解决方案1】:

你应该findViewById为你的按钮calculateButton

将此代码设置为findViews()

calculateButton = findViewById(R.id.calculateButton);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    • 2015-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    相关资源
    最近更新 更多