Adding Gridview to fragment it's crashing
Asked Answered
P

4

6

I'm following a tutorial and trying to build in a grid view into my fragment and every time I launch the app it crashes. I opened up LogCat and it gives me nothing... Can someone help me find out what I can do to get this to display correctly and not crash the app? Thank you!!!

Below I've included my Main Activity, GridView Adapter and Fragment...

MainActivity

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;


   import android.view.ViewGroup;


public class MainActivity extends ActionBarActivity
        implements NavigationDrawerFragment.NavigationDrawerCallbacks {

    /**
     * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
     */
    private NavigationDrawerFragment mNavigationDrawerFragment;

    /**
     * Used to store the last screen title. For use in {@link #restoreActionBar()}.
     */
    private Char

Sequence mTitle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);

    // Set the first title
    mTitle = "Inventory";

    // Set up the drawer.
    mNavigationDrawerFragment.setUp(
            R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout));

    // Remove shadow under actionbar
    getSupportActionBar().setElevation(0);
}

@Override
public void onNavigationDrawerItemSelected(int position) {

    Fragment objFragment = null;

    switch (position) {
        case 0:
            objFragment = new Inventory_Fragment();
            mTitle = getString(R.string.title_section1);
            break;
        case 1:
            objFragment = new Orders_Fragment();
            mTitle = getString(R.string.title_section2);
            break;
        case 2:
            objFragment = new Cart_Fragment();
            mTitle = getString(R.string.title_section3);
            break;
        case 3:
            objFragment = new Settings_Fragment();
            mTitle = getString(R.string.title_section4);
            break;
    }


    // update the main content by replacing fragments
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.container, objFragment)
            .commit();
}

public void onSectionAttached(int number) {
    switch (number) {
        case 1:
            mTitle = getString(R.string.title_section1);
            break;
        case 2:
            mTitle = getString(R.string.title_section2);
            break;
        case 3:
            mTitle = getString(R.string.title_section3);
            break;
        case 4:
            mTitle = getString(R.string.title_section4);
            break;
    }
}

public void restoreActionBar() {
    ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setDisplayShowTitleEnabled(true);
    actionBar.setTitle(mTitle);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (!mNavigationDrawerFragment.isDrawerOpen()) {
        // Only show items in the action bar relevant to this screen
        // if the drawer is not showing. Otherwise, let the drawer
        // decide what to show in the action bar.
        getMenuInflater().inflate(R.menu.main, menu);
        restoreActionBar();
        return true;
    }
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.inventory_layout, container, false);
        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((MainActivity) activity).onSectionAttached(
                getArguments().getInt(ARG_SECTION_NUMBER));
    }
}

// MARK: - Helpers
public void setActionBarTitle(String title) {
    getSupportActionBar().setTitle(title);
}

}

GridViewAdapter

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

/**
 * Created by kenbarlow on 5/20/15.
 */
public class GridViewAdapter extends BaseAdapter {
    private Context context;

    public GridViewAdapter(Context context) {
        context = context;
    }

    private int[] icons = {
            // Temporary
        R.drawable.image1,
        R.drawable.image2,
        R.drawable.image3,
        R.drawable.image4,
        R.drawable.image5,
        R.drawable.image6,
        R.drawable.image7,
        R.drawable.image8,
        R.drawable.image9,
        R.drawable.image10,
        R.drawable.image11,
        R.drawable.image12,
        R.drawable.image13,
        R.drawable.image14,
        R.drawable.image15,
        R.drawable.image16,
        R.drawable.image17

};


@Override
public int getCount() {
    return icons.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 imageView;
    if (convertView == null) {
        imageView = new ImageView(context);
        imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(10, 10, 10, 10);
    } else {
        imageView = (ImageView) convertView;
    }
    imageView.setImageResource(icons[position]);
    return imageView;
}
}

Inventory_Fragment --- I Feel like the problem is in here but I'm not sure.

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

/**
 * Created by kenbarlow on 5/19/15.
 */
public class Inventory_Fragment extends Fragment {

    View rootView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.inventory_layout, container, false);

    GridView gridview = (GridView) getActivity().findViewById(R.id.gridview);
    gridview.setAdapter(new GridViewAdapter(this));


    return rootView;
}

}
Partake answered 20/5, 2015 at 18:36 Comment(3)
Could you step through the code and see where the code is crashing?Miss
If you place a breakpoint on super.onCreate() in MainActivity, does it reach to that code? I m concerned about your compiler settings. Have you run any other apps before this?Miss
return the position value @ getItem method instead of null.Hann
C
2

I guess your are trying to find a gridview in the activity's layout, which is wrong because the gridview is in the Fragment's layout. Please change in your fragment code:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.inventory_layout, container, false);

    GridView gridview = (GridView) rootView.findViewById(R.id.gridview);
    if(gridview != null){
        gridview.setAdapter(new GridViewAdapter(getActivity()));
    }

    return rootView;
}

If it's not the cause of the problem, please post a stacktrace when the crash happen (it should have already been done).

Coreligionist answered 2/6, 2015 at 15:51 Comment(0)
M
0

I have not looked at all the code yet. I reviewed GridViewAdapter and Inventory_Fragment.

Remove getItemId method in GridViewAdapter. You don't use it anyway. It is possible that the BaseAdapter is referencing it, and returning it null from that method may crash the adapter.

Code suggestion, return an item for this method:

@Override
public Object getItem(int position){
    return icons[position];
}
Miss answered 29/5, 2015 at 8:28 Comment(0)
P
0

LogCat and it gives me nothing...

if literally nothing, try to reset LogCat.

as i see in these lines

rootView = inflater.inflate(R.layout.inventory_layout, container, false);

GridView gridview = (GridView) getActivity().findViewById(R.id.gridview);

you are inflating and makeing rootView to find your inner layouts. so instead of getActivity() in second line use your rootView.

and remember to put your gridView layout inside inventory_layout

Pendergast answered 2/6, 2015 at 16:43 Comment(0)
S
0

I am not sure if it is the only issue or not, but this one looks a bit wrong. In your Inventory_Fragment class:

gridview.setAdapter(new GridViewAdapter(this));

You should change it to this:

gridview.setAdapter(new GridViewAdapter(getActivity()));

Also please upload your layouts and/or logcat report here. It is hard to go through the code like this.

Sev answered 2/6, 2015 at 17:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.