2

In my main activity if I apply this code I got error gridviewadapter() cannot be applied, it is problem in context this.

  GridViewAdapter adapter = new GridViewAdapter(this, R.layout.grid_item_layout, result);

if I apply it like this :

public MainActivity (Context context) {
        mContext = context;

    }
      GridViewAdapter adapter = new GridViewAdapter(mContext, R.layout.grid_item_layout, result);

I got this error in logcat

java.lang.InstantiationException: can't instantiate class com.justedhak.www.i.MainActivity; no empty constructor

I am trying to pass asynctask arraylist value to mainactivity .. how to fix that ?

BNK
  • 23,994
  • 8
  • 77
  • 87
Moudiz
  • 7,211
  • 22
  • 78
  • 156
  • I think you should not using constructor for `MainActivity`. You could use: `GridViewAdapter adapter = new GridViewAdapter(getApplicationContext(), R.layout.grid_item_layout, result);` – Neo Oct 17 '15 at 09:21
  • @MrNeo i tried your way and it worked you can post it as answer to accept it – Moudiz Oct 17 '15 at 09:30

5 Answers5

3

try

GridViewAdapter adapter = new GridViewAdapter(MainActivity.this, R.layout.grid_item_layout, result);
RDY
  • 613
  • 1
  • 9
  • 25
2

remove this constructor

public MainActivity (Context context) {
    mContext = context;
}

add this inside onCreate() of MainActivity

mContext = this;
Kishore Jethava
  • 6,666
  • 5
  • 35
  • 51
2

GridViewAdapter adapter = new GridViewAdapter(this, R.layout.grid_item_layout, Listitem);

2

If you want to pass mContext variable, initialize this variable like the following:

public class MainActivity extends AppCompatActivity {
     private final Context mContext = this;

     ... // other codes such as onCreate...
}

Hope this helps!

BNK
  • 23,994
  • 8
  • 77
  • 87
  • Hi man thanks for your answer, the above accepted answer did the work. Now i am working with styling the toolbar do you have answers for you about that? About toolbars? – Moudiz Oct 18 '15 at 06:12
  • I still didnt post the question, i still reading about it , once i ask the question ill tell you btw what time is at your country? – Moudiz Oct 18 '15 at 06:29
  • hi man do you have best result for this question ? http://stackoverflow.com/questions/33197213/styling-bottom-action-icon-postion-and-background-color – Moudiz Oct 18 '15 at 18:01
1

Should not using constructor for MainActivity class. Kindly using:

GridViewAdapter adapter = new GridViewAdapter(getApplicationContext(), R.layout.grid_item_layout, result);

instead of:

GridViewAdapter adapter = new GridViewAdapter(this, R.layout.grid_item_layout, result);
Neo
  • 1,469
  • 3
  • 23
  • 40