Call a method of an inner class from outside

2

I have a class B and in this class I need to call a method, getSomething(type, option) , which is defined in a class A that is a class that extends AsyncTask and that is within class C.

My class C is defined as follows:

public class C extends BaseActivity{
(...)
 public class A extends AsyncTask<String, Void, String> {
 (...)
  public String doInBackground(String... params) {
  (...)
  }
  public String getSomething(String type, String option){
  (...)
  }
  protected void onPreExecute() {
  (...)
  }
  protected void onPostExecute(String result){
  (...)
  }
(...)
}

What I'm trying to do is within class B, inside a method put this call to the getSomething () method:

String sentence = new C().new A().getSomething(type,option);

I've already tried it:

new C().new A().execute();

I can not call the method because it gives me an exception

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference

whenever the call to the getSomething (.) method is executed.

Can anyone help, how can I call the procedure from another class?

    
asked by anonymous 27.07.2016 / 16:20

3 answers

0

To instantiate an inner class outside the class where it was declared, you must first create an instance of the outer class and use the objectoExterno.new ClasseInterna() syntax to create the inner object.

ClasseExterna objectoExterno = new ClasseExterna();
ClasseExterna.ClasseInterna objectoInterno = objectoExterno.new ClasseInterna();

In your case, since the C class is an Activity , this can not be done because an Activity should not be instantiated through new operator.

The solution is to transform the inner class into a normal class and instantiate it as usual in each of the classes.

    
27.07.2016 / 20:50
0

To use Async to always try to use an ex execute:

C setence = new C(this); setence.execute(type,option);

because in this way the object is null since the method was not called.

    
27.07.2016 / 16:50
0

In class 'A', create instance:

public static A a;

Now in your 'B' class, call:

A.a.getSomething();

I did not test if it works, but it should work!

    
27.07.2016 / 21:33