Adding a gridview to fragment its failure

I follow the tutorial and try to build a grid in my fragment, and every time I run the application, it crashes. I opened LogCat and it does not give anything ... Can someone help me find out what I can do to display it correctly and not collapse the application? Thanks!

Below I have 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 the problem is 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; } } 
+7
android-activity android-gridview android-fragments
source share
4 answers

I think you are trying to find the gridview in the action layout, which is not true because the gridview is in the fragment layout. Change the 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 this is not the cause of the problem, send a stop loss when the failure occurs (this should already have been done).

+2
source share

I haven't looked at all the code yet. I reviewed the GridViewAdapter and Inventory_Fragment.

Remove the getItemId method in the GridViewAdapter . You do not use it anyway. It is possible that the BaseAdapter referencing it, and returning it null from this method may cause the adapter to fail.

Suggesting code, return an element for this method:

 @Override public Object getItem(int position){ return icons[position]; } 
0
source share

LogCat, and it gives me nothing ...

if literally nothing, try 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 inflate and force rootView to find its internal layouts. so instead of getActivity() in the second line use your rootView.

and don't forget to place your gridView layout inside inventory_layout

0
source share

I'm not sure if this is the only problem 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 upload your layouts and / or logcat report here. It is hard to go through such a code.

0
source share

All Articles