尝试获取图像路径时java null指针异常

时间:2011-12-27 16:26:37

标签: android

我正在处理联系人列表应用程序,当我尝试在编辑文本中获取图像的路径时出现错误:

12-27 18:12:37.909: E/AndroidRuntime(2620): Caused by: java.lang.NullPointerException
12-27 18:12:37.909: E/AndroidRuntime(2620):     at org.example.dbcontactconsole.ContactDetails.onCreate(ContactDetails.java:54)
12-27 18:12:37.909: E/AndroidRuntime(2620):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-27 18:12:37.909: E/AndroidRuntime(2620):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)

ContactDetails.java来源是:

package org.example.dbcontactconsole;

import android.app.Activity;
import android.app.AlertDialog;                                                                            
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class ContactDetails extends Activity{
    private Button cancelButton;
    private Button saveContactButton;
    private static EditText poza;
    private static final int SELECT_PICTURE = 1;
    private static EditText nameField; 
    private static EditText mobilePhoneField;
    private static EditText emailField;
    private static Uri currImageURI;

    private static String operationType;
    static final int RESULT_MODIFY_USER = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contactdetails);
         Button sButton = (Button) findViewById(R.id.button1);
         sButton.setOnClickListener(new OnClickListener() {

              public void onClick(View arg0) {
                  // in onCreate or any event where your want the user to
                  // select a file
                  Intent intent = new Intent();
                  intent.setType("image/*");
                  intent.setAction(Intent.ACTION_GET_CONTENT);
                  startActivityForResult(Intent.createChooser(intent,
                          "Select Picture"), SELECT_PICTURE);
              }
          });
        Bundle incomingActionType = getIntent().getExtras();
        operationType = incomingActionType.getString("mod_type");

        if (operationType.equals("addPerson")){

            nameField = (EditText) findViewById(R.id.contactName); 
            mobilePhoneField = (EditText) findViewById(R.id.contactmobile);
            emailField = (EditText) findViewById(R.id.contactemail);
            poza=(EditText) findViewById(R.id.editText1);
            poza.setText(currImageURI.toString());
        }

        else if (operationType.equals("modifyPerson")){

            String modifyFirstName = incomingActionType.getString("cFirstName");
            nameField = (EditText) findViewById(R.id.contactName);
            nameField.setText(modifyFirstName);


            String modifyPhoneNumber = incomingActionType.getString("cMobilePhone");
            mobilePhoneField = (EditText) findViewById(R.id.contactmobile);
            mobilePhoneField.setText(modifyPhoneNumber);

            String modifyEmail = incomingActionType.getString("cEmail");
            emailField = (EditText) findViewById(R.id.contactemail);
            emailField.setText(modifyEmail);    
        }


    cancelButton = (Button) findViewById(R.id.cancelcontactbutton);
    saveContactButton = (Button) findViewById(R.id.savecontactbutton);

    saveContactButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            Intent resultIntent = new Intent();
            Bundle contactData = new Bundle();

                if (nameField.getText().toString().length() == 0){
                    new AlertDialog.Builder(ContactDetails.this).setTitle("Error").setMessage("First name field cannot be empty!").setNeutralButton("Close", null).show();
                } else if (mobilePhoneField.getText().toString().length() == 0){
                    new AlertDialog.Builder(ContactDetails.this).setTitle("Error").setMessage("Mobile Phone field cannot be empty!").setNeutralButton("Close", null).show();
                } else if (emailField.getText().toString().length() == 0){
                    new AlertDialog.Builder(ContactDetails.this).setTitle("Error").setMessage("Email field cannot be empty!").setNeutralButton("Close", null).show();
                } else{
                    contactData.putString("contactFirstName", nameField.getText().toString());
                    contactData.putString("contactMobilePhone", mobilePhoneField.getText().toString());
                    contactData.putString("contactEmail", emailField.getText().toString());

                    resultIntent.putExtra("contactData", contactData);

                    if (operationType.equals("addPerson")){
                        setResult(RESULT_FIRST_USER, resultIntent);
                    }else if (operationType.equals("modifyPerson")){
                        setResult(RESULT_MODIFY_USER, resultIntent);
                    }               
                    finish();
                }
        }
    });

    cancelButton.setOnClickListener(new View.OnClickListener(){


        public void onClick(View v) {
            Intent resultIntent = new Intent();
            setResult(RESULT_CANCELED, resultIntent);
            finish();


        }
    });

    }
    public void onActivityResult(int requestCode, int resultCode, Intent data) {  
           if (resultCode == RESULT_OK) {
               if (requestCode == 1) {
                   // currImageURI is the global variable I'm using to hold the content:// URI of the image
                    currImageURI = data.getData();
                   getRealPathFromURI(currImageURI);

               }
             }
           }


        public String getRealPathFromURI(Uri currImageURI) {
           // can post image
           String [] proj={MediaStore.Images.Media.DATA};
           Cursor cursor = managedQuery( currImageURI,
                   proj, // Which columns to return
                   null,       // WHERE clause; which rows to return (all rows)
                   null,       // WHERE clause selection arguments (none)
                   null); // Order-by clause (ascending by name)
           int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
           cursor.moveToFirst();


              poza=(EditText)findViewById(R.id.editText1);
               poza.setText(currImageURI.toString());
              return cursor.getString(column_index);

        }
  }

第54行是:poza.setText(currImageURI.toString()); 我认为错误出现是因为currimageURI没有值,但我制作了onActivityResult方法。你能帮帮我吗?

3 个答案:

答案 0 :(得分:2)

不确定。你写了onActivityResult方法,那又怎样? onCreate之后调用该方法(如果调用),currImageURI在执行onCreate时为空。

请勿在{{1​​}}中执行此操作。当onCreate不为空时,即在currImageURI方法中,请执行此操作。

答案 1 :(得分:0)

在您的方法onActivityResult(...)中,您将变量currImageURI重新声明为局部变量,从而隐藏字段:

Uri currImageURI = data.getData(); // hiding the field currImageURI

相反,你应该

currImageURI = data.getData(); // now it's fine

答案 2 :(得分:0)

public void onActivityResult(int requestCode, int resultCode, Intent data) 
{  
       if (resultCode == RESULT_OK) 
       {
           if (requestCode == 1) 
           {
               // currImageURI is the global variable I'm using to hold the content:// URI of the image
               //Uri currImageURI = data.getData(); Replace this as follows
               currImageURI = data.getData();
               getRealPathFromURI(currImageURI);

           }
         }
       }

您在currImageURI中重新声明变量onActivityResult两次。