I try to make CallBack from a fragment in Activity for this I created an interface, Activity implements it, the fragment receives the implementation of this interface in the onAttach method, in the same method I make a callback call - and it works fine. Further, when I try to make a call to this callback in another method (captureImage ()) of this fragment, I get a NullPointerException, what could be the problem.

Interface:

public interface UpdateBitmapPaths{ void updateBitmap( ArrayList bitmappaths ); } 

Fragment:

 public class CameraFragment extends Fragment implements SurfaceHolder.Callback, AudioRecord.TakePictureListener, AudioRecord.SavePictureListener, AudioRecord.ReceivePictureListener { private String TAG = CameraFragment.class.getSimpleName(); private Context context; private AppCompatActivity activity; public static final String EXTRA_CAMERA_DATA = "camera_data"; private static final String KEY_IS_CAPTURING = "is_capturing"; private static final int TAKE_PICTURE_REQUEST_B = 100; private static Camera mCamera =null; private ImageView mCameraImage; private SurfaceView mCameraPreview; private static byte[] mCameraData; private boolean mIsCapturing; public static Bitmap mCameraBitmap; private View view; static String[] filetime2 = new String[100]; static ArrayList filetime3 = new ArrayList(); AudioRecord audioRecord; static int clicked=1; private UpdateBitmapPaths bitmapCallback; public static ArrayList <String> bitmappaths = new ArrayList<>(); @Override public void onAttach(Context context) { super.onAttach(context); this.context = context; this.activity = (AppCompatActivity) context; audioRecord = (AudioRecord) context; bitmapCallback = (AudioRecord) context; bitmapCallback.updateBitmap(bitmappaths); } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(R.layout.camera_fragment, container, false); mCameraImage = (ImageView) view.findViewById(R.id.camera_image_view); mCameraPreview = (SurfaceView) view.findViewById(R.id.preview_view); RelativeLayout fragment = (RelativeLayout) view.findViewById(R.id.fragment); DoubleTap = new GestureDetectorCompat(getActivity(), new MyGestureListener()); mCameraImage.setOnTouchListener(onTouchListener); mCameraImage.setOnLongClickListener(onLongClickListener); final SurfaceHolder surfaceHolder = mCameraPreview.getHolder(); surfaceHolder.addCallback(this); surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); mIsCapturing = true; if (mCamera == null) { try { mCamera = Camera.open(); mCamera.setPreviewDisplay(mCameraPreview.getHolder()); mCamera.setDisplayOrientation(90); if (mIsCapturing) { mCamera.startPreview(); } } catch (Exception e) { Toast.makeText(context, "Unable to open camera.", Toast.LENGTH_LONG) .show(); } } tempBitmap = Bitmap.createBitmap(4000, 3000, Bitmap.Config.RGB_565); tempCanvas = new Canvas(tempBitmap); return view; } @Override public void onSaveInstanceState(Bundle savedInstanceState) { super.onSaveInstanceState(savedInstanceState); savedInstanceState.putBoolean(KEY_IS_CAPTURING, mIsCapturing); } public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == TAKE_PICTURE_REQUEST_B) { if (resultCode == AppCompatActivity.RESULT_OK) { if (mCameraBitmap != null) { mCameraBitmap.recycle(); mCameraBitmap = null; } Bundle extras = data.getExtras(); byte[] cameraData = extras.getByteArray(CameraFragment.EXTRA_CAMERA_DATA); if (cameraData != null) { mCameraBitmap = BitmapFactory.decodeByteArray(cameraData, 0, cameraData.length); mCameraImageView.setImageBitmap(mCameraBitmap) } } else { mCameraBitmap = null; } } } @Override public void onPause() { super.onPause(); Log.i("On pause method works!", "works!"); if (mCamera != null) { mCamera.release(); mCamera = null; } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { if (mCamera != null) { try { mCamera.setPreviewDisplay(holder); if (mIsCapturing) { mCamera.startPreview(); } } catch (IOException e) { Toast.makeText(context, "Unable to start camera preview.", Toast.LENGTH_LONG).show(); } } } @Override public void surfaceCreated(SurfaceHolder holder) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { } public void captureImage() { if (mCamera != null) { mCamera.takePicture(null, null, new PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera) { mCameraData = data; File imageDirectory = null; SimpleDateFormat dateFormatfolder = new SimpleDateFormat("yyyy_MM_dd", Locale.getDefault()); imageDirectory = new File( Environment.getExternalStorageDirectory() + "/Audio_Recorder_Picture/Picture", dateFormatfolder.format(new Date()) + "_" + FirstscreenActivity.newpressed); if (!imageDirectory.exists() && !imageDirectory.mkdirs()) { imageDirectory = null; } else { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy_mm_dd_hh_mm_ss", Locale.getDefault()); File file = new File( imageDirectory.getPath() + File.separator + "image_" + dateFormat.format(new Date()) + ".png"); String filepath = file.getPath(); bitmappaths.add(filepath); // строчка, которая приводит к NullPointerException bitmapCallback.updateBitmap(bitmappaths); } } public void takePicture() { captureImage(); } @Override public void recievePicture(Bitmap bitmap) { mCameraImage.setImageBitmap(bitmap); mCameraImage.setRotation(0); } } } 

Activity:

  public class AudioRecord extends AppCompatActivity implements UpdateBitmapPaths{ ArrayList bitmapppaths; private CustomViewPagerH mPager; private PagerAdapter mPagerAdapter; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.activity_main); LinearLayout ll = (LinearLayout) findViewById(R.id.lin_three); mNextButton = (Button) findViewById(R.id.test4); mPreviousButton = (Button) findViewById(R.id.test5); mLabelPlayButton = (Button) findViewById(R.id.test6); mCaptureImageButton = (Button) findViewById(R.id.capture_image); mCaptureImageButton.setOnClickListener(mCaptureImageButtonClickListener); findViewById(R.id.capture_image).setOnClickListener(mCaptureImageButtonClickListener); recordButtonpause = (Button) findViewById(R.id.record_button); recordButtonpause.setOnClickListener(recordButtonListener); findViewById(R.id.record_button).setOnClickListener(recordButtonListener); chooseButton = (Button) findViewById(R.id.choose_button); chooseButton.setOnClickListener(chooseButtonListener); findViewById(R.id.choose_button).setOnClickListener(chooseButtonListener); mPager = (CustomViewPagerH) findViewById(R.id.pager); mPager.setPagingEnabled(false); mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager()); mPager.setAdapter(mPagerAdapter); } @Override public void updateBitmap(ArrayList bitmappaths) { this.bitmapppaths=bitmappaths; } private OnClickListener mCaptureImageButtonClickListener = new OnClickListener() { @Override public void onClick(View v) { Log.wtf("TAG", "First listener"); mCaptureImageButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (firstslide==0) { firstslide++; if (fragment!=null) { fragment.takePicture(); } mPager.setCurrentItem(mPager.getCurrentItem() + 1); updateCallback.update(CameraFragment.bitmappaths.size()); for (int i =0; i<CameraFragment.bitmappaths.size(); i++ ){ } } else{ mPager.setCurrentItem(mPager.getCurrentItem() - 1); firstslide--; } } }); } }; } 

    1 answer 1

    Oddly enough, in order for CallBack work, you need to subscribe to it from the activity in your fragment. As an analogy, the setOnClickListener(this) method signs your class to receive this event, just as you need to do with the fragment. You can do this without an interface.

    create a method in AudioRecord

     public void setArrayBitmap(ArrayList bitmappaths){ // тут ваш код } 

    and call it from the fragment (If the activity is the launching point of this fragment) so

     ((AudioRecord) getActivity()).setArrayBitmap(yuobitmap); 
    • I tried to do as you wrote, I still get a NullPointerException in the fragment: java.lang.NullPointerException reference - Lucky_girl
    • one
      then try to use the audioRecord variable in the snippet ... You basically need to add, you obviously have the listener == null, since you don't pass the subscriber to the snippet. - Chaynik