简体   繁体   中英

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: FATAL EXCEPTION: main Process: com.example.android.interestcal enter image description here culator, PID: 5655 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.interestcalculator/com.example.android.interestcalculator.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect. Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at com.example.android.interestcalculator.MainActivity.onCreate(MainActivity.java:25) at android.app.Activity.performCreate(Activity.java:6662) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread .main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) **

you should findViewById for your Button calculateButton

set this code in findViews()

calculateButton = findViewById(R.id.calculateButton);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM