Hi,
I'm trying to make a simple menu for adding another contact to a list.
On the ListView I have a button on a footer which uses
startActivityForResult to create a new menu (called add_dialog). This
is a simple EditText with an OK button and a Cancel button. The idea
is that clicking the OK button will pass the string from the EditText
back to the main Activity.

When the add_dialog Activity finishes and onActivityResult tries to
read the string extra it throws a null pointer exception.

The debugger gives the following line:
E/AndroidRuntime(  410): java.lang.RuntimeException: Failure
delivering result ResultInfo{who=null, request=0, result=-1,
data=null} to activity {com.test.hello_listview/
com.test.hello_listview.hello_listview}:
java.lang.NullPointerException

Can someone please explain why data is always null? Am I not using
getIntent().putExtra() correctly?

The source for the main Activity is:

public class hello_listview extends Activity
{
    private Log logger;
    private static final String TAG = "hello_listview";

    private String[] Pals;
    private ArrayAdapter<String> adapter;
    private Button addButton;
    private String palName = "";
    private static final int ADD_DIALOG = 0;
    private static final int MAP_VIEW = 1;
    Intent intent_addPal;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.palslist);
        Pals = getResources().getStringArray(R.array.pals);
        Arrays.sort(Pals);

        ListView list = (ListView) findViewById(R.id.list);
        adapter = new ArrayAdapter<String>(this, R.layout.listitem);
        for (int i = 0; i < Pals.length; i++) {
            adapter.add(Pals[i]);
        }
        list.setAdapter(adapter);

        addButton = (Button) findViewById(R.id.addButton);
        addButton.setOnClickListener(addButton_clicked);
        intent_addPal = new Intent(this,
com.test.hello_listview.add_dialog.class);
    }

    OnClickListener addButton_clicked = new OnClickListener() {
        @Override
        public void onClick(View v) {
            logger.i(TAG, "add clicked");
            startActivityForResult(intent_addPal, ADD_DIALOG);
        }
    };

    @Override
    public void onActivityResult(int requestCode, int resultCode,
Intent data) {
        if ((resultCode == Activity.RESULT_OK) && (requestCode ==
ADD_DIALOG)) {

                    logger.i(TAG, "add okay. data = " +
data.getStringExtra("PALNAME"));            // THIS LINE HERE

        }
    }

} // End of hello_mapview class


The source for the add_dialog Activity is:

public class add_dialog extends Activity {
    EditText addText;
    Button cancelButton;
    Button doneButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_dialog);

        addText = (EditText) findViewById(R.id.addText);
        cancelButton = (Button) findViewById(R.id.cancelButton);
        doneButton = (Button) findViewById(R.id.doneButton);

        cancelButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                 setResult(RESULT_CANCELED);
                 finish();
            }
         });

        doneButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                getIntent().putExtra("CONTACTNAME",
addText.getText());                                   // ALSO THIS
LINE HERE
                setResult(RESULT_OK);
                finish();
            }
        });


    } // End of onCreate

} // End of add_dialog class

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to