简体   繁体   中英

Application crashes when I use intent to retrieve data from another activity

I have two activities MainActivity and MenuActivity. I need the ArrayList in Menu Activity to be passed to Main Activity. I did this using intent.

MenuActivity.java

    public class MenuActivity extends ActionBarActivity {

    Button add,save;
     EditText subject;
     ArrayList list = new ArrayList();
     protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.subject_main);
     add = (Button) findViewById(R.id.button1);
     subject = (EditText) findViewById(R.id.editText1);
     save= (Button) findViewById(R.id.button2);




     add.setOnClickListener(new OnClickListener() {


         Context context;

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String content;
             content = subject.getText().toString();

             MainActivity.myStringList.add(content);
            ArrayAdapter dataAdapter = new ArrayAdapter(MenuActivity.this,android.R.layout.simple_spinner_item, list);

            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

              //spinner1.setAdapter(dataAdapter);
             // Showing selected spinner item
                   Toast.makeText(getApplicationContext(),
                            content +"Added", Toast.LENGTH_LONG).show();


        }
       });
     save.setOnClickListener(new OnClickListener() {


         Context context;

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent i1 = new Intent(MenuActivity.this, MainActivity.class);
            i1.putStringArrayListExtra("LIST",list);

             startActivity(i1);


        }
       });



}
}

The Arraylist list needs to be used in MainActivity

MainActivity.java

public class MainActivity extends ActionBarActivity {
 Spinner spinner1,spinner2,spinner3;
 public static List myStringList = new ArrayList();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if(myStringList.isEmpty())
    {
        Intent myIntent = new Intent(MainActivity.this, MenuActivity.class);

        MainActivity.this.startActivity(myIntent);

    }
 // Spinner element
   spinner1 = (Spinner) findViewById(R.id.spinner1);
   spinner2 = (Spinner) findViewById(R.id.spinner2);
   spinner3 = (Spinner) findViewById(R.id.spinner3);





    ArrayAdapter dataAdapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_spinner_item, myStringList);

    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(dataAdapter);


// Spinner click listener

   //spinner2.setOnItemSelectedListener(this);
   //spinner3.setOnItemSelectedListener(this);
   spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

       @Override
       public void onItemSelected(AdapterView<?> adapter, View v,
               int position, long id) {
           // On selecting a spinner item
           String item = adapter.getItemAtPosition(position).toString();

       }   

       @Override
       public void onNothingSelected(AdapterView<?> arg0) {
           // TODO Auto-generated method stub

       }
   });


    }
public void addListenerOnSpinnerItemSelection() {
    spinner1 = (Spinner) findViewById(R.id.spinner1);
    spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
            //do something here
        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            //optionally do something here
        }
    });
  }



@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.




    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.


        switch (item.getItemId())
        {
        case R.id.settings:
            // Single menu item is selected do something
            // Ex: launching new activity/screen or show alert message

            return true;

        case R.id.help:

            return true;

        case R.id.subjects:
            Intent myIntent = new Intent(MainActivity.this, MenuActivity.class);

            MainActivity.this.startActivity(myIntent);

            return true;


        default:
            return super.onOptionsItemSelected(item);
        }
    }    

How can I display the ArrayList in MainActivty?

Is MainActivity the first activity that occurs when you start your app? If it is, when onCreate runs the first time, list1 will not be populated. I'm assuming that your MainActivity calls your MenuActivity at some point, and then your MenuActivity returns to the MainActivity by using an intent, at which point the MainActivity updates the spinners? Something else you could try is before your MainActivity's onCreate, type:

public static List<String> myStringList = new ArrayList<String>();

and when you are adding to the list in MenuActivity, use MainActivity.myStringList.add(content);

Then when you are done adding to the list in MenuActivity, you could call finish(); to return back to MainActivity, where you could update the spinners by overriding the onResume method. Example - in your MainActivity below the onCreate method:

@Override
public void onResume(){
    super.onResume();
    try{
        spinner1 = (Spinner) findViewById(R.id.spinner1);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, myStringList.toArray(), android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner1.setAdapter(adapter);
    } catch (RuntimeException e){
        Log.d("error",e+"");
    }
}

This is from memory so I'm sorry if it has some issues. The runtimexception is so that it doesn't crash the first time that onResume is called (before anything else is initialized).

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