将imageUri保存到数据库中

时间:2016-01-05 02:09:11

标签: java android android-intent android-studio uri

我想将我的图像保存到数据库中,但每当我点击保存我的应用程序崩溃

任何人都可以帮我解释原因吗?

  

保存录入方法

protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        switch (requestCode) {
            case REQUEST_CODE:
                if (data != null) {
                    int id = dbHandler.getEntriesCount();
                    long time = System.currentTimeMillis();
                    SimpleDateFormat dayTime = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
                    String date = dayTime.format(new Date(time));
                    String title = data.getStringExtra("Title");
                    String passage = data.getStringExtra("Passage");
                    Uri imageUri = Uri.parse(data.getStringExtra("imageUri"));
                    Entry entry = new Entry(id, title, passage, imageUri, date);
                    dbHandler.createEntry(entry);
                    Entry Entry = dbHandler.getEntry(id);
                    entryList.add(Entry);
                    EntryListadapter.notifyDataSetChanged();
                }
                break;
  

Save.onCLickListner

 final Button SaveBtn = (Button) findViewById(R.id.btnSave);

    SaveBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent data = new Intent();
            data.putExtra("Title", TitleTxt.getText().toString());
            data.putExtra("Passage", WriteTxt.getText().toString());
            data.putExtra("imageUri", imageUri);
            setResult(RESULT_OK, data);
            finish();
            /*imageTest1.setText(imageUri.toString());*/
            Toast.makeText(getApplicationContext(), "Entry Added", Toast.LENGTH_SHORT).show();
        }
    });
  

错误记录

> 01-05 02:03:50.689 9683-9683/unipersonalinc.mypersonalediary E/AndroidRuntime: FATAL EXCEPTION: main

>01-05 02:03:50.689 9683-9683/unipersonalinc.mypersonalediary E/AndroidRuntime: Process: unipersonalinc.mypersonalediary, PID: 9683

>01-05 02:03:50.689 9683-9683/unipersonalinc.mypersonalediary E/AndroidRuntime: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent { (has extras) }} to activity {unipersonalinc.mypersonalediary/unipersonalinc.mypersonalediary.MainActivity}: java.lang.NullPointerException: uriString

>01-05 02:03:50.689 9683-9683/unipersonalinc.mypersonalediary E/AndroidRuntime:     at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)

>01-05 02:03:50.689 9683-9683/unipersonalinc.mypersonalediary E/AndroidRuntime:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)

>01-05 02:03:50.689 9683-9683/unipersonalinc.mypersonalediary E/AndroidRuntime:     at android.app.ActivityThread.-wrap16(ActivityThread.java)

>01-05 02:03:50.689 9683-9683/unipersonalinc.mypersonalediary E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)

>01-05 02:03:50.689 9683-9683/unipersonalinc.mypersonalediary E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)

>01-05 02:03:50.689 9683-9683/unipersonalinc.mypersonalediary E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148)

>01-05 02:03:50.689 9683-9683/unipersonalinc.mypersonalediary E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417)

>01-05 02:03:50.689 9683-9683/unipersonalinc.mypersonalediary E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)

>01-05 02:03:50.689 9683-9683/unipersonalinc.mypersonalediary E/AndroidRuntime:     at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)

>01-05 02:03:50.689 9683-9683/unipersonalinc.mypersonalediary E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

>01-05 02:03:50.689 9683-9683/unipersonalinc.mypersonalediary E/AndroidRuntime:  Caused by: java.lang.NullPointerException: uriString

>01-05 02:03:50.689 9683-9683/unipersonalinc.mypersonalediary E/AndroidRuntime:     at android.net.Uri$StringUri.<init>(Uri.java:475)

>01-05 02:03:50.689 9683-9683/unipersonalinc.mypersonalediary E/AndroidRuntime:     at android.net.Uri$StringUri.<init>(Uri.java)

>01-05 02:03:50.689 9683-9683/unipersonalinc.mypersonalediary E/AndroidRuntime:     at android.net.Uri.parse(Uri.java:437)

>01-05 02:03:50.689 9683-9683/unipersonalinc.mypersonalediary E/AndroidRuntime:     at unipersonalinc.mypersonalediary.MainActivity.onActivityResult(MainActivity.java:84)

>01-05 02:03:50.689 9683-9683/unipersonalinc.mypersonalediary E/AndroidRuntime:     at android.app.Activity.dispatchActivityResult(Activity.java:6428)

>01-05 02:03:50.689 9683-9683/unipersonalinc.mypersonalediary E/AndroidRuntime:     at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)

>01-05 02:03:50.689 9683-9683/unipersonalinc.mypersonalediary E/AndroidRuntime:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742) 

>01-05 02:03:50.689 9683-9683/unipersonalinc.mypersonalediary E/AndroidRuntime:     at android.app.ActivityThread.-wrap16(ActivityThread.java) 

>01-05 02:03:50.689 9683-9683/unipersonalinc.mypersonalediary E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) 

>01-05 02:03:50.689 9683-9683/unipersonalinc.mypersonalediary E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 

>01-05 02:03:50.689 9683-9683/unipersonalinc.mypersonalediary E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148) 

>01-05 02:03:50.689 9683-9683/unipersonalinc.mypersonalediary E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417) 

>01-05 02:03:50.689 9683-9683/unipersonalinc.mypersonalediary E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 

>01-05 02:03:50.689 9683-9683/unipersonalinc.mypersonalediary E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 

>01-05 02:03:50.689 9683-9683/unipersonalinc.mypersonalediary E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

1 个答案:

答案 0 :(得分:0)

尝试找到“uriString”,它必须位于 MainActivity 中的某个位置。使用Ctrl + f并尝试跟踪它,可能是它未初始化。