Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I cannot pass parameters to Android Activity Constructor

I know that I cannot pass parameters to the Activity constructor in android, but I would like to understand why.

What I tried to do is:

CalorieSelectorActivity csa = new CalorieSelectorActivity(userName);
                Intent i = new Intent(thisContext, csa.getClass());
                startActivity(i);

I have defined the class as follows:

public class CalorieSelectorActivity extends Activity {

public CalorieSelectorActivity(String name) {
    super();

}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.calorieselector);

}
}

And it works perfectly when I remove the parameters from the constructor. I finally did it using intent.putExtra, but am very curious to know why it cannot be done by passing parameters to the constructor?

LogCat:

02-04 06:46:52.257: W/dalvikvm(800): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
02-04 06:46:52.277: E/AndroidRuntime(800): FATAL EXCEPTION: main
02-04 06:46:52.277: E/AndroidRuntime(800): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.manarbushnaq.calorietracker/com.manarbushnaq.calorietracker.CalorieSelectorActivity}: java.lang.InstantiationException: com.manarbushnaq.calorietracker.CalorieSelectorActivity
02-04 06:46:52.277: E/AndroidRuntime(800):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
02-04 06:46:52.277: E/AndroidRuntime(800):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-04 06:46:52.277: E/AndroidRuntime(800):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-04 06:46:52.277: E/AndroidRuntime(800):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-04 06:46:52.277: E/AndroidRuntime(800):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-04 06:46:52.277: E/AndroidRuntime(800):  at android.os.Looper.loop(Looper.java:123)
02-04 06:46:52.277: E/AndroidRuntime(800):  at android.app.ActivityThread.main(ActivityThread.java:4627)
02-04 06:46:52.277: E/AndroidRuntime(800):  at java.lang.reflect.Method.invokeNative(Native Method)
02-04 06:46:52.277: E/AndroidRuntime(800):  at java.lang.reflect.Method.invoke(Method.java:521)
02-04 06:46:52.277: E/AndroidRuntime(800):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-04 06:46:52.277: E/AndroidRuntime(800):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-04 06:46:52.277: E/AndroidRuntime(800):  at dalvik.system.NativeStart.main(Native Method)
02-04 06:46:52.277: E/AndroidRuntime(800): Caused by: java.lang.InstantiationException: com.manarbushnaq.calorietracker.CalorieSelectorActivity
02-04 06:46:52.277: E/AndroidRuntime(800):  at java.lang.Class.newInstanceImpl(Native Method)
02-04 06:46:52.277: E/AndroidRuntime(800):  at java.lang.Class.newInstance(Class.java:1429)
02-04 06:46:52.277: E/AndroidRuntime(800):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
02-04 06:46:52.277: E/AndroidRuntime(800):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
02-04 06:46:52.277: E/AndroidRuntime(800):  ... 11 more
like image 990
Manar Bushnaq Avatar asked Feb 04 '12 02:02

Manar Bushnaq


People also ask

How do you pass parameters to a constructor?

You can only define the coursebookname variable one time (which is when you specify the type). Remove the String designation from before the variable name when you pass it to the Person constructor and it should work fine. Person p1 = new Person(cousebookname); Spelling aside.

Can constructor pass parameters?

You can use any data type for a parameter of a method or a constructor. This includes primitive data types, such as doubles, floats, and integers, as you saw in the computePayment method, and reference data types, such as objects and arrays.

What are the two parameters that an intent constructor takes when calling another activity?

The Intent constructor takes two parameters, a Context and a Class . The Context parameter is used first because the Activity class is a subclass of Context . The Class parameter of the app component, to which the system delivers the Intent, is, in this case, the activity to start.

Which constructor contains object as parameter?

Copy constructors A Copy constructor has one formal parameter that is the type of the class (the parameter may be a reference to an object). It is used to create a copy of an existing object of the same class.


2 Answers

Refer to your code:

CalorieSelectorActivity csa = new CalorieSelectorActivity(userName);

Intent i = new Intent(thisContext, csa.getClass());

startActivity(i);

Even if you create an object of your activity, what you are "passing" in the Intent object is not the activity object but just the class of your activity. In startActivity() the Android framework will try to instantiate an object of your activity. And it calls the default constructor (without parameters) when it does that. It fails when your class does not have a constructor without parameters.

Of course, you have found the correct solution, pass the parameters as part of Intent object.

like image 115
Sameer Avatar answered Oct 17 '22 07:10

Sameer


I'm surprised that nobody answered the actual question asked! So to answer this Why I cannot pass parameters to Android Activity Constructor: because Android calls the constructor of the Activity and the only constructor Android calls is the default constructor which of course you can override but "It doesn't matter whether you add it or not, and there is no context in which it can be employed in a useful way.".

So as there is no way you can pass your parameters to default constructor, you cannot employ it to work for you.

like image 26
Marian Paździoch Avatar answered Oct 17 '22 07:10

Marian Paździoch