-1

This line is throwing a null pointer exception error:

String dataPath = intent.getData().toString();

Does anyone know why? Below is a snippet of my main activity. Everything looks alright.

   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contact_picker);
    Intent intent = getIntent();

    String dataPath = intent.getData().toString();
    final Uri data = Uri.parse(dataPath+"people/");
    final Cursor c = managedQuery(data, null, null, null, null);

    String[] from = new String[] {People.NAME};
    int[] to = new int[]  {R.id.itemTextView };

     SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,                                   
    R. layout.listitemlayout, c, from, to);
    ListView lv = (ListView)findViewById(R.id.contactListView);
    lv.setAdapter(adapter);
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int pos,
                long id) {
            c.moveToPosition(pos);
            int rowId = c.getInt(c.getColumnIndexOrThrow("_id"));

            Uri outURI = Uri.parse(data.toString() + rowId);
            Intent outData = new Intent();
            outData.setData(outURI);
            setResult(Activity.RESULT_OK, outData);
            finish();
        }

    });
   }
Sudhir Mishra
  • 578
  • 2
  • 16

1 Answers1

1

To invoke another activity generally we use intent like this :

first activity to send :

Intent in = new Intent(MainActivity.this, NewActivity.class);   
String StrName = "xyz"
in.putExtra("data",StrName);
startActivity (in);

to receive in another activity:

String Sname = getIntent().getExtras().getString("data");

to see the details of intents refer here : http://developer.android.com/guide/components/intents-filters.html

  • what's wrong with using `setData`, especially since the OP is clearly working on an Uri? – njzk2 Apr 07 '14 at 17:07
  • I think this explains a bit : http://stackoverflow.com/questions/18794504/intent-setdata-vs-intent-putextra http://stackoverflow.com/questions/19151692/difference-between-putextra-and-setdata – Shashank Chavan Apr 07 '14 at 19:47
  • yes, and in the case of this question, the use of `setData` seems relevant. – njzk2 Apr 07 '14 at 19:49