简体   繁体   English

尝试在 null object 引用上调用虚拟方法并设置点击侦听器

[英]Attempt to invoke virtual method on a null object reference with set on click listener

I am trying to have a button within a popup send a variable to another class.我试图让弹出窗口中的按钮将变量发送到另一个 class。 However, when setting the on click listener I get this issue.但是,在设置点击监听器时,我遇到了这个问题。 I have initialized the button and tried to move the code to a different location.我已初始化按钮并尝试将代码移动到其他位置。 I have removed some of the code after the issue as it is not relevant问题后我删除了一些代码,因为它不相关

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.camera2.*;
import android.os.Bundle;
import android.view.*;
import android.util.*;
import android.widget.*;
import java.math.*;


public class MainActivity extends AppCompatActivity {

    ImageButton imageButton, singleImageButton;
    Integer width, height, minISO, maxISO, ISO, numberImages;
    boolean focusable;
    View popupView;
    LayoutInflater inflater;
    SeekBar ISOseekbar, exposureBar;
    TextView ISOreadout, exposureReadout, textView;
    String cameraId, numberImagesString;
    Range<Integer> rangeISO;
    Range<Long> exposureRange;
    Long exposureMax, exposureMin, exposure;
    BigDecimal exposureRoundedMax, exposureRoundedMin;
    Button acceptNumberImages;
    EditText numberImagesInput;


    public void mainUI(){

        setContentView(R.layout.activity_main);
        imageButton = findViewById(R.id.imageButton);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                inflater = (LayoutInflater)
                        getSystemService(LAYOUT_INFLATER_SERVICE);
                popupView = inflater.inflate(R.layout.cameralayout, null);
                width = LinearLayout.LayoutParams.WRAP_CONTENT;
                height = LinearLayout.LayoutParams.WRAP_CONTENT;
                focusable = true;
                final PopupWindow popupwindow = new PopupWindow(popupView, width, height, focusable);
                popupwindow.showAtLocation(view, Gravity.CENTER, 0, 0);
                acceptNumberImages = findViewById(R.id.acceptNumberImages);
                acceptNumberImages.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        numberImagesInput = findViewById(R.id.numberImagesInput);
                        numberImagesString = numberImagesInput.getText().toString();
                        numberImages = Integer.parseInt(numberImagesString);
                        takePhoto.multipleImages(numberImages);

                    }
                });
                popupView.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        popupwindow.dismiss();
                        return true;
                    }
                });

            }

        });

The full error that I get is:我得到的完整错误是:

    Process: com.example.startrailscamera, PID: 18190
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at com.example.startrailscamera.MainActivity$1.onClick(MainActivity.java:52)
        at android.view.View.performClick(View.java:6608)
        at android.view.View.performClickInternal(View.java:6585)
        at android.view.View.access$3100(View.java:785)
        at android.view.View$PerformClick.run(View.java:25921)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:201)
        at android.app.ActivityThread.main(ActivityThread.java:6861)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

Try changing acceptNumberImages = findViewById(R.id.acceptNumberImages) to acceptNumberImages = popupView.findViewById(R.id.acceptNumberImages) if acceptNumberImages is a view in your cameralayout.xml file.如果 acceptNumberImages 是您的 cameralayout.xml 文件中的一个视图,请尝试将acceptNumberImages = findViewById(R.id.acceptNumberImages)更改为acceptNumberImages = popupView.findViewById(R.id.acceptNumberImages)

  1. Have you tryed to put the setOnClickListener outside the other setOnClickListener?您是否尝试将 setOnClickListener 放在另一个 setOnClickListener 之外?
  2. Are u sure the "findViewById(R.id.acceptNumberImages);"你确定“findViewById(R.id.acceptNumberImages);” aren't returning null?不返回 null?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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