1

I'm developing an Android app. In my app, I lanuch an Intent to another class. As soon as this happens, the app crashes. It says that it can't instantiate my class. My code is below:

public class CustomObject extends Activity{
ArrayList<String> Alerts;
ArrayList<String> Names;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
Intent intent = getIntent();
Names = intent.getExtras().getStringArrayList("Names");
Alerts = intent.getExtras().getStringArrayList("Alerts");
}


public CustomObject(ArrayList<String> prop1, ArrayList<String> prop2) {
    this.Names = prop1;
    this.Alerts = prop2;
}

public ArrayList<String> getProp1() {
    return Names;
}

public ArrayList<String> getProp2() {
   return Alerts;
}
}

Here's my LogCat:

10-24 19:56:25.234: E/AndroidRuntime(12982): FATAL EXCEPTION: main
10-24 19:56:25.234: E/AndroidRuntime(12982): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.dev.chicagotraintracker/com.dev.chicagotraintracker.CustomObject}: java.lang.InstantiationException: can't instantiate class com.dev.chicagotraintracker.CustomObject; no empty constructor
10-24 19:56:25.234: E/AndroidRuntime(12982):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
10-24 19:56:25.234: E/AndroidRuntime(12982):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-24 19:56:25.234: E/AndroidRuntime(12982):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-24 19:56:25.234: E/AndroidRuntime(12982):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-24 19:56:25.234: E/AndroidRuntime(12982):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-24 19:56:25.234: E/AndroidRuntime(12982):    at android.os.Looper.loop(Looper.java:137)
10-24 19:56:25.234: E/AndroidRuntime(12982):    at android.app.ActivityThread.main(ActivityThread.java:5103)
10-24 19:56:25.234: E/AndroidRuntime(12982):    at java.lang.reflect.Method.invokeNative(Native Method)
10-24 19:56:25.234: E/AndroidRuntime(12982):    at java.lang.reflect.Method.invoke(Method.java:525)
10-24 19:56:25.234: E/AndroidRuntime(12982):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-24 19:56:25.234: E/AndroidRuntime(12982):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-24 19:56:25.234: E/AndroidRuntime(12982):    at dalvik.system.NativeStart.main(Native Method)
10-24 19:56:25.234: E/AndroidRuntime(12982): Caused by: java.lang.InstantiationException: can't instantiate class com.dev.chicagotraintracker.CustomObject; no empty constructor
10-24 19:56:25.234: E/AndroidRuntime(12982):    at java.lang.Class.newInstanceImpl(Native Method)
10-24 19:56:25.234: E/AndroidRuntime(12982):    at java.lang.Class.newInstance(Class.java:1130)
10-24 19:56:25.234: E/AndroidRuntime(12982):    at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
10-24 19:56:25.234: E/AndroidRuntime(12982):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
10-24 19:56:25.234: E/AndroidRuntime(12982):    ... 11 more

I can't find the problem in my code. Some other questions have said something about a no-argument constructor, but how do I implement that? Is that even my problem? Thank you for your help.

hichris123
  • 10,145
  • 15
  • 56
  • 70
  • 2
    Not having a `no-argument constructor` is EXACTLY your problem. Did you read your logcat, or just C&P here for us to read for you? `Caused by: java.lang.InstantiationException: can't instantiate class com.dev.chicagotraintracker.CustomObject; no empty constructor` – nhgrif Oct 25 '13 at 00:03
  • I saw what it said, I just don't understand what a no-argument constructor is and what the exact problem with my code is. – hichris123 Oct 25 '13 at 00:06

3 Answers3

1

Activities and Fragments may not have constructors with parameters (a.k.a. arguments), like this one:

public CustomObject(ArrayList<String> prop1, ArrayList<String> prop2) {
    this.Names = prop1;
    this.Alerts = prop2;
}

Only a constructor with no parameters is allowed public CustomObject() { (you don't need to specify a constructor at all)

You will have to pass that info in the Intent using a String array. See putExtra and getStringArrayExtra

To create this intent from another Activity, you can use

Intent intent = new Intent(this, CustomObject.class);
intent.putExtra("prop1", arrayList.toArray(new String[arrayList.size()]));
intent.putExtra("prop2", arrayList2.toArray(new String[arrayList2.size()]));
startActivity(intent);

And then in the onCreate method of your Activity, you can retrieve the values using:

Intent intent = getIntent();
List<String> list1 = new ArrayList<String>(Arrays.asList(intent.getStringArrayExtra("prop1")));
List<String> list2 = new ArrayList<String>(Arrays.asList(intent.getStringArrayExtra("prop2")));

Because it's not possible to send a List over an intent that easily, here it is turned to a String array and then turned back to a List in the onCreate method.

Simon Forsberg
  • 13,086
  • 10
  • 64
  • 108
  • How would I create that intent? What would the class be? – hichris123 Oct 25 '13 at 00:04
  • Is there any way to pass that ArrayList to another class without an Intent? I don't want it to ever display the class I have above, I just want to pass the data to that class from the UI class, then pass it to another, then pass it back to the UI class. – hichris123 Oct 25 '13 at 19:44
  • @hichris123 It is possible to "pass" (or rather store) any object through the `Application` object, [read here](http://stackoverflow.com/questions/4572338/extending-application-to-share-variables-globally) for more info about that – Simon Forsberg Oct 25 '13 at 23:27
0

Class that inherit Activity class or Fragment MUST have empty constructor. Do some reading on this.

In your current situation you might try to workaround it by keeping this argumented constructor and call super(), note: this is empty super class constructor call.

I don't like this approach. Simon's solution by sending through intent is right way to do this.

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
0

Activity needs a non-argument constructor.Now,you added a constructor with two arguments.So,you also should add a non-constructor:

public CustomObject(){
}
baifuyou
  • 69
  • 1
  • 5