hichris123
hichris123

Reputation: 10233

Cannot instantiate class in Android App

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.

Upvotes: 1

Views: 871

Answers (3)

baifuyou
baifuyou

Reputation: 69

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

public CustomObject(){
}

Upvotes: 0

Nikola Despotoski
Nikola Despotoski

Reputation: 50578

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.

Upvotes: 0

Simon Forsberg
Simon Forsberg

Reputation: 13351

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.

Upvotes: 1

Related Questions