2012-04-24 163 views
0

我想从数据库启动相机的名字,我有三个数据库.. 我有这样的代码:错误致命异常:主要

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(); 

} 

这是没有错误,但活动的调用不work..how解决呢?谢谢:)

+0

删除此行System.out.println(file); – 2012-04-24 02:49:48

+0

@Agarwal我试图删除,但仍然error..error在这一行file = new File(_path,fileName);' – akubabas 2012-04-24 03:02:30

+0

@ Agarwal的评论没有任何意义 - 忽略。 – 2012-04-24 03:03:07

回答

0

例外说的参数传递给File构造一个为空。它不能是path_,因为这会导致以前的异常。它必须是filename_。您的三路if-else允许分支机构的为真,如果没有,那么filename_将为空 - 所以必须是发生了什么。如果没有一个条件是真的,你可以有默认的文件名 - 例如,你可以在链的末尾添加一个没有ifelse

+0

我编辑我的代码就像上面..没有错误,但相机活动并没有启动..我解决它??谢谢你:) – akubabas 2012-04-24 03:57:12

+0

这并不奇怪,我们已经怀疑每个'if'条件都被评估为false。现在你已经做好了,所以如果情况如此,代码根本就不运行......所以现在它根本不运行。它看起来像你所有的'光标'是空的。回过头来看看那些'helper'方法,找出你的查询是否有问题。 – 2012-04-24 04:03:48

+0

是的,我试图操纵我的查询它的工作!!谢谢!! :) – akubabas 2012-04-24 04:54:02

相关问题