0

This is the main activity from which i'm passing the values to a class called choice.

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

String x="";
for(int i=0;i<count;i++)
{
    x+=songf[i]+" ";
}
Toast.makeText(getApplicationContext(), x, Toast.LENGTH_SHORT).show();
Choice o = new Choice();
o.initializeit(songf,sizef,linkf,indexf,count);
Intent ix=new Intent("com.example.harmony.CHOICE");
startActivity(ix);
}

/*public void doit() 
{
    Choice o = new Choice();
    o.initializeit(songf,sizef,linkf,indexf,count);  
}
*/

This is the class choice

public class Choice extends Activity implements OnClickListener{

Button b;
RadioButton rb1,rb2,rb3,rb4 ;
RadioGroup rg;

static String[] songf = new String[19];
static Integer[] indexf = new Integer[19];
static String[] linkf = new String[19];
static double[] sizef = new double[19];
static int count;

public static void initializeit(String[] song, double[] size, String[] link,Integer[] index, int c) {
    // TODO Auto-generated method stub
    songf=song;
    sizef=size;
    indexf=index;
    linkf=link;
    count=c;
    String x="";
    //x+=Integer.toString(count);
    //Toast.makeText(getApplicationContext(), x, Toast.LENGTH_SHORT).show();
    //for(int i=0;i<count;i++)
    //{
    //  x+=songf[i]+" ";
//  }
//  Toast.makeText(getApplicationContext(), x, Toast.LENGTH_SHORT).show();
}

@Override
 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.choice);

    b   = (Button) findViewById(R.id.download);
    rg  = (RadioGroup) findViewById(R.id.radioGroup1);
    rb1 = (RadioButton) findViewById(R.id.fcfs);
    rb2 = (RadioButton) findViewById(R.id.sjf);
    rb3 = (RadioButton) findViewById(R.id.priority);
    b.setOnClickListener(this);
    MainActivity o = new MainActivity();
    o.doit();

 }

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if(rb1.isChecked())
    {
     for(int i=0;i<count;i++)
     {
        for(int j=0;j<count-i-1;j++)
        {
            if(indexf[j]>indexf[j+1])
            {
                int temp1=indexf[j];
                indexf[j]=indexf[j+1];
                indexf[j+1]=temp1;
                String temp2=linkf[j];
                linkf[j]=linkf[j+1];
                linkf[j+1]=temp2;
                String temp3=songf[j];
                songf[j]=songf[j+1];
                songf[j+1]=temp3;
                double temp4=sizef[j];
                sizef[j]=sizef[j+1];
                sizef[j+1]=temp4;
            }
        }
     }
     String x="";
        for(int i=0;i<count;i++)
        {
            x+=songf[i]+" ";
        }
        Toast.makeText(getApplicationContext(), x, Toast.LENGTH_SHORT).show();
    }
    else if(rb2.isChecked())
    {
        for(int i=0;i<count;i++)
         {
            for(int j=0;j<count-i-1;j++)
            {
                if(sizef[j]>sizef[j+1])
                {
                    double temp1=sizef[j];
                    sizef[j]=sizef[j+1];
                    sizef[j+1]=temp1;
                    String temp2=linkf[j];
                    linkf[j]=linkf[j+1];
                    linkf[j+1]=temp2;
                    String temp3=songf[j];
                    songf[j]=songf[j+1];
                    songf[j+1]=temp3;
                    int temp4=indexf[j];
                    indexf[j]=indexf[j+1];
                    indexf[j+1]=temp4;
                }
            }
         }
        String x="";
        for(int i=0;i<count;i++)
        {
            x+=songf[i]+" ";
        }
        x+=Integer.toString(count)+songf[0]+indexf[0];
        Toast.makeText(getApplicationContext(), x, Toast.LENGTH_SHORT).show();
    }
    else if(rb3.isChecked())
    {
        String x="";
        for(int i=0;i<count;i++)
        {
            x+=songf[i]+" ";
        }
        Toast.makeText(getApplicationContext(), x, Toast.LENGTH_SHORT).show();

    }

}


}

The string values initialized in initializeit are not initialized and giving me null values. I cannot understand the issue. Please help me out.

Sergey Kuryanov
  • 6,114
  • 30
  • 52

8 Answers8

1

You should pass data this way:

Intent intent = new Intent(this, Choice.class); 
intent.putExtra("fname", "My first name");
intent.putExtra("lname", "My last name");
startActivity(intent);

And retrieve data in Choice.class this way:

Intent intent = getIntent();
String fName = intent.getStringExtra("fname");
String lName = intent.getStringExtra("lname");

Use method putExtra("key", value); and getExtra("key"); and you shouldn't create instance of any Activity unless you want to navigate there. See more details doc.

Yurets
  • 3,999
  • 17
  • 54
  • 74
  • To pass an array, do I simply type intent.putExtra("fname",songf[]); ? – Viren Chugh May 01 '15 at 18:01
  • Yes, you can do this for sure. Only one thing, if you want to pass `Object`, you need to wrap it within `Bundle`.http://developer.android.com/reference/android/os/Bundle.html and example of `Bundle` http://stackoverflow.com/questions/768969/passing-a-bundle-on-startactivity – Yurets May 01 '15 at 18:04
  • Don't worry about that. Anyways. if it did solve the problem, please accept the answer, so the question will be closed :) – Yurets May 02 '15 at 14:19
0

Use putExtra() to pass data from one Activity to other

intent.putExtra("KEY", "DATA_TO_BE_SENT");

and in your second activity, you have to retrieve the values using

getIntent().getStringExtra("KEY");

Thus, in your code,

Choice o = new Choice();
o.initializeit(songf,sizef,linkf,indexf,count);
Intent ix=new Intent("com.example.harmony.CHOICE");
startActivity(ix);

has to be replaced with

Intent ix=new Intent("com.example.harmony.CHOICE");
intent.putExtra("KEY", "DATA_TO_BE_SENT");
startActivity(ix);
Lal
  • 14,726
  • 4
  • 45
  • 70
0

If you want to transport some data from one activity to another you must use the Intent.

Intent intent = new Intent(this, Choice.class);
**intent.putExtra()** // insert here your data !
startActivity(intent);

Search more abut this intent.putExtra here http://developer.android.com/training/basics/firstapp/starting-activity.html

Dennis Anderson
  • 1,348
  • 2
  • 14
  • 33
0

The shared elements must be passed by Intent.putExtra

Send :

Intent i = new Intent(this, ActivityTwo.class);
i.putExtra("Value1", "This value one for ActivityTwo ");
i.putExtra("Value2", "This value two ActivityTwo"); 

Receive :

Bundle extras = getIntent().getExtras();
if (extras != null) {
    // get data via the key
    String value1 = extras.getString("Value1");    
    if (value1 != null) {
        // do something with the data
    } 
}
0

The startActivity call will not start the instance of Choice that you have created, you can think of it as sending a message to application to create a new Activity of type Choice.

If you want to pass information to the Activity, it in the "extra" information in the intent. You can then retrieve the information from the extras in onCreate of the Choice activity.

See documentation.

Finn K
  • 620
  • 3
  • 8
0
Intent ix=new Intent(this ,Choice.class);

this may work . And to pass values , u need to add follwing code

ix.putExtra("key_name",value);

than write ix.startActivity(); to retrieve you can use getIntent().getStringExtra("key_name");

Hiren patel
  • 971
  • 8
  • 25
0

You are not passing anything to the new activity you are opening. You need to use putExtra before startActivity and then getExtra in your OnCreate in the other activity.

Use a class Choice that does not extends activity and with a constructor and members.

public class Choice(){
//members
//constructor
}

Choice myChoice = new Choice(songf,sizef,linkf,indexf,count);

Then call the second activity like while passing the extra.

Intent ix=new Intent("com.example.harmony.mysecondactivity");
ix.putExtra(myChoice, strName);
startActivity(ix);

In the other activity get the extra back

private Choice myChoiceFromTheOtherActivity;

@Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.intent);
        Intent i= getIntent();
        Bundle b = i.getExtras();
        myChoiceFromTheOtherActivity = new Choice();
        //you may need to parse this i don't remember
        myChoiceFromTheOtherActivity = extras.getString("myChoice");


    }

http://developer.android.com/reference/android/content/Intent.html

Paperbag Writer
  • 823
  • 1
  • 11
  • 39
0

You're trying to pass data from the main activity to Choice activity? Did I get you right? If so, I recommend using putExtra/s methods of the Intent class. Then, in the Choice activity retrieve the the extras by invoking the getIntent() method of the Activity class, which returns the intent that started the activity. Then use getExtra/s method of the Intent class. Notice that mainly you can put primitive data types, primitive data types arrays, and some other classes which I recommend for you to explore. So, in the MainActivity:

Intent intent = new Intent(this, SomeOtherActivity.class);
intent.putExtra(KEY, new String("some data")); //Remember to check out
// which values you can pass in the Intent documentation
startActivity(intent);

And in the SomeOtherActivity retrieve the data

    Intent intent = getIntent();
    String data = intent.getStringExtra(KEY);
    // Notice that because i passed am String value, I'm using the 
    // corresponding method to retrieve the data. Also in the Intent documentation

There is also a way to pass costume objects, by making them to implement the Serializable class, and passing them using the putExtra(String key, Serializable value) method, and retrieving the data by invoking the Intent.getSerializableExtra(String key) method.

I recommend you to go over some tutorials on the subject. Here is one of my favoriets, Vogella tutorials. Intent tutorial. Good luck!