这个Android应用程序在API 22模拟器中工作,但在API 16模拟器中

时间:2017-10-05 09:25:10

标签: java android

我是android开发的新手。我正在制作一个简单的应用程序在sufaceView中打开相机。我想从android API级别16开始使用这个应用程序。我已经编写了一些代码,它可以在API级别22模拟器中运行,但不能在API级别16模拟器中运行(当我单击“开始”按钮时,应用程序崩溃)

  

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
    }
}

,布局文件看起来像

<?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>

我的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>

当我点击开始按钮时,app停止,我得到以下异常

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)

2 个答案:

答案 0 :(得分:0)

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

然后将您的版本检入build.gradle。例如:

android { defaultConfig { minSdkVersion 16 // here you need to change into 16 if you already didn't targetSdkVersion 25 . . 要使用API​​ 16,您需要将minSdkVersion设置为16。然后重新同步gradle并解决与版本16不兼容的代码可能出现的问题。

答案 1 :(得分:0)

最后我自己解决了这个问题。 以下代码行导致NullPointerException

  

camera = Camera.open();

我改为

  

camera = Camera.open(1);

然后它奏效了。