Recently I used cctv camera app but if not have a camera in device, showing this logcat and app is died
java.lang.RuntimeException: Unable to start activity ComponentInfo{kr.co.iosystem.blackeyeonandroid/kr.co.iosystem.blackeyeonandroid.BlackEyeActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at org.webrtc.PeerConnectionFactory.createVideoSource(PeerConnectionFactory.java:111)
at kr.co.iosystem.blackeyeonandroid.BlackEyeActivity.onCreate(MainActivity.java:235)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
and my source MainActivity.java
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String nameOfFrontFacingDevice = VideoCapturerAndroid.getNameOfFrontFacingDevice();
String nameOfBackFacingDevice = VideoCapturerAndroid.getNameOfBackFacingDevice();
VideoCapturerAndroid capturer = VideoCapturerAndroid.create(nameOfFrontFacingDevice);
.
.
.
}
if not have a camera. capturer
this return null
so , I test
if (capturer == null || capturer.equals("") == true) {
try {
rebootProcess = Runtime.getRuntime().exec(new String[]{"su", "-c", "reboot"});
} catch (IOException e) {
e.printStackTrace();
}
}
and I execute app. I rebooted my device but I want if not have a camera, showing black color background
If its connected to camera then it showing GLSurfaceView
glview = (GLSurfaceView) findViewById(R.id.glview);
VideoRendererGui.setView(glview, null) ;
try {
. . . .
} catch {
}
perhaps, I use fragment? please advice for me. I think
if (capturer == null || capturer.equals("") == true) {
// showing black background ??..
}
but I don't know showing black background part.
thanks.
@update
MainActivity.java (full oncreate)
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String nameOfFrontFacingDevice = VideoCapturerAndroid.getNameOfFrontFacingDevice();
if (nameOfFrontFacingDevice != null) {
VideoCapturerAndroid capturer = VideoCapturerAndroid.create(nameOfFrontFacingDevice);
MediaConstraints videoConstraints = new MediaConstraints();
VideoSource videoSource = peerConnectionFactory.createVideoSource(capturer, videoConstraints);
localVideoTrack = peerConnectionFactory.createVideoTrack(VIDEO_TRACK_ID, videoSource);
glview =(GLSurfaceView) findViewById.(R.id.showing);
VideoRendererGui.setView(glview, null);
try {
renderer = VideoRendererGui.createGui(0,0,100,100, VideoRendererGui.ScalingType.SCALE_ASPECT_FILL, true);
localVideoTrack.addRenderer(renderer);
} catch (Exception e) {
e.printStackTrace();
}
mediaStream = peerConnectionFactory.createLocalMediaStream(LOCAL_MEDIA_STREAM_ID);
mediaStream.addTrack(localVideoTrack);
} else {
//space
}
I changed my source your advice. but occur nullpointerException
In else part. how can I programmatically?
If its an app where you are using camera then, the app should not install in phones that lack this feature. In your manifest put:
<uses-feature android:name="android.hardware.camera"
required="true" />
[EDIT]
According to VideoCapturerAndroid
source, the following line will return null
if no camera exist:
String nameOfFrontFacingDevice = VideoCapturerAndroid.getNameOfFrontFacingDevice();
So, in your case this must itself be coming null. However, you are passing the name to create
. I think this is where NPE is coming from:
VideoCapturerAndroid capturer = VideoCapturerAndroid.create(nameOfFrontFacingDevice); // name might be null
So, you should put up a null check here:
if (nameOfFrontFacingDevice != null) {
VideoCapturerAndroid capturer = VideoCapturerAndroid.create(nameOfFrontFacingDevice);
} else {
// Other stuff
}
[EDIT 2]
For devices with no camera, your capturer would still be coming out null
and that is creating the problem with call to peerConnectionFactory.createVideoSource(capturer, videoConstraints)
. Hence, we can make sure we call it only if we have the camera and avoid the NPE. Now, we run into another problem in glview.onResume()
call in activity onResume()
. So, we must initialise that before we check for camera. Check the code below for suggested fix:
String nameOfFrontFacingDevice = VideoCapturerAndroid.getNameOfFrontFacingDevice();
String nameOfBackFacingDevice = VideoCapturerAndroid.getNameOfBackFacingDevice();
Log.i(TAG, "VideoCapturerAndroid.getNameOfFrontFacingDevice() = " + nameOfFrontFacingDevice);
Log.i(TAG, "VideoCapturerAndroid.getNameOfBackFacingDevice() = " + nameOfBackFacingDevice);
VideoCapturerAndroid capturer = VideoCapturerAndroid.create(nameOfFrontFacingDevice);
// Initialising the glview here
glview = (GLSurfaceView) findViewById(R.id.glview);
VideoRendererGui.setView(glview, null);
MediaConstraints videoConstraints = new MediaConstraints();
if (capturer == null || capturer.equals("")) {
Log.d(TAG, "not camera");
}
// Doing further processing only if capturer is not null
else {
VideoSource videoSource = peerConnectionFactory.createVideoSource(capturer, videoConstraints);
localVideoTrack = peerConnectionFactory.createVideoTrack(VIDEO_TRACK_ID, videoSource);
try {
renderer = VideoRendererGui.createGui(0, 0, 100, 100, VideoRendererGui.ScalingType.SCALE_ASPECT_FILL, true);
renderer_sub = VideoRendererGui.createGui(72, 72, 25, 25, VideoRendererGui.ScalingType.SCALE_ASPECT_FILL, true);
localVideoTrack.addRenderer(renderer_sub);
localVideoTrack.addRenderer(renderer);
} catch (Exception e) {
e.printStackTrace();
}
mediaStream = peerConnectionFactory.createLocalMediaStream(LOCAL_MEDIA_STREAM_ID);
mediaStream.addTrack(localVideoTrack);
ImageButton imageButton = (ImageButton) findViewById(R.id.backbutton);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
moveTaskToBack(true);
finish();
android.os.Process.killProcess(android.os.Process.myPid());
}
});
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments