최근에 CCTV 카메라 앱을 사용했는데 기기에 카메라가 없으면이 logcat 이 표시 되고 앱이 종료됩니다.
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)
내 소스 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);
.
.
.
}
카메라가 없다면. capturer
이 반환 null
그래서 나는 테스트
if (capturer == null || capturer.equals("") == true) {
try {
rebootProcess = Runtime.getRuntime().exec(new String[]{"su", "-c", "reboot"});
} catch (IOException e) {
e.printStackTrace();
}
}
앱을 실행합니다. 장치를 재부팅했지만 카메라가없는 경우 검정색 배경을 표시하고 싶습니다.
카메라에 연결되어 있으면 GLSurfaceView
glview = (GLSurfaceView) findViewById(R.id.glview);
VideoRendererGui.setView(glview, null) ;
try {
. . . .
} catch {
}
아마도 조각을 사용합니까? 저에게 조언을 부탁드립니다. 나는 생각한다
if (capturer == null || capturer.equals("") == true) {
// showing black background ??..
}
근데 검은 색 배경 부분은 잘 모르겠어요.
감사.
@최신 정보
MainActivity.java (전체 생성시)
@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
}
나는 당신의 조언을 내 소스를 변경했습니다. 그러나 발생nullpointerException
다른 부분에서. 프로그래밍 방식으로 어떻게 할 수 있습니까?
카메라를 사용하는 앱인 경우이 기능이없는 휴대폰에 앱을 설치하면 안됩니다. 매니페스트에 다음을 입력하십시오.
<uses-feature android:name="android.hardware.camera"
required="true" />
[편집하다]
VideoCapturerAndroid
소스 에 따르면 null
카메라가 없으면 다음 줄이 반환 됩니다.
String nameOfFrontFacingDevice = VideoCapturerAndroid.getNameOfFrontFacingDevice();
따라서 귀하의 경우 이것은 자체적으로 null이어야합니다. 그러나 이름을 create
. 나는 이것이 NPE가 오는 곳이라고 생각합니다.
VideoCapturerAndroid capturer = VideoCapturerAndroid.create(nameOfFrontFacingDevice); // name might be null
따라서 여기에 null 검사를해야합니다.
if (nameOfFrontFacingDevice != null) {
VideoCapturerAndroid capturer = VideoCapturerAndroid.create(nameOfFrontFacingDevice);
} else {
// Other stuff
}
[편집 2]
어떤 카메라 장치를 들어, 포착 기 아직도 나오는 것 null
그리고 그에게 전화로 문제를 만들고있다 peerConnectionFactory.createVideoSource(capturer, videoConstraints)
. 따라서 카메라가 있고 NPE를 피하는 경우에만 호출 할 수 있습니다. 이제 glview.onResume()
call in activity 에서 또 다른 문제가 발생 합니다 onResume()
. 따라서 카메라를 확인하기 전에 초기화해야합니다. 제안 된 수정 사항은 아래 코드를 확인하십시오.
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());
}
});
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다