Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7406133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:30:35+00:00 2026-05-29T05:30:35+00:00

I know that I cannot pass parameters to the Activity constructor in android, but

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:30:36+00:00Added an answer on May 29, 2026 at 5:30 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that you cannot return anonymous types from methods but I am wondering
I know that you cannot simply set DocumentRoot in htaccess but it's all I
I know that I've seen this site before, but cannot remember it for the
Now while I know that you can not perform inheritance like you would in
I'm a complete Java noob. I know that Java treats all parameters as pass
I am storing objects in a buffer. Now I know that I cannot make
I know that the CGContext cannot call it to draw directly, and it needs
I know that a 32-bit OS cannot see more than 4 GB of RAM.
I know for a matter of fact that Type cannot be used when passing
I know it is a condition of hard links that they cannot span filesystems.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.