Java 尝试使用set-on-click listener对空对象引用调用虚拟方法
我试图让弹出窗口中的按钮向另一个类发送变量。但是,当设置on-click侦听器时,我发现了这个问题。我已初始化按钮并尝试将代码移动到其他位置。 我已经删除了一些代码后的问题,因为它是不相关的Java 尝试使用set-on-click listener对空对象引用调用虚拟方法,java,android-studio,Java,Android Studio,我试图让弹出窗口中的按钮向另一个类发送变量。但是,当设置on-click侦听器时,我发现了这个问题。我已初始化按钮并尝试将代码移动到其他位置。 我已经删除了一些代码后的问题,因为它是不相关的 import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import android.Manif
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;
}
});
}
});
尝试将
acceptNumberImages=findViewById(R.id.acceptNumberImages)
更改为
acceptNumberImages=popupView.findViewById(R.id.acceptNumberImages)
如果acceptNumberImages是cameralayout.xml文件中的视图。看起来像acceptNumberImages=findViewById(R.id.acceptNumberImages)代码>不起作用,并且acceptNumberImages
为null
。。。但这只是基于错误消息和代码的猜测。可能是重复的
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)