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).