문제

I'm getting a NullPointerException in my application when trying to detect a shake.

In my application I have a service running in the background, and an activity that calls the method cameraIntent().

public class Run1 extends Activity implements Shaker.Callback {
public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;
public static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
public static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;
private Uri fileUri;
private static final String TAG = Shaker.class.getSimpleName();
final Handler handle = new Handler();
Shaker shaker;
shakeCameraService shakecamera;


public void miThread() {
    Thread t = new Thread() {
        public void run() {
            handle.post(proceso);
        }
    };
    t.start();

}

public final Runnable proceso = new Runnable() {
    public void run() {         
        cameraIntent();


    }
};



/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Log.e("ONCREATE RUN1 ", "ONCREATE RUN1");
    startService(new Intent(this, shakeCameraService.class));       

}

public void cameraIntent() {
    shaker = new Shaker(this, 5.0d, 500, this);
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to
                                                        // // save the image
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file
                                                        // name
    // start the image capture Intent
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);


}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {

            if (fileUri != null) {
                Log.e(TAG, "Result ok");
                Log.e(TAG, "Image saved to:\n" + fileUri);
                Log.e(TAG, "Image path:\n" + fileUri.getPath());
                // uri.getLastPathSegment()
                // if store
                // in folder
            }

        } else if (resultCode == RESULT_CANCELED) {
            Log.e(TAG, "RESULT CANCELED");

        } else {
            // Image capture failed, advise user
        }
    }

}

private static Uri getOutputMediaFileUri(int type) {
    return Uri.fromFile(getOutputMediaFile(type));
}

/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type) {
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = new File(
            Environment.getExternalStorageDirectory() + "/DCIM/Camera");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d("ShakerCam", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
            .format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator
                + "IMG_" + timeStamp + ".jpg");
    } else {
        return null;
    }

    return mediaFile;
}

public void shakingStarted() {
    miThread();

}

public void shakingStopped() {
    // TODO Auto-generated method stub

}
}

This is my service

public class shakeCameraService extends Service implements Shaker.Callback {
Run1 run1;
Shaker shaker =null;


@Override
public void onCreate() {
    super.onCreate();
    shaker = new Shaker(this, 5.0d, 500, this);
    Log.e("ONCREATE SERVICE", "ONCREATE SERVICE");

}

@Override
public void onStart(Intent intent, int startId) {
    shaker = new Shaker(this, 5.0d, 500, this);
    Log.e("onStart Servicio ", "onStart Servicio ");
}

@Override
public void shakingStarted() {
    shaker = new Shaker(this, 5.0d, 500, this);
    Log.e("shakingstrated SERVICE", "shakingstrated SERVICE");
    run1.cameraIntent();

}


@Override
public void shakingStopped() {
    // TODO Auto-generated method stub

}

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}



}

The error is logcat me back then! And nose like really solve this problem!

04-19 23:26:30.788: ERROR/ONCREATE RUN1(356): ONCREATE RUN1
04-19 23:26:30.808: DEBUG/onCReate RUN1(356): antes de llamar a shakingstrated
04-19 23:26:30.808: DEBUG/AndroidRuntime(356): Shutting down VM
04-19 23:26:30.808: WARN/dalvikvm(356): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
04-19 23:26:30.822: ERROR/AndroidRuntime(356): Uncaught handler: thread main exiting due to uncaught exception
04-19 23:26:30.842: ERROR/AndroidRuntime(356): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sebastian.shakeCam/com.sebastian.shakeCam.Run1}: java.lang.NullPointerException
04-19 23:26:30.842: ERROR/AndroidRuntime(356):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
04-19 23:26:30.842: ERROR/AndroidRuntime(356):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
04-19 23:26:30.842: ERROR/AndroidRuntime(356):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
04-19 23:26:30.842: ERROR/AndroidRuntime(356):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
04-19 23:26:30.842: ERROR/AndroidRuntime(356):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-19 23:26:30.842: ERROR/AndroidRuntime(356):     at android.os.Looper.loop(Looper.java:123)
04-19 23:26:30.842: ERROR/AndroidRuntime(356):     at android.app.ActivityThread.main(ActivityThread.java:4363)
04-19 23:26:30.842: ERROR/AndroidRuntime(356):     at java.lang.reflect.Method.invokeNative(Native Method)
04-19 23:26:30.842: ERROR/AndroidRuntime(356):     at java.lang.reflect.Method.invoke(Method.java:521)
04-19 23:26:30.842: ERROR/AndroidRuntime(356):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-19 23:26:30.842: ERROR/AndroidRuntime(356):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-19 23:26:30.842: ERROR/AndroidRuntime(356):     at dalvik.system.NativeStart.main(Native Method)
04-19 23:26:30.842: ERROR/AndroidRuntime(356): Caused by: java.lang.NullPointerException
04-19 23:26:30.842: ERROR/AndroidRuntime(356):     at com.sebastian.shakeCam.Run1.onCreate(Run1.java:57)
04-19 23:26:30.842: ERROR/AndroidRuntime(356):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-19 23:26:30.842: ERROR/AndroidRuntime(356):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
04-19 23:26:30.842: ERROR/AndroidRuntime(356):     ... 11 more
도움이 되었습니까?

해결책

The onStart(...) method of Service is deprecated.

You should implement onStartCommand (...) instead. See the docs for onStartCommand.

I'm not sure if it will fix your problem though.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top