Im currently trying to Make a GridView with ABS(ActionBarSherlock) within a FRAGMENT. But Im having some issue, getting NPE(NullPointerException). Here is my Fragment Code:
public class Fragment2 extends SherlockFragment {
GridView gridView;
ImageAdapter imgAdapter;
private Context context;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment2, container, false);
gridView = (GridView) rootView.findViewById(R.id.gridview);
context = getSherlockActivity();
gridView.setAdapter(new ImageAdapter(context));
return rootView;
}
And my ImageAdapterClass:
public class ImageAdapter extends BaseAdapter {
// references to our images
private Integer[] mThumbIds = {
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7
};
private static Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
}
And Finally my LogCat:
10-25 01:06:32.239: E/AndroidRuntime(12344): at com.AlexPrograms.NavDrawerSherlock.Fragment2.onCreateView(Fragment2.java:24)
10-25 01:06:32.239: E/AndroidRuntime(12344): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
10-25 01:06:32.239: E/AndroidRuntime(12344): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
10-25 01:06:32.239: E/AndroidRuntime(12344): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
10-25 01:06:32.239: E/AndroidRuntime(12344): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
10-25 01:06:32.239: E/AndroidRuntime(12344): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
10-25 01:06:32.239: E/AndroidRuntime(12344): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
10-25 01:06:32.239: E/AndroidRuntime(12344): at android.os.Handler.handleCallback(Handler.java:587)
10-25 01:06:32.239: E/AndroidRuntime(12344): at android.os.Handler.dispatchMessage(Handler.java:92)
10-25 01:06:32.239: E/AndroidRuntime(12344): at android.os.Looper.loop(Looper.java:130)
10-25 01:06:32.239: E/AndroidRuntime(12344): at android.app.ActivityThread.main(ActivityThread.java:3687)
10-25 01:06:32.239: E/AndroidRuntime(12344): at java.lang.reflect.Method.invokeNative(Native Method)
10-25 01:06:32.239: E/AndroidRuntime(12344): at java.lang.reflect.Method.invoke(Method.java:507)
10-25 01:06:32.239: E/AndroidRuntime(12344): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
10-25 01:06:32.239: E/AndroidRuntime(12344): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
10-25 01:06:32.239: E/AndroidRuntime(12344): at dalvik.system.NativeStart.main(Native Method)
I wish my best if u can find what's wrong. Thanks in Advance
I Just get over ABS and when to ActionBar Compat with de Support Lib.
Here my fragmentClass.
public class FragmentCategories extends Fragment {
private GridView vGrid;
private Context vContenx;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View primalView = inflater.inflate(R.layout.fragment_categories_grid, container, false);
vContenx = getActivity();
vGrid = (GridView) primalView.findViewById(R.id.gridview);
vGrid.setAdapter(new GridAdapter(vContenx));
return primalView;
}
And the getView of my Adaptor for the GridView
@Override
public View getView(int pos, View gridView, ViewGroup fatherGroup) {
catNames = mContext.getResources().getStringArray(R.array.value_Cats);
ImageView imageView;
TextView textView;
if (gridView == null) {
gridView = mlayoutPrinter.inflate(R.layout.grid_items, null);
imageView = (ImageView) gridView.findViewById(R.id.img);
imageView.getLayoutParams().height = 120;
textView = (TextView) gridView.findViewById(R.id.TextItem);
textView.setText(catNames[pos]);
gridView.setTag(imageView);
textView.setTag(textView);
} else {
imageView = (ImageView)gridView.getTag();
}
imageView.setImageResource(mThumbIds[pos]);
return gridView;
}
And the XML for the fragment which contains a GridView
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/FrameLayout1"
android:layout_width="match_parent"
android:background="@color/BandHookGrey"
android:layout_height="match_parent"
tools:context=".FragmentCategories" >
<GridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="2"
android:padding="5dp"
android:horizontalSpacing="7dp"
android:verticalSpacing="7dp"
android:background="@color/BandHookGrey"
android:stretchMode="columnWidth"
android:gravity="center"/>
And by Last the XML of the Layout that is inflated to populate the Grid.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/GridItemImage"
android:layout_width="wrap_content"
android:orientation="vertical"
android:background="#FFF"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/img"
android:layout_height="120dp"
android:layout_margin="5dp"
android:layout_width="match_parent"
android:contentDescription="@string/content"/>
<TextView
android:id="@+id/TextItem"
android:layout_width="match_parent"
android:gravity="center"
android:background="@android:color/transparent"
android:layout_height="wrap_content"
android:textColor="@color/gridText"
android:textSize="16sp"
android:paddingBottom="2dp"
android:singleLine="true"/>