1

I wrote an app which utlizes the phone's camera. On most of the devices I tried it worked fine but with some the application crashes on opening. A little debugging showed that there's a problem when trying to access the camera.

Full Error message:

06-14 23:15:01.550 21872-21872/bguproject.vlc E/AndroidRuntime: FATAL EXCEPTION: main
Process: bguproject.vlc, PID: 21872
java.lang.RuntimeException: Unable to start activity ComponentInfo{bguproject.vlc/bguproject.vlc.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.hardware.Camera$Parameters android.hardware.Camera.getParameters()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
at android.app.ActivityThread.access$900(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5525)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.hardware.Camera$Parameters android.hardware.Camera.getParameters()' on a null object reference
at bguproject.vlc.MainActivity.onCreate(MainActivity.java:54)
at android.app.Activity.performCreate(Activity.java:6272)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494) 
at android.app.ActivityThread.access$900(ActivityThread.java:157) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5525) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 

as you can see the error says I try to invoke virtual method on a null object reference which wouldn't happen if the writing of mCamera was done properly

The code (the writing is on the try-catch phrase at the end):

package bguproject.vlc;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ScrollView;


public class MainActivity extends Activity implements View.OnClickListener{
    private Camera mCamera = null;
    private CameraView mCameraView = null;
    private int fpsRate[] = new int[2];
    private  boolean work = true,receiving = false;
    private boolean send = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Logger l = Logger.getInstance(this); //create the logger instance

        Logger.Log("Starting VLC app...");

        try{
            bguproject.vlc.Logger.Log("\tAccessing camera...");
            mCamera = Camera.open();//you can use open(int) to use different cameras
        } catch (Exception e){
            bguproject.vlc.Logger.LogError("\tFailed to get camera: " + e.getMessage());
        }

full code: http://pastebin.com/hxqJK3t2

all the phones that crushed had a working camera and were relatively new (Samsung s6, samsung s7 and LG g4).

Voly
  • 145
  • 4
  • 16

1 Answers1

3

Samsung S6 and LG G4 both are upgradable to Android Marshmallow (v6 or API 23) and Samsung S7 is running Marshmallow on stock ROM so I'll assume all 3 of that devices are running Marshmallow :)

From https://developer.android.com/training/permissions/requesting.html
Note: Beginning with Android 6.0 (API level 23), users can revoke permissions from any app at any time, even if the app targets a lower API level. You should test your app to verify that it behaves properly when it's missing a needed permission, regardless of what API level your app targets.

Aside from the permissions set in the manifest, you'll need to request/check for permission on runtime. There are sample codes in there you can use, or...


Quick solution,

go to Settings-> Apps->(Your app name)->Permissions and enable the camera permission. Done, although not recommended for final product

then try your app again. Should be working now :D

Red Dango
  • 372
  • 2
  • 7
  • That was it, thank you very much. I implemented a permission request but now I get a 'setParameters failed' error although I gave permission and the camera was accessed. got any ideas? full error - http://pastebin.com/F2FGMrri – Voly Jun 18 '16 at 10:14
  • Looking at your code, I believe the problem lies at the `p.setPreviewFpsRange(14000, 14000); `, you can see the solutions at [link](http://stackoverflow.com/questions/16128271/setpreviewfpsrange-not-working-despite-values-being-within-getpreviewfpsranges) and [link](http://stackoverflow.com/questions/16128271/setpreviewfpsrange-not-working-despite-values-being-within-getpreviewfpsranges), it seems that you will have to call `.getSupportedPreviewFpsRange(); ` and use the supported range instead of supplying an arbitrary value :) – Red Dango Jun 19 '16 at 05:50