Android E / AndroidRuntime:FATAL EXCEPTION:主要错误

时间:2016-01-30 21:20:24

标签: android eclipse

我有一个问题,我似乎无法修复。这是错误:

01-30 15:56:48.350 891-891/com.example.hummus.onething E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.hummus.onething, PID: 891
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hummus.onething/com.example.hummus.onething.MainActivity}: java.lang.UnsupportedOperationException
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.UnsupportedOperationException
at com.google.android.gms.common.api.GoogleApiClient.getContext(Unknown Source)
at com.google.android.gms.internal.zzju.zza(Unknown Source)
at com.google.android.gms.internal.zzju.start(Unknown Source)
at com.example.hummus.onething.MainActivity.onStart(MainActivity.java:465)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
at android.app.Activity.performStart(Activity.java:5241)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2157)
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) 

它假设是Android相机的应用程序,用于确定手机与相机之间的距离。这是我的活动课

package com.example.hummus.onething;

import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.Face;
import android.hardware.Camera.FaceDetectionListener;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.net.Uri;
import android.nfc.Tag;
import android.os.Bundle;
import android.provider.MediaStore.Images.Media;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.Api;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.Status;

import static android.util.Log.*;


public class MainActivity extends Activity implements SurfaceHolder.Callback
{

static Camera camera;
Button snap;

SurfaceView surface;
SurfaceHolder surfaceHolder;
boolean previewing = false;
LayoutInflater controlInflater;
static TextView info;

View drawingView;
static Face[] detectedFaces;



private ScheduledExecutorService myScheduledExecutorService;
/**
 * ATTENTION: This was auto-generated to implement the App Indexing API.
 * See https://g.co/AppIndexing/AndroidStudio for more information.
 */
private GoogleApiClient client = new GoogleApiClient() {
    @Override
    public boolean hasConnectedApi(Api<?> api) {
        return false;
    }

    @Override
    public ConnectionResult getConnectionResult(Api<?> api) {
        return null;
    }

    @Override
    public void connect() {

    }

    @Override
    public ConnectionResult blockingConnect() {
        return null;
    }

    @Override
    public ConnectionResult blockingConnect(long l, TimeUnit timeUnit) {
        return null;
    }

    @Override
    public void disconnect() {

    }

    @Override
    public void reconnect() {

    }

    @Override
    public PendingResult<Status> clearDefaultAccountAndReconnect() {
        return null;
    }

    @Override
    public void stopAutoManage(FragmentActivity fragmentActivity) {

    }

    @Override
    public boolean isConnected() {
        return false;
    }

    @Override
    public boolean isConnecting() {
        return false;
    }

    @Override
    public void registerConnectionCallbacks(ConnectionCallbacks connectionCallbacks) {

    }

    @Override
    public boolean isConnectionCallbacksRegistered(ConnectionCallbacks connectionCallbacks) {
        return false;
    }

    @Override
    public void unregisterConnectionCallbacks(ConnectionCallbacks connectionCallbacks) {

    }

    @Override
    public void registerConnectionFailedListener(OnConnectionFailedListener onConnectionFailedListener) {

    }

    @Override
    public boolean isConnectionFailedListenerRegistered(OnConnectionFailedListener onConnectionFailedListener) {
        return false;
    }

    @Override
    public void unregisterConnectionFailedListener(OnConnectionFailedListener onConnectionFailedListener) {

    }

    @Override
    public void dump(String s, FileDescriptor fileDescriptor, PrintWriter printWriter, String[] strings) {

    }
};

@Override
protected void onCreate(Bundle savedInstanceState)
{
    Log.d("DEBUG", "Import Done");

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    surface = (SurfaceView) findViewById(R.id.surfaceView);
    surfaceHolder = surface.getHolder();
    surfaceHolder.addCallback(this);

    drawingView = new View(this);
    LayoutParams layoutParamsDrawing
            = new LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.FILL_PARENT);
    this.addContentView(drawingView, layoutParamsDrawing);

    controlInflater = LayoutInflater.from(getBaseContext());
    View viewControl = controlInflater.inflate(R.layout.activity_main, null);
    LayoutParams layoutParamsControl
            = new LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.FILL_PARENT);
    this.addContentView(viewControl, layoutParamsControl);


    snap = (Button) findViewById(R.id.button);
    snap.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

    Log.d("DEBUG", "CHECKPOINT 1");

    LinearLayout layoutBackground = (LinearLayout) findViewById(R.id.background);
    layoutBackground.setOnClickListener(new LinearLayout.OnClickListener()
    {

        AutoFocusCallback myAutoFocusCallback = new AutoFocusCallback()
        {

            @Override
            public void onAutoFocus(boolean arg0, Camera arg1)
            {
                // TODO Auto-generated method stub
                if (arg0)
                {
                    snap.setEnabled(true);
                    camera.cancelAutoFocus();
                }

                float focusDistances[] = new float[3];
                arg1.getParameters().getFocusDistances(focusDistances);
                info.setText("Optimal Focus Distance(meters): "
                        + focusDistances[Parameters.FOCUS_DISTANCE_OPTIMAL_INDEX]);
            }
        };

        @Override
        public void onClick(View arg0)
        {
            // TODO Auto-generated method stub

            snap.setEnabled(false);
            camera.autoFocus(myAutoFocusCallback);
        }
    });

    info = (TextView) findViewById(R.id.textView);
    Log.d("DEBUG","CHECKPOINT 2");
    Log.d("DEBUG","WHERE PROGRAM STOPS");

}



private static class DrawingView extends View {

    static boolean haveFace;
    Paint drawingPaint;

    public DrawingView(Context context) {
        super(context);
        haveFace = false;
        drawingPaint = new Paint();
        drawingPaint.setColor(Color.GREEN);
        drawingPaint.setStyle(Paint.Style.STROKE);
        drawingPaint.setStrokeWidth(2);
    }

    public static void setHaveFace(boolean h)
    {
        haveFace = h;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        if (haveFace) {

            // Camera driver coordinates range from (-1000, -1000) to (1000, 1000).
            // UI coordinates range from (0, 0) to (width, height).

            int vWidth = getWidth();
            int vHeight = getHeight();

            for (int i = 0; i < detectedFaces.length; i++) {

                if (i == 0) {
                    drawingPaint.setColor(Color.GREEN);
                } else {
                    drawingPaint.setColor(Color.RED);
                }

                int l = detectedFaces[i].rect.left;
                int t = detectedFaces[i].rect.top;
                int r = detectedFaces[i].rect.right;
                int b = detectedFaces[i].rect.bottom;
                int left = (l + 1000) * vWidth / 2000;
                int top = (t + 1000) * vHeight / 2000;
                int right = (r + 1000) * vWidth / 2000;
                int bottom = (b + 1000) * vHeight / 2000;
                canvas.drawRect(
                        left, top, right, bottom,
                        drawingPaint);
            }
        } else {
            canvas.drawColor(Color.TRANSPARENT);
        }
        Log.d("DEBUG","CHECKPOINT 3");
    }

}



FaceDetectionListener faceDetectionListener
        = new FaceDetectionListener()
{

    @Override
    public void onFaceDetection(Face[] faces, Camera tcamera)
    {
        final AutoFocusCallback myAutoFocusCallback = new AutoFocusCallback()
        {

            @Override
            public void onAutoFocus(boolean arg0, Camera arg1)
            {
                // TODO Auto-generated method stub
                if (arg0)
                {
                    snap.setEnabled(true);
                    camera.cancelAutoFocus();
                }

                float focusDistances[] = new float[3];
                arg1.getParameters().getFocusDistances(focusDistances);
                info.setText("Optimal Focus Distance(meters): "
                        + focusDistances[Parameters.FOCUS_DISTANCE_OPTIMAL_INDEX]);
            }
        };


        if (faces.length == 0) {
            info.setText(" No Face Detected! ");
            DrawingView.setHaveFace(true);
        } else {
            info.setText(String.valueOf(faces.length) + " Face Detected :) ");
            DrawingView.setHaveFace(false);
            detectedFaces = faces;

            //Set the FocusAreas using the first detected face
            List<Camera.Area> focusList = new ArrayList<Camera.Area>();
            Camera.Area firstFace = new Camera.Area(faces[0].rect, 1000);
            focusList.add(firstFace);

            if (camera.getParameters().getMaxNumFocusAreas() > 0) {
                camera.getParameters().setFocusAreas(focusList);
            }

            if (camera.getParameters().getMaxNumMeteringAreas() > 0) {
                camera.getParameters().setMeteringAreas(focusList);
            }

            focusList = new ArrayList<Camera.Area>();
            firstFace = new Camera.Area(faces[0].rect, 1000);
            focusList.add(firstFace);

            if(camera.getParameters().getMaxNumFocusAreas()>0){
                camera.getParameters().setFocusAreas(focusList);
            }

            if(camera.getParameters().getMaxNumMeteringAreas()>0){
                camera.getParameters().setMeteringAreas(focusList);
            }

            snap.setEnabled(false);
            //camera.getParameters().setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);

            //Stop further Face Detection
            camera.stopFaceDetection();

            snap.setEnabled(false);

            camera.autoFocus(myAutoFocusCallback);


            //Delay call autoFocus(myAutoFocusCallback)
            myScheduledExecutorService = Executors.newScheduledThreadPool(1);
            myScheduledExecutorService.schedule(new Runnable(){
                public void run() {
                    camera.autoFocus(myAutoFocusCallback);
                }
            }, 500, TimeUnit.MILLISECONDS);
        }
        drawingView.invalidate();

        Log.d("DEBUG", "CHECKPOINT 4");
    }
};




@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
                           int height) {
    // TODO Auto-generated method stub
    if(previewing){
        camera.stopFaceDetection();
        camera.stopPreview();
        previewing = false;
    }
    Log.d("DEBUG","CHECKPOINT 5");
    if (camera != null){
        try {
            camera.setPreviewDisplay(surfaceHolder);
            camera.startPreview();

            info.setText(String.valueOf(
                    "Max Face: " + camera.getParameters().getMaxNumDetectedFaces()));
            camera.startFaceDetection();
            previewing = true;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    Log.d("DEBUG","CHECKPOINT 6");
}

@Override
public void surfaceCreated(SurfaceHolder holder)
{
    // TODO Auto-generated method stub
    camera = Camera.open();
    camera.setFaceDetectionListener(faceDetectionListener);
    Log.d("DEBUG", "CHECKPOINT 7");
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    camera.stopFaceDetection();
    camera.stopPreview();
    camera.release();
    camera = null;
    previewing = false;
    Log.d("DEBUG","CHECKPOINT 8");
}

@Override
public void onStart() {
    //super.onStart();

    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    client.connect();
    Log.d("DEBUG", "CHECKPOINT 9");
    Action viewAction = Action.newAction(
            Action.TYPE_VIEW, // TODO: choose an action type.
            "Main Page", // TODO: Define a title for the content shown.
            // TODO: If you have web page content that matches this app activity's content,
            // make sure this auto-generated web page URL is correct.
            // Otherwise, set the URL to null.
            Uri.parse("http://host/path"),
            // TODO: Make sure this auto-generated app deep link URI is correct.
            Uri.parse("android-app://com.example.hummus.onething/http/host/path")
    );
    Log.d("DEBUG","CHECKPOINT 10");
    client.connect();
    viewAction = Action.newAction(Action.TYPE_VIEW, "The Action?", Uri.parse("android-app://com.example.hummus.onething/http/host/path"));
    AppIndex.AppIndexApi.start(client, viewAction); //The Problem
    Log.d("DEBUG", "CHECKPOINT 11");
}

@SuppressLint("MissingSuperCall")
@Override
public void onStop() {
   // super.onStop();

    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
    Action viewAction = Action.newAction(
            Action.TYPE_VIEW, // TODO: choose an action type.
            "Main Page", // TODO: Define a title for the content shown.
            // TODO: If you have web page content that matches this app activity's content,
            // make sure this auto-generated web page URL is correct.
            // Otherwise, set the URL to null.
            Uri.parse("http://host/path"),
            // TODO: Make sure this auto-generated app deep link URI is correct.
            Uri.parse("android-app://com.example.hummus.onething/http/host/path")
    );
    AppIndex.AppIndexApi.end(client, viewAction);
    client.disconnect();


}
}

你们有什么事吗?

0 个答案:

没有答案