0

I am pasting all three java classes which I created to make an imagegrid in a fragment in AppCompatActivity, but I am getting the shown error that I have pasted below.

my main activity:

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.main_fragment, new MainActivityFragment())
                .commit();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_settings) {
        return true;
    }

    if (id == R.id.action_sort) {
        showPop();
        return true;
    }

    return super.onOptionsItemSelected(item);
}

public void showPop()
{
    View menuItemView = findViewById(R.id.action_sort);
    PopupMenu popup = new PopupMenu(MainActivity.this, menuItemView);
    MenuInflater inflate = popup.getMenuInflater();
    inflate.inflate(R.menu.sort_popup, popup.getMenu());
    popup.show();
}


}

fragment with gridview:

public class MainActivityFragment extends Fragment
{
    public MainActivityFragment(){
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_main, null);
    GridView gv1=(GridView)root.findViewById(R.id.grid);
    gv1.setAdapter(new ImageAdapter(getActivity()));
    return root;
}

}

imageadapter class:

public class ImageAdapter extends BaseAdapter
{
private Context ctx;

public ImageAdapter(Context c)
{
    ctx=c;
}

@Override
public int getCount()
{
    return pics.length;
}

@Override
public Object getItem(int position)
{
    return null;
}

@Override
public long getItemId(int position)
{
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    ImageView iv;
    if (convertView == null)
    {
        iv = new ImageView(ctx);
        iv.setLayoutParams(new GridView.LayoutParams(350,350));
        iv.setScaleType(ImageView.ScaleType.FIT_XY);
        iv.setPadding(4, 4, 4, 4);
    }
    else
    {
        iv = (ImageView)convertView;
    }
    iv.setImageResource(pics[position]);
    return iv;

}

private Integer[] pics={
        R.drawable.a,R.drawable.b,
        R.drawable.c,R.drawable.d,
        R.drawable.e,R.drawable.f,
        R.drawable.g,R.drawable.h
};

}

I am getting this error log :

08-20 21:50:15.218 16512-16512/com.example.android.mymovieapp     E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.example.android.mymovieapp, PID: 16512
                                                                            java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.mymovieapp/com.example.android.mymovieapp.MainActivity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                at android.os.Looper.loop(Looper.java:148)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                             Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
                                                                                at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:198)
                                                                                at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:130)
                                                                                at com.example.android.mymovieapp.MainActivity.onCreate(MainActivity.java:19)
                                                                                at android.app.Activity.performCreate(Activity.java:6237)
                                                                                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                at android.os.Looper.loop(Looper.java:148) 
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                at java.lang.reflect.Method.invoke(Native Method) 
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • `Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.` should be talkative enough. – Phantômaxx Aug 20 '16 at 17:27
  • tried doing dat, nt working – noopur tanwar Aug 20 '16 at 18:15
  • try this link http://stackoverflow.com/questions/26515058/this-activity-already-has-an-action-bar-supplied-by-the-window-decor – Shubham Aug 20 '16 at 18:42
  • Make sure Theme.AppCompat.NoActionBar is extended in your styles – VVB Aug 20 '16 at 18:52
  • solved that with extending Theme.AppCompat.NoActionBar,thenks – noopur tanwar Aug 24 '16 at 17:25

1 Answers1

0

It is exception error. Try to Surrounds your code try/catch,

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.mymovieapp/com.example.android.mymovieapp.MainActivity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.

Kalu Singh Rao
  • 1,671
  • 1
  • 16
  • 21
Steven_art
  • 153
  • 10