错误致命异常:主要

时间:2012-04-24 02:47:09

标签: android camera

我想从数据库启动摄像头名称,我有三个数据库.. 我有这样的代码:

public void startCamera() 
{
    long tim=System.currentTimeMillis();
     SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
     String curTime =df.format(tim);
     System.out.println("Time : " + curTime);

    Cursor c = helper.getKomp(almagId2);
    Cursor ca = helper.getSat(almagId);
    Cursor cb = helper.getUlok(almagId1);
    if(c.moveToFirst()){
        fileName =c.getString(1)+"_"+"kanan"+"_"+System.currentTimeMillis()+ ".jpg";
    }else if(ca.moveToFirst()){
        fileName =ca.getString(1)+"_"+"kanan"+"_"+System.currentTimeMillis()+ ".jpg";
    }else if(cb.moveToFirst()){
        fileName =cb.getString(1)+"_"+"kanan"+"_"+System.currentTimeMillis()+ ".jpg";
    }

    _path=Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera/";


    file = new File(_path, fileName);
    try {
    file.createNewFile();
    } catch (IOException e) {
    e.printStackTrace();
    }               
    System.out.println(file);
    Uri outputFileUri = Uri.fromFile(file);
    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    startActivityForResult(intent, IMAGE_CAPTURE);

    SQLiteDatabase db = helper.getWritableDatabase();
    if(c.moveToFirst()){
        db.execSQL("insert into image (kdstore,image1,tgl_buat) values ('"+c.getString(1)+"','"+file+"','"+curTime+"')");
    }else if(ca.moveToFirst()){
        db.execSQL("insert into image (kdstore,image1,tgl_buat) values ('"+ca.getString(1)+"','"+file+"','"+curTime+"')");
    }else if(cb.moveToFirst()){
        db.execSQL("insert into image (kdstore,image1,tgl_buat) values ('"+cb.getString(1)+"','"+file+"','"+curTime+"')");
    }
    db.close();


}

但是当我想调用方法startCamera时,我得到这样的错误:

04-24 09:29:34.902: ERROR/AndroidRuntime(9411): FATAL EXCEPTION: main
04-24 09:29:34.902: ERROR/AndroidRuntime(9411): java.lang.NullPointerException
04-24 09:29:34.902: ERROR/AndroidRuntime(9411):     at java.io.File.<init>(File.java:168)
04-24 09:29:34.902: ERROR/AndroidRuntime(9411):     at com.sat.alfaloc.Camera.startCamera1(Camera.java:153)
04-24 09:29:34.902: ERROR/AndroidRuntime(9411):     at com.sat.alfaloc.Camera.onClick(Camera.java:319)
04-24 09:29:34.902: ERROR/AndroidRuntime(9411):     at android.view.View.performClick(View.java:2408)
04-24 09:29:34.902: ERROR/AndroidRuntime(9411):     at android.view.View$PerformClick.run(View.java:8816)
04-24 09:29:34.902: ERROR/AndroidRuntime(9411):     at android.os.Handler.handleCallback(Handler.java:587)
04-24 09:29:34.902: ERROR/AndroidRuntime(9411):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-24 09:29:34.902: ERROR/AndroidRuntime(9411):     at android.os.Looper.loop(Looper.java:123)
04-24 09:29:34.902: ERROR/AndroidRuntime(9411):     at android.app.ActivityThread.main(ActivityThread.java:4627)
04-24 09:29:34.902: ERROR/AndroidRuntime(9411):     at java.lang.reflect.Method.invokeNative(Native Method)
04-24 09:29:34.902: ERROR/AndroidRuntime(9411):     at java.lang.reflect.Method.invoke(Method.java:521)
04-24 09:29:34.902: ERROR/AndroidRuntime(9411):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-24 09:29:34.902: ERROR/AndroidRuntime(9411):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-24 09:29:34.902: ERROR/AndroidRuntime(9411):     at dalvik.system.NativeStart.main(Native Method)

我怎么能解决这个问题?谢谢你的反馈:)

以上代码编辑如下:

public void startCamera1() 
{
    long tim=System.currentTimeMillis();
     SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
     String curTime =df.format(tim);
     System.out.println("Time : " + curTime);
     SQLiteDatabase db = helper.getWritableDatabase();

    Cursor c = helper.getKomp(almagId2);
    Cursor ca = helper.getSat(almagId);
    Cursor cb = helper.getUlok(almagId1);
    if(c.moveToFirst()){
        fileName =c.getString(1)+"_"+"kiri"+"_"+System.currentTimeMillis()+ ".jpg";
        _path=Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera/";
        file = new File(_path, fileName);
        try {
        file.createNewFile();
        } catch (IOException e) {
        e.printStackTrace();
        }
        System.out.println(file);

        Uri outputFileUri = Uri.fromFile(file);
        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        startActivityForResult(intent, IMAGE_CAPTURE1);
        db.execSQL("insert into image (kdstore,image1,tgl_buat) values ('"+c.getString(1)+"','"+file+"','"+curTime+"')");
    }
    if(ca.moveToFirst()){
        fileName =ca.getString(1)+"_"+"kiri"+"_"+System.currentTimeMillis()+ ".jpg";
        file = new File(_path, fileName);
        try {
        file.createNewFile();
        } catch (IOException e) {
        e.printStackTrace();
        }
        System.out.println(file);

        Uri outputFileUri = Uri.fromFile(file);
        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        startActivityForResult(intent, IMAGE_CAPTURE1);
        db.execSQL("insert into image (kdstore,image1,tgl_buat) values ('"+ca.getString(1)+"','"+file+"','"+curTime+"')");
    }
    if(cb.moveToFirst()){
        fileName =cb.getString(1)+"_"+"kiri"+"_"+System.currentTimeMillis()+ ".jpg";
        file = new File(_path, fileName);
        try {
        file.createNewFile();
        } catch (IOException e) {
        e.printStackTrace();
        }
        System.out.println(file);

        Uri outputFileUri = Uri.fromFile(file);
        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        startActivityForResult(intent, IMAGE_CAPTURE1);
        db.execSQL("insert into image (kdstore,image1,tgl_buat) values ('"+cb.getString(1)+"','"+file+"','"+curTime+"')");
    }

    db.close();

}

这不是错误,但是活动调用不起作用。如何解决?谢谢:)

1 个答案:

答案 0 :(得分:0)

异常表示File构造函数的一个参数为null。它不能是path_,因为这会导致先前的异常。它必须是filename_。你的三向if-else允许分支的 none 可能是真的,如果没有,那么filename_将为空 - 所以这必须是正在发生的事情。如果没有条件属实,你能否拥有默认文件名 - 即,你可以在链的末尾添加else而不是if吗?