-2

When I try to call a method from my adapter class, I'm getting an error System services not available to Activities before onCreate();

MainTask.Class

public class MainTask extends AppCompatActivity  {
    public static final String mypreference = "mypref";
    public static String Name = "nameKey";
    SharedPreferences taskcount, currenttime;
    public static int completask;
    long shared_time;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_task);
             Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
       setupViewPager(viewPager);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        tabLayout.setupWithViewPager(viewPager);//setting tab over viewpager

        taskcount = getSharedPreferences(mypreference,
                MainActivity.MODE_PRIVATE);
        completask = taskcount.getInt(Name, 0);

    }
public void propertask(){
    final NiftyDialogBuilder dialog1 = NiftyDialogBuilder.getInstance(MainTask.this);
    Effectstype effect;
    effect = Effectstype.SlideBottom;
    dialog1.setCancelable(false);
    dialog1.isCancelableOnTouchOutside(false)
            .withTitle(null)
            .withMessage(null)
            .withEffect(effect)
            .setCustomView(R.layout.proper_task, MainTask.this)
            .show();


    Button rate = (Button) dialog1.findViewById(R.id.rate_button);


    rate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            dialog1.dismiss();

        }
    });
    dialog1.show();
}

GridListAdapter.Java

 if(taskvalue==0)
     {
   MainTask mainTask=new MainTask();
   mainTask.propertask();

  }

Please help me, how I can access propertask method from my Adapter class. Thanks in advance.

EJK
  • 12,332
  • 3
  • 38
  • 55

3 Answers3

1

Never create an instance of an activity yourself (MainTask mainTask=new MainTask() in your code). Activities are created by the framework, in response to a startActivity() call.

Since GridListAdapter seems to be used in some class other than MainTask, perhaps propertask() should be a method on GridListAdapter or on the activity or fragment that is displaying the GridListAdapter.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

You can move the propertask method to in GridListAdapter. Than you need to MainTask Activity's context for this method.

Mete
  • 2,805
  • 1
  • 28
  • 38
0

You do not have to create an object for an Activity. Actually what CommonsWare said you shouldn't do it.

You can call that function like this

if(taskvalue==0)
 {
  // assuming you have passed context to your adapter. If not, then do it.
  ((MainTask)context).propertask();

 }

EDIT: As you said in your comments "you do not have context so you couldn't made Dialog from Adapter". But now since you have passed it, you can do it now from your Adapter and remove this unnecessary casting-to-activity function call.

nitinkumarp
  • 2,120
  • 1
  • 21
  • 30