简体   繁体   中英

Intents keep coming back null

So Im making a simple organization app and once I getIntent() and set variables to whatever i used.putExtra() on and it just wont transmit the data between views, could it be that I copied the code from a different app I've made and adapted it or what? because when I do getStringExtra it comes up as null (which i've tested by setting some buttons text to the getStringExtra and it shows up blank)and doesnt have a value. Even though everything should be correct, can someone just look this over please and tell me whats up?

code from main activity

  @Override
public void onResume() {
    super.onResume();
   if(test==1){
        Intent itemCreate = getIntent();
        itemName = itemCreate.getStringExtra(CreateItem.NAME_TEXT);
        itemLocation = itemCreate.getStringExtra(CreateItem.LOCATION_TEXT);
        itemDesc = itemCreate.getStringExtra(CreateItem.DESC_TEXT);
        items.add(new Item(itemName, itemLocation, itemDesc));
        adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, items);
       // itemNames.add(items.get(items.size()-1).getName());
        addItem.setText(itemName);
       toast.show();
       if(items!=null) {
           lv.setAdapter(adapter);
       }                //toast.show();
    }
}

code from sending activity

public void openMainActivity() {
    editName = (EditText) findViewById(R.id.itemNameText);
    editLoc = (EditText) findViewById(R.id.itemLocationText);
    editDesc = (EditText) findViewById(R.id.itemPriceText);
    String name = editName.getText().toString();
    String location = editLoc.getText().toString();
    String desc = editDesc.getText().toString();
    Intent itemCreate = new Intent(this, MainActivity.class);
    itemCreate.putExtra(NAME_TEXT, name);
    itemCreate.putExtra(LOCATION_TEXT, location);
    itemCreate.putExtra(DESC_TEXT, desc);
    itemCreate.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    startActivity(itemCreate);
}

In the receiving activity have you tried as follows:

@Override
public void onResume() {
    super.onResume();
   if(test==1){
        Intent itemCreate = getIntent();

        Bundle bundle = intent.getExtras();

        if (bundle != null) {
            itemName = itemCreate.getString(CreateItem.NAME_TEXT);
            itemLocation = itemCreate.getString(CreateItem.LOCATION_TEXT);
            itemDesc = itemCreate.getString(CreateItem.DESC_TEXT);
            items.add(new Item(itemName, itemLocation, itemDesc));
            adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, items);
            // itemNames.add(items.get(items.size()-1).getName());
            addItem.setText(itemName);
            toast.show();
        }

       if(items!=null) {
           lv.setAdapter(adapter);
       }                //toast.show();
    }
}

Try this: For checking if your string in being received or not, you can use Logcat:

////add this on receiving activity, in Logcat search using the key "aaaaaa"
  ////if you received it, it will show you the received text in logcat .
    String s = getIntent().getStringExtra("NAME_TEXT");
    Log.i("aaaaaaa",s);

/////put the key under double quotation/////
public void openMainActivity() {
    editName = (EditText) findViewById(R.id.itemNameText);
    editLoc = (EditText) findViewById(R.id.itemLocationText);
    editDesc = (EditText) findViewById(R.id.itemPriceText);
    String name = editName.getText().toString();
    String location = editLoc.getText().toString();
    String desc = editDesc.getText().toString();
    Intent itemCreate = new Intent(this, MainActivity.class);
    itemCreate.putExtra("NAME_TEXT", name);
    itemCreate.putExtra("LOCATION_TEXT", location);
    itemCreate.putExtra("DESC_TEXT", desc);
    itemCreate.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    startActivity(itemCreate);
}

in receiving activity: make sure that getStringExtra does not get null value, you can use the getIntent() method directly.

 @Override
public void onResume() {
    super.onResume();
   if(test==1){
        itemName = getIntent().getStringExtra("NAME_TEXT");
        itemLocation = getIntent().getStringExtra("LOCATION_TEXT");
        itemDesc = getIntent().getStringExtra("DESC_TEXT");
        items.add(new Item(itemName, itemLocation, itemDesc));
        adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, items);
       // itemNames.add(items.get(items.size()-1).getName());
        addItem.setText(itemName);
       toast.show();
       if(items!=null) {
           lv.setAdapter(adapter);
       }                //toast.show();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM