I have an Activity Class Anmeldung, a normal java class thread_haupt.class and a Activity funktionen_haupt.class.
on Start of Anmeldung.class starts a function(funktionstarter) of threads_haupt.class This calls a function from functionen_haupt.class via invoke.
The idea is to have a function that can start other functions also from other classes the problem is java.lang.InstantiationException: can't instantiate class … no empty constructor in my thread_haupt.class when I try to newInstance() (see below in thread_haupt.class) java.lang.InstantiationException: can't instantiate class … no empty constructor
Anmeldung.Class
public class anmeldung extends Activity implements OnClickListener{
@Override
protected void onStart() { // start ist dann aufgerufen wenn alles gebaut ist
// TODO Auto-generated method stub
meine_funktionen.buttons_vorbereiten(this);
/**
* hier wirde zuerst der spinner befüllt
*/
thread_haupt meinThread = new thread_haupt();
//HOMEPAGE,naj.nuz.wz.wa.dc.kommunikation.allgemein kommunikation,naj.nuz.wz.wa.dc.drinkcoffee.helfer.allgemein helfer, Activity meineAct};
meinThread.meine_parameter= new Object[] {HOMEPAGE,kommunikation,helfer,meineAct};
meinThread.meine_funktion="staedte_abfragen";
meinThread.mein_context=this.getApplicationContext();
meinThread.meine_activity=this;
meinThread.thread_starten("staedte_abfragen"); // HERE !!!
threads.class
public class thread_haupt {
public Object[] meine_parameter;
public String meine_funktion;
public Object mein_context;
public Activity meine_activity;
public allgemein mein_helfer = new allgemein();
public funktionen_haupt meine_funktionen = new funktionen_haupt((Context) mein_context, meine_activity);
/**
* hier sollen die threads gestartet werden
* @param threadName
*/
public void thread_starten(String threadName){
switch (threadName) {
case "staedte_abfragen":
//Thread thread = new Thread(new Runnable(){
//hier hab ich das gefunden http://stackoverflow.com/questions/5161951/android-only-the-original-thread-that-created-a-view-hierarchy-can-touch-its-vi
//public void run() {
//meine_funktionen.staedte_abfragen(HOMEPAGE,kommunikation,helfer, meineAct);
try {
meine_funktionen.funktionstarter("naj.nuz.wz.wa.dc.drinkcoffee.funktionen_haupt", meine_funktion, meine_parameter, mein_context);
// hier aufgehört dies startet nicht
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//}
//});
//thread.start();
break;
default:
break;
}
}
}
funktionen_haupt.class
public class funktionen_haupt extends Activity {
// CONTEXT empfangen von den die die klasse aufrufen
Context mContext;
Activity mActivity;
allgemein kommunikation = new allgemein();
naj.nuz.wz.wa.dc.drinkcoffee.helfer.allgemein helfer = new naj.nuz.wz.wa.dc.drinkcoffee.helfer.allgemein();
public funktionen_haupt(Context mContext,Activity mActivity){
this.mContext = mContext;
this.mActivity=mActivity;
}
public void funktionstarter(String package_name,String funktion_name,Object[] arguments, Object meincontext) throws NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, java.lang.InstantiationException {
//no paramater
Method[] methoden;
Class cls = null;
Object obj = null;
try {
cls = Class.forName(package_name);
obj = cls.newInstance(); // !!! HERE IS MY PROBLEM !!!!
//Constructor<funktionen> obj = cls.getConstructor(cls);
//funktionen dieklasse = obj.newInstance(null);
//Object obj = FactoryRegistry.getFactory(cls).newInstance();
methoden = cls.getDeclaredMethods();
for (int i =0;i<=methoden.length-1;i++){
if (methoden[i].getName().equals(funktion_name)){
Method method2 =cls.getDeclaredMethod(funktion_name, methoden[i].getParameterTypes());
if (meincontext==null){
method2.invoke(obj, arguments);
}
else{
method2.invoke(meincontext, arguments);
}
break;
}
}
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
methoden = cls.getDeclaredMethods();
for (int i =0;i<=methoden.length-1;i++){
if (methoden[i].getName().equals(funktion_name)){
Method method2 =cls.getDeclaredMethod(funktion_name, methoden[i].getParameterTypes());
method2.invoke(this, arguments);
break;
}
}
}
}
I get an exeption by
cls = Class.forName(package_name);
obj = cls.newInstance(); // java.lang.InstantiationException: can't instantiate class … no empty constructor
and I don´t know why.
Can somone tell me whats wrong, why it´s not working and what I doing wrong ? Thanks in advance.
Because you declared a constructor with different parameters, the default (empty) constructor is gone. The default constructor is only available when you have not defined another constructor. You can create an empty constructor like this:
public funktionen_haupt(){
}
public funktionen_haupt(Context mContext,Activity mActivity){
this.mContext = mContext;
this.mActivity=mActivity;
}
If you want to create the object with the parameters then you should not use the newInstance() method but just call the constuctor.