0

I have created an Activity class and defined a constructor:

   public class Spinner_Name extends Activity{

        List<String> listSp;
        public Spinner_Name(List<String> list)
        {
            listSp = new ArrayList<String>();
            listSp = list;
        }


        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.spinner_layout);

        }
}

And I want to call that Activity from this Activity:

 public class MAIN extends Activity{

            List<String> listSp;


            @Override
            public void onCreate(Bundle savedInstanceState)
            {
                // TODO Auto-generated method stub
                super.onCreate(savedInstanceState);
                setContentView(R.layout.spinner_layout);
                 listSp = new ArrayList<String>();
                  listSp.add("a");

            }
    }

But when I run the application this message shows up:

Unfortunately, Main has been stopped

When I delete the argument from the constructor the application runs successfully. How do I get the activity to run with the argument??

John
  • 3,769
  • 6
  • 30
  • 49
oop12
  • 17
  • 5

3 Answers3

0

1) "You never construct an activity directly, so you can't use it to pass in parameters." You do things on onCreate().

2) You can send parameters from one activity to another using Bundle, for example send int:

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
Bundle b = new Bundle();
b.putInt("key", 1); //Your id
intent.putExtras(b); //Put your id to your next Intent
startActivity(intent);
finish();

and get that value from new opened activity:

Bundle b = getIntent().getExtras();
int value = b.getInt("key");

3)To send List, you need to copy list into array and send array:

List<String> a;
String[] b = new String[a.size()];
a.toArray(b);
Bundle c=new Bundle();
c.putStringArray("myList", b);     
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtras(c)
startActivity(intent);
finish();

Get array (check):

Bundle b=getIntent().getExtras();
String[] myNewList=b.getStringArray("myList");
Community
  • 1
  • 1
Jemshit
  • 9,501
  • 5
  • 69
  • 106
0

You should not write constructors for classes extending Activity. You should initialize any fields either where they are declared or in onCreate(). If you need to pass an ArrayList from one activity to another, you should use putExtra to store the ArrayList with an Intent and getExtra to retrieve it back again.

Paul Boddington
  • 37,127
  • 10
  • 65
  • 116
0

I think this isn't the solution for your problem, but the two lines don't make sense:

listSp = new ArrayList<String>();
listSp = list;

You create a new ArrayList-Object and afterwards overwrite immediately the reference. So the garbage collector will throw it away.

Baschdl
  • 375
  • 3
  • 15
  • Currently you have two objects: the parameter-ArrayList-object, which is forwarded in the constructor and the one you create with the line `listSp = new ArrayList();` – Baschdl Apr 10 '15 at 17:14
  • I ' m not shure.Baschdl,I once delete the implementation code in constructor and run it,but i see error of this!!!! – oop12 Apr 10 '15 at 17:17
  • I think it should look like this: `List listSp; public Spinner_Name(List list) { listSp = list; } ` – Baschdl Apr 10 '15 at 17:19
  • this can been according of your suggest.ok,but no problem create, – oop12 Apr 10 '15 at 17:23
  • Sorry, I don't understand this? – Baschdl Apr 10 '15 at 17:24
  • It mean this: your suggest code is correctly and it is beeter and my code is coorect and no problem create accroding to your comment, but problem is in other ... – oop12 Apr 10 '15 at 17:32
  • This code lines aren't responsible for the bug, but will cause other problems. – Baschdl Apr 11 '15 at 09:32