0

My activity class contains a constructor that computes some data:

    public IPrintPanelActivity(String title, Object data, byte logoChar,
        String keyName, boolean printNCopies, boolean showPrintButton) {
    /*
     * Configure the panel
     */
    super();
    panelTitle = title;
    this.logoChar = logoChar;

    if (data != null) {
        setTextArea((String) data);
    }
    //put the display print panel here
    SignOnActivity.startMyActivity(context,(String) data,"CORRECT?");

    finish();
}

Then I need this data Object (which is actually a string) to display it in a TextView of the associated layout file. The problem is that I don't know how to get data "out of the constructor" to write something like

myTextView.setText(data);

I found an answer to a question that was asked more than 2 years ago and it seems to be what I need. The problem is that I get a NullPointerException for the context variable.

Here's the definition of the static function startMyActivity:

    public static void startMyActivity(Context context, String paramA, String paramB) {
    // Build extras with passed in parameters
    Bundle extras = new Bundle();
    extras.putString("PARAM_A", paramA);
    extras.putString("PARAM_B", paramB);

    // Create and start intent for this activity
    Intent intent = new Intent(context,IPrintPanelActivity.class);
    intent.putExtras(extras);
    context.startActivity(intent);
}

Do I give you enough information? Please let me know and please help me fix the NullPointerException.

Monica
  • 389
  • 1
  • 7
  • 19
  • is this `startMyActivity` a Activity class?? – Raghunandan Apr 13 '14 at 20:00
  • it is a method that I define in another activity, the one that calls the activity with the constructor. – Monica Apr 13 '14 at 20:03
  • You are using an `Activity` in a wrong way. First, you probably shouldn't define a constructor for an `Activity` - use `onCreate` instead. – d3dave Apr 13 '14 at 20:03
  • You don't need to have constructors explicitly for Activities coz you don't instantiate them. You declare them in manifest – Raghunandan Apr 13 '14 at 20:04
  • Well, it happens that I have to work with code written by someone else. – Monica Apr 13 '14 at 20:05
  • @Monica you have intent and you can pass data between Activities. You need to change the code written by someone – Raghunandan Apr 13 '14 at 20:06
  • Yes, I know about intents, but I don;t have this data when I want to use an intent. Do you all mean that I have to get rid of all the constructors in the code and use intents instead? – Monica Apr 13 '14 at 20:08
  • @Monica if you have constructors for Activities get rid of them. Secondly what is that you are doing can you elaborate – Raghunandan Apr 13 '14 at 20:09
  • It is quite a big project. At this point I need to use information (data) that is obtained by instantiating a java Class and this data needs to be written on the screen. The java class is not an activity, it is just a Java class in my project. – Monica Apr 13 '14 at 20:12
  • @Monica `IPrintPanelActivity` is an Activity class and you have a constructor for the same. get rid of it. Well i still not sure what you want. – Raghunandan Apr 13 '14 at 20:15
  • Th constructor in the IPrintPanelActivity is used in another java class which is not an activity. (But I can make it to be an activity, if necessary.) – Monica Apr 13 '14 at 20:17
  • @Monica well you can't have constructor for Activities forget where is used good luck – Raghunandan Apr 13 '14 at 20:19
  • Just as a note: all of that code in the constructor can be pulled out and put in place of the Activity's instantiation in whatever class is calling it. It obviously has access to everything needed to start the Activity correctly. – Mike M. Apr 13 '14 at 20:19
  • Then, when the `IPrintPanelActivity` starts, retrieve the params with `getIntent().getExtras().getString()` in its `onCreate()`. Or just use the regular `putExtra()` and `getStringExtra()` methods for Intents. – Mike M. Apr 13 '14 at 20:22
  • Ok, I shall probably try to get rid of the constructor, but here's the question and the answer by @Chase http://stackoverflow.com/questions/8725530/android-activity-class-constructor-working – Monica Apr 13 '14 at 20:22
  • @Chase seems to be able to use constructor for an activity. – Monica Apr 13 '14 at 20:29
  • Ok, Thanks everybody for your answers. – Monica Apr 13 '14 at 20:35

2 Answers2

2

Do not create constructors for your Activity classes. The Android OS is responsible for instantiating Activities and will only try to do so with the default constructor. Keep in mind that an Activity may be destroyed and recreated by the system at various times, usually during configuration changes, such as when the device is rotated between portrait and landscape orientations.

Any "arguments" you pass to an Activity should be done using a Bundle in the Intent that starts the Activity. In one of the lifecycle callback methods (e.g. onCreate()) you can call getIntent() and check its extras for data, then do as you wish.

The link you posted where the user Chase create d static method for starting an activity still follows these guidelines. All his method does is compose the Intent and its extras using the arguments of the static method, then calls startActivity using this Intent. He did not create a constructor for the Activity, nor did he ever call new to instantiate an Activity. He just simplified the process of creating the Intent and its extras to start the Activity with the proper data.

Karakuri
  • 38,365
  • 12
  • 84
  • 104
  • Thank you for explanations @Karakuri. I just tried to use the example code written by Chase but it gives me a NullPointerException. Could you help me understand why I get this error? – Monica Apr 13 '14 at 21:11
  • The vast majority of the time a `NullPointerException` occurs because you used the `.` operator to invoke a method or access a member variable of an object, but that object is `null` (not initialized) at that point in time. If you look in the Logcat and find the exact line the exception occurs, figure out which object(s) could be `null` and make changes so that they will not be `null`. – Karakuri Apr 13 '14 at 22:56
  • Yes, I know the context is null. And probably it is because I refer to it indirectly through a constructor. According to what has been discussed here, this is expected. But I have to figure out how to get rid of the constructors. – Monica Apr 13 '14 at 23:51
  • Instead of calling `new IPrintPanelActivity(...)`, call `SignOnActivity.startMyActivity(...)`. Whatever initialization you need to do in IPrintPanelActivity, do it in `onCreate()` and inspect the Intent extras. There's only so much I can help you without seeing more of your code and understanding where you are trying to start your IPrintPanelActivity. – Karakuri Apr 14 '14 at 01:19
  • Thank you @Karakuri. You are right, I would need to give you more information (see below my comment for @Raghunandan.) I shall try to explain more, but probably tomorrow because now I don't have the code with the modifications I made today at work. – Monica Apr 15 '14 at 04:42
1

You don't have constructors explicitly for Activties. You don't instantiate a Activity class. You only declare the Activity in manifest file.

Please read the answer by Raghav Sood

And i quote Raghav

By treating an Activity as a normal Java class, you end up with a null context. As most methods in an Activity are called on its Context, you will get a null pointer exception, which is why your app crashes.

Can i Create the object of a activity in other class?

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • Thank you for the answer. I read what Raghav Sood wrote, but this contradicts the answer by @Chase at http://stackoverflow.com/questions/8725530/android-activity-class-constructor-working – Monica Apr 13 '14 at 20:30
  • @Monica wait a minute there is no constructor in that link you posted and also he says intents are better way of doing this. Anyway its still a wrong approach – Raghunandan Apr 13 '14 at 20:42
  • Yeah, Raghunandan is correct. I didn't even notice that when I checked the link. The comments say it's a constructor, but it's not. I guess that's how it was "working". Nice catch. – Mike M. Apr 13 '14 at 22:46
  • @Monica i still don't understand why intents cannot be used. – Raghunandan Apr 14 '14 at 03:15
  • @Raghunandan I am not saying that intents cannot be used. My problem is that it is not yet clear how to get rid of the constructors and use intents instead. I tried to replace the constructors by usual methods but I get some error message about an Activity that cannot be started. It is also a problem the fact that the constructors are defined for classes which are not Activities. So I changed the base abstract class for all these non-activity classes to extend Activity. Still, I'm afraid you would need more information to be able to help me. – Monica Apr 15 '14 at 04:38
  • @Monica you need to get your basics right. Read the docs about activities and intents. Good luck – Raghunandan Apr 15 '14 at 04:43