简体   繁体   中英

Android spinners moving data from activity to another

This is the method that is Sending the data.

public void commitData(View v) {
        Bundle bundle=new Bundle();
        bundle.putString("key", spinner.getItemAtPosition(spinner.getSelectedItemPosition()).toString());
    //  String selection = spinner.getSelectedItem().toString();
        Intent alarmSet = new Intent(getApplicationContext(), ViewOffense.class);
        alarmSet.putExtras(bundle);
        startActivityForResult(alarmSet, 0);
    }

This is the method Receiving the data

public class ViewOffense extends Activity {
    EditOffense eo=new EditOffense();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewoffense);

        Bundle bundle=this.getIntent().getExtras();
        if(bundle != null){
        String selection=bundle.getString("key");
        TextView textview=(TextView)findViewById(R.id.textview);
        textview.setText(selection);
        }
        else{
            Toast.makeText(ViewOffense.this,"Haven't Received any data yet", Toast.LENGTH_LONG).show();
        }
        }

I keep getting null pointer exceptions.

Debug your app to see exactly which variable is null, but from what you have posted it looks like the problem is not the putting and retrieving from the bundle.

If the null pointer is on the setText line, then textview is most like null (you can set null to a text view, so if the selection text was null, it wouldn't matter.

Check that your textview has an id of R.id.textview. If it doesn't, the find view by id won't be able to find your textview.

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