1

I'm trying to call a function present in one class from another class by creating its object. Somehow it's not working. The new activity doesn't load.

My java code:

public class MessagesActivity extends TabActivity {

    public WorkEntryScreenActivity workEntryObject = new WorkEntryScreenActivity() ;

    public void AddWorkEntryClick(View v) {
        workEntryObject.newWorkEntry();
    }
}

The other class:

public class WorkEntryScreenActivity extends Activity {

public void newWorkEntry() {
    try {
        Intent i = new Intent(this, WorkEntryActivity.class);
        i.putExtra("CurDate", mDateDisplay.getText());
        i.putExtra("DD", String.valueOf(mDay));
        i.putExtra("MM", String.valueOf(mMonth));
        i.putExtra("YYYY", String.valueOf(mYear));
        startActivity(i);
        finish();
    } catch (Exception e) {
        System.out.println("Exception" + e.getStackTrace());
        Log.d(TAG, "Exception" + e.getStackTrace());
    }
}
 }
Harsh
  • 487
  • 3
  • 9
  • 29
  • 1
    http://stackoverflow.com/questions/10997996/why-arent-getters-preferred-when-accessing-variables-between-activities/10998128#10998128 Possibly related, w.r.t. answers.. – Kristopher Micinski Jun 13 '12 at 19:06
  • 1
    The fact you're creating an instance of Activity (as if it were usual Java) tells you are missing Android basics. Read the basics guide first (biovamp points the link). – Vit Khudenko Jun 13 '12 at 19:28
  • @Arhimed I'm new to android and I've been reading about it as much as got a chance to since then. Though I still couldn't figure out a way to call a function present in another class. I've come across a few examples where people do create an instance of an activity, like I noticed in switching tabs most people use `MyTabActivity myTab = (MyTabActivity) getParent();` I tried even this: `public void AddWorkEntryClick(View v) { WorkEntryScreenActivity object = (WorkEntryScreenActivity) getParent(); object.newWorkEntry(); }` didn't work! Any suggestions? – Harsh Jun 19 '12 at 14:30
  • @Harsh: in most cases (I'm not familiar with TabActivity) activities to do not communicate directly (as it would be in a standard Java), but they can communicate via `Activity.startActivityForResult()` method. – Vit Khudenko Jun 19 '12 at 15:28

2 Answers2

1

You must create your workEntryObject first (it's not C++). Like this

public WorkEntryScreenActivity workEntryObject=new WorkEntryScreenActivity();

Also, I highly recommed you to read Android Basics http://developer.android.com/guide/index.html

Dmitry Zaytsev
  • 23,650
  • 14
  • 92
  • 146
  • Wow! I've lost it. Anyway thanks. Now it doesn't give me any errors but it doesn't load the new activity – Harsh Jun 13 '12 at 19:15
  • To create new Activity use `startActivity()` method of your current Activity. You can read about this here http://developer.android.com/guide/topics/fundamentals/activities.html – Dmitry Zaytsev Jun 13 '12 at 19:42
0

@biovamp is correct. It looks like you have a null reference that you're trying to call a method on. In order to call a non-static method, you need a instance of that object.

From the naming of your method, it looks like you might be trying to re-use some of your UI in another part of your application. In Android, the way to accomplish that is through Intents and Activities. If you're not familiar with those or how to use them, I would highly suggest researching them.

Wesley Wiser
  • 9,491
  • 4
  • 50
  • 69