最近我使用了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();
}
}
我执行app。我重新启动了我的设备,但如果没有相机,我想要显示黑色背景
如果它已连接到相机,则会显示GLSurfaceView
glview = (GLSurfaceView) findViewById(R.id.glview);
VideoRendererGui.setView(glview, null) ;
try {
. . . .
} catch {
}
或许,我使用片段?
请给我建议。
我想
if (capturer == null || capturer.equals("") == true) {
// showing black background ??..
}
但我不知道显示黑色背景部分。
感谢。
@Update
MainActivity.java(完全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
}
我改变了我的意见。
但发生nullpointerException
在其他方面。我怎么能以编程方式?
答案 0 :(得分:1)
如果它是您使用相机的应用程序,那么该应用程序不应安装在缺少此功能的手机中。在你的清单中:
<uses-feature android:name="android.hardware.camera"
required="true" />
<强> [编辑] 强>
根据VideoCapturerAndroid
来源,如果没有相机,则以下行将返回null
:
String nameOfFrontFacingDevice = VideoCapturerAndroid.getNameOfFrontFacingDevice();
所以,在你的情况下,这必须是空的。但是,您将名称传递给create
。我认为这是NPE的来源:
VideoCapturerAndroid capturer = VideoCapturerAndroid.create(nameOfFrontFacingDevice); // name might be null
所以,你应该在这里进行空检查:
if (nameOfFrontFacingDevice != null) {
VideoCapturerAndroid capturer = VideoCapturerAndroid.create(nameOfFrontFacingDevice);
} else {
// Other stuff
}
[编辑2]
对于没有相机的设备,您的捕获器仍然会出现null
,这就是通过调用peerConnectionFactory.createVideoSource(capturer, videoConstraints)
来解决问题。因此,我们可以确保只有拥有相机并避开NPE才能调用它。现在,我们在glview.onResume()
调用活动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());
}
});
}