Reputation: 429
Let's say I have an Android AsyncTask called UpdateInterfaceStatusesTask:
private class UpdateInterfaceStatusesTask extends AsyncTask<Object, Object, Object> {
public UpdateInterfaceStatusesTask() {
;
}
protected Object doInBackground(Object... params) {
View header = ((NavigationView) findViewById(R.id.nav_view)).getHeaderView(0);
((TextView) header.findViewById(R.id.nav_head_status)).setText(ServerThread.getInstance().isInterrupted() ? "Offline" : "Online");
return null;
}
@Override protected void onPostExecute(Object param) {
;
}
}
And I want to programatically create a new instance of it:
public void createNewInstance(Class<? extends AsyncTask<?, ?, ?>> clazz) {
try {
clazz.newInstance().execute();
} catch (Exception x) {
x.printStackTrace();
}
}
I'm getting the following error:
W/System.err: java.lang.InstantiationException: java.lang.Class<nl.hypothermic.offthegrid.ChatActivity$UpdateInterfaceStatusesTask> has no zero argument constructor
W/System.err: at java.lang.Class.newInstance(Native Method)
W/System.err: at nl.hypothermic.offthegrid.tasks.TaskScheduler$1.run(TaskScheduler.java:27)
W/System.err: at android.os.Handler.handleCallback(Handler.java:790)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err: at android.os.Looper.loop(Looper.java:164)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6494)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Anyone have the same problem?
Upvotes: 1
Views: 58
Reputation: 830
I guess your private class is actually a nested class and you need to make it static in order to use the 0 arguments constructor. Otherwise, there is always at least one argument (always the 1st), which is an instance of the containing class.
Upvotes: 5