0

How the code Intent intent=new Intent(context,Activity.class) which is in the superclass can be reused by its subclasses given that the subclasses have different context and different activities to start after on click listener is called. Is it possible?

This is the superclass:

public class CommonPost extends AppCompatActivity {

    public void on_create(final Context context, final Class aclass) {
        post.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonPost commonPost = new CommonPost();
                MyTask task = commonPost.new MyTask(context, aclass);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
                    task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                else
                    task.execute();
            }
        });
    }


class MyTask extends AsyncTask<Void,Void,Void> {
          Context context;
          Class aclass;

        public MyTask(Context context,Class aclass){
             this.context=context;
             this.aclass=aclass;
        }

        @Override
        protected void onPreExecute() {
            // do something
        }

        @Override
        protected Void doInBackground(Void... voids) {
            // do something
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            Intent intent = new Intent(context, aclass);
            startActivity(intent);
        }
}

One of the subclass:

public class PlacementPost extends CommonPost {
            Context context=PlacementPost.this;
            Class aclass=Placements.class;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                on_create(context,aclass);
            }
        }

I am getting the following error when I try the above code:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
       at android.app.Activity.startActivityForResult(Activity.java:4266)
       at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)



at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
   at android.app.Activity.startActivityForResult(Activity.java:4224)
   at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
   at android.app.Activity.startActivity(Activity.java:4548)
   at android.app.Activity.startActivity(Activity.java:4516)
   at studentapp.notefi.CommonPost$PlaceTask.onPostExecute(CommonPost.java:240)
   at studentapp.notefi.CommonPost$PlaceTask.onPostExecute(CommonPost.java:177)
   at android.os.AsyncTask.finish(AsyncTask.java:660)
   at android.os.AsyncTask.-wrap1(AsyncTask.java)
   at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:677)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:154)
   at android.app.ActivityThread.main(ActivityThread.java:6077)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

I am not sure where I am wrong or what I am missing. I just tried out whatever logically I felt correct. Please do correct me out where I am wrong!

digital_pro
  • 183
  • 2
  • 13

2 Answers2

1

for the starters you should never initialize your Activity using new, it has it's own life cycle and context should be of the class where you are actually starting intent, change you code to

on_create

 public void on_create(final Context context, final Class aclass) {
            post.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    MyTask task = new MyTask(stor_root, mProgress, editTextplace, post, ninfo, imageUri,
                            mstorage, mDatabase, context, aclass);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
                        task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                    else
                        task.execute();
                }
            });
        }

MyTask - Stop passing context from child

class MyTask extends AsyncTask<Void,Void,Void> {
        Class aclass;

        public MyTask(Class aclass){
             this.aclass=aclass;
        }

        @Override
        protected void onPreExecute() {
            // do something
        }

        @Override
        protected Void doInBackground(Void... voids) {
            // do something
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            Intent intent = new Intent(CommonPost.this, aclass);
            startActivity(intent);
        }
}

SubClass

public class PlacementPost extends CommonPost {
            Context context=PlacementPost.this;
            Class aclass=Placements.class;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                on_create(aclass);
            }
        }
Rajan Kali
  • 12,627
  • 3
  • 25
  • 37
  • 1
    Because with base class `context` you can access child class `context` too, so you no need to send it to parent explicitly – Rajan Kali Dec 08 '17 at 10:14
0

You need to make the variable a class scope not an inner class scope.

This is an inner class scope variables:

public class CommonPost extends AppCompatActivity {

  ...
  class MyTask extends AsyncTask<Void,Void,Void> {
     Context context;
     Class aclass;

     ...

  }
}

You can't access the Context context and Class aclass; from the child class.

You need to make it a class scope:

public class CommonPost extends AppCompatActivity {

  // set to protected to only allow child class access.
  protected Context context;
  protected Class aclass;

  ...
  class MyTask extends AsyncTask<Void,Void,Void> {

  }
}

Then, in your child class, change the variables to:

public class PlacementPost extends CommonPost {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // set the value to the base class.
    context = PlacementPost.this;
    aclass = Placements.class;

    on_create(context,aclass);
  }
}
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96