-2

I want to open a class and a method of that class from another class simultaneously. When i click on button it stops the application. Help!

Method of class 'Fixtures' from which I want to call class and Method

public void onClick(View arg0) {
    // TODO Auto-generated method stub

    int id = arg0.getId();
    FixtureDetails abc = new FixtureDetails();
    abc.xyz(id);
    startActivity(new Intent(Fixtures.this, FixtureDetails.class));
}

Class and method which I want to be opened

public class FixtureDetails extends Activity{

TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fixturedetails);
    tv = (TextView) findViewById(R.id.tv);
}

void xyz(int lmn)
{
    switch(lmn)
    {
    case R.id.tvMatch1:
        tv.setText("Hey there, wassup");
        break;
    }
}
}
Anuj
  • 39
  • 2
  • 6
  • yes you can do that.. make it public void xyz(){}.. thats it.. is that what you looking for? – Elltz Aug 29 '14 at 18:38
  • making it public is not doing any better. I think the problem is when I call the method, I have not created the Intent yet, so it is not referring to the fixturedetails.xml file and hence not setting the text. – Anuj Aug 29 '14 at 18:44

1 Answers1

0

Because Android handles the lifecycles of Activity classes it's not recommended to instantiate it directly, and calling that method like you are Android will recreate the class anyways destroying anything you changed in it.

The recommended way to do what you want is to use Intent Extras to pass data to your Activity.

public void onClick(View arg0) {
    // TODO Auto-generated method stub

    int id = arg0.getId();
    Intent intent = new Intent(Fixtures.this, FixturesDetails.class);
    intent.putExtra("id_key", id); // Set your ID as a Intent Extra
    startActivity(intent);
}

FixtureDetails.class

public class FixtureDetails extends Activity{

    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fixturedetails);
        tv = (TextView) findViewById(R.id.tv);
        Intent intent = getIntent();
        if(intent != null && intent.getExtras() != null) {
            xyz(intent.getIntExtra("id_key", -1)); // Run the method with the ID Value
                                                   // passed through the Intent Extra
        }
    }

    void xyz(int lmn) {
        switch(lmn) {
            case R.id.tvMatch1:
                tv.setText("Hey there, wassup");
                break;
        }
    }
}
Pedlar
  • 1,034
  • 10
  • 7