简体   繁体   English

这个Android应用程式无法在API 22模拟器中使用,但可以在API 16模拟器中使用

[英]this android app works in API 22 emulator not but in API 16 emulator

I am a newbie to android development. 我是android开发的新手。 I am making a simple app to open camera in sufaceView. 我正在制作一个简单的应用程序,以在sufaceView中打开相机。 I want to work this app from the android API level 16. I have written some code and that works in API level 22 emulator but not in API level 16 emulator (When I click on Start button, app crashes) 我想从android API级别16开始运行此应用程序。我编写了一些代码,该代码可以在API级别22模拟器中使用,但不能在API级别16模拟器中使用(当我单击“开始”按钮时,应用程序崩溃)

VideoServer.java VideoServer.java

 package com.example.admin2.cameraonsurfaceview; import android.app.Activity; import android.hardware.Camera; import android.os.Bundle; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class VideoServer extends Activity implements SurfaceHolder.Callback { TextView testView; Camera camera; SurfaceView surfaceView; SurfaceHolder surfaceHolder; private final String tag = "VideoServer"; Button start, stop; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); start = (Button)findViewById(R.id.btn_start); start.setOnClickListener(new Button.OnClickListener() { public void onClick(View arg0) { start_camera(); } }); stop = (Button)findViewById(R.id.btn_stop); stop.setOnClickListener(new Button.OnClickListener() { public void onClick(View arg0) { stop_camera(); } }); surfaceView = (SurfaceView)findViewById(R.id.surfaceView1); surfaceHolder = surfaceView.getHolder(); surfaceHolder.addCallback(this); surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); Toast.makeText(this, "Need your location!", Toast.LENGTH_SHORT).show(); } private void start_camera() { try{ camera = Camera.open(); }catch(RuntimeException e){ Log.e(tag, "init_camera: " + e); return; } Camera.Parameters param; param = camera.getParameters(); //modify parameter param.setPreviewFrameRate(20); param.setPreviewSize(176, 144); camera.setParameters(param); try { camera.setPreviewDisplay(surfaceHolder); camera.startPreview(); } catch (Exception e) { Log.e(tag, "init_camera: " + e); return; } } private void stop_camera() { camera.stopPreview(); camera.release(); } public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub } public void surfaceCreated(SurfaceHolder holder) { // TODO Auto-generated method stub } public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub } } 

and the layout file look like 布局文件看起来像

 <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.admin2.cameraonsurfaceview.MainActivity"> <SurfaceView android:id="@+id/surfaceView1" android:layout_width="201dp" android:layout_height="230dp" tools:layout_constraintTop_creator="1" tools:layout_constraintRight_creator="1" android:layout_marginStart="52dp" android:layout_marginEnd="52dp" app:layout_constraintRight_toRightOf="parent" android:layout_marginTop="25dp" tools:layout_constraintLeft_creator="1" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent" android:layout_marginLeft="52dp" android:layout_marginRight="52dp" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintBottom_toBottomOf="parent" android:layout_marginBottom="8dp" app:layout_constraintVertical_bias="0.0" /> <Button android:id="@+id/btn_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start" android:layout_marginTop="27dp" app:layout_constraintTop_toBottomOf="@+id/surfaceView1" android:layout_marginLeft="52dp" app:layout_constraintLeft_toLeftOf="parent" android:layout_marginStart="52dp" /> <Button android:id="@+id/btn_stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Stop" tools:layout_constraintRight_creator="1" tools:layout_constraintBottom_creator="1" app:layout_constraintBottom_toBottomOf="parent" android:layout_marginEnd="52dp" app:layout_constraintRight_toRightOf="parent" android:layout_marginBottom="16dp" android:layout_marginRight="131dp" android:layout_marginTop="8dp" app:layout_constraintTop_toBottomOf="@+id/surfaceView1" app:layout_constraintVertical_bias="0.157" /> </android.support.constraint.ConstraintLayout> 

My android manifest looks like 我的Android清单看起来像

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.admin2.cameraonsurfaceview"> <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="22" /> <uses-feature android:name="android.hardware.camera" /> <uses-permission android:name="android.permission.CAMERA" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".VideoServer"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> 

when I click on the start button, app stops and I get the following exception 当我单击开始按钮时,应用程序停止,并且出现以下异常

10-05 15:39:48.174 2335-2335/com.example.admin2.cameraonsurfaceview E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
  at com.example.admin2.cameraonsurfaceview.VideoServer.start_camera(VideoServer.java:61)
  at com.example.admin2.cameraonsurfaceview.VideoServer.access$000(VideoServer.java:14)
  at com.example.admin2.cameraonsurfaceview.VideoServer$1.onClick(VideoServer.java:34)
  at android.view.View.performClick(View.java:4084)
  at android.view.View$PerformClick.run(View.java:16966)
  at android.os.Handler.handleCallback(Handler.java:615)
  at android.os.Handler.dispatchMessage(Handler.java:92)
  at android.os.Looper.loop(Looper.java:137)
  at android.app.ActivityThread.main(ActivityThread.java:4745)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:511)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
  at dalvik.system.NativeStart.main(Native Method)

Delete <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="22" /> from your manifest. 从清单中删除<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="22" />

Then check your version into build.gradle . 然后将您的版本检查到build.gradle Example: 例:

android { defaultConfig { minSdkVersion 16 // here you need to change into 16 if you already didn't targetSdkVersion 25 . . In order to work on API 16 you need to set minSdkVersion to 16 . 为了使用API​​ 16,您需要将minSdkVersion设置为16 Then resync gradle and solve the problems that may appear with code that is not compatible with version 16. 然后重新同步gradle并解决与版本16不兼容的代码可能出现的问题。

Finally I fixed the issue myself. 最后,我自己解决了这个问题。 The following line of code was causing the NullPointerException 以下代码行导致NullPointerException

camera = Camera.open(); 相机= Camera.open();

I changed to 我改为

camera = Camera.open(1); 相机= Camera.open(1);

then it worked. 然后它起作用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM