hasan hasan - 3 years ago 72
Android Question

grid view Attempt to invoke virtual method.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup)' on a null object reference

i have List View and i'm trying to go to Grid View OnItemCLiclick listener
i have an exception on the adapter put i can't solve it.

the list view is a fragment and it's work very well!

the Exception is :

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup)' on a null object reference
at com.rt_no.myprogect.ui.adapter.CareerImageAdapter.getView(CareerImageAdapter.java:52)

at the line :

convertView= layoutInflater.inflate(R.layout.career_grid_view, null);


on the gridview adapter.

this is the List view :

public class CitiesFragment extends Fragment {
private static final String TAG ="CitiesFragment";

private ListView cityList;
private ArrayList<City> cities;
private CitiesAdapter citiesAdapter;
private CitiesFragmentListener listener;

private Context context;

private GridView gridView;
@Override
public void onAttach(Context context) {
super.onAttach(context);
Log.d(TAG, "onAttach_CitiesFragment");
listener = (CitiesFragmentListener) context;
}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(TAG, "onCreateView_CitiesFragment");
View view = inflater.inflate(R.layout.cities_list_view_fragment, null);
return view;
}


private ArrayList<City> initCities(){.....}

private void initGridView(Fragment fragment){
Log.d(TAG, "CitiesFragment_initGridView");
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.container,fragment);
fragmentTransaction.addToBackStack("fragment");
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.commit();
}


@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.d(TAG, "onViewCreated_CitiesFragment");
cities = initCities();
cityList = (ListView) view.findViewById(R.id.cityList);
citiesAdapter = new CitiesAdapter(getActivity(), cities);
cityList.setAdapter(citiesAdapter);
cityList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d(TAG, "onItemClick_CitiesFragment position : " + cities.get(position).getCityName());
GridViewFragment fragment = new GridViewFragment();
initGridView(fragment);


}
});

}


the Grid View:

public class GridViewFragment extends Fragment {
private static final String TAG = "GridViewFragment";

private CitiesFragmentListener listener;
private GridView gridView;
private CareerImageAdapter careerImageAdapter;
private int position;



@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
Log.d(TAG, "onCreateView_GridViewFragment");
View view = inflater.inflate(R.layout.career_grid_view, null);
gridView = (GridView) view.findViewById(R.id.careerGridView);

return view;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.d(TAG, "onViewCreated_GridViewFragment");
gridView = (GridView) view.findViewById(R.id.careerGridView);
gridView.setAdapter(new CareerImageAdapter(getActivity()));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d(TAG, "onItemClick_GridViewFragment position : " + position);

// go to workers_list_view_fragment layout

}
});

}


the Grid View Adapter:

public class CareerImageAdapter extends BaseAdapter {
private static final String TAG = "CareerImageAdapter";

private Context mContext;
private LayoutInflater layoutInflater;



public CareerImageAdapter (Context c){
mContext = c;
}


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

@Override
public CareerImageAdapter getItem(int position) {

return null ;
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView ;

if (convertView == null){
convertView= layoutInflater.inflate(R.layout.career_grid_view, null);
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(180, 180));
imageView.setScaleType(ImageView.ScaleType.FIT_END);
imageView.setPadding(8, 8, 8, 8);
}else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(ImageIdes[position]);
return convertView;
}

private Integer[] ImageIdes =
{
R.drawable.afar,R.drawable.asbaka,R.drawable.bnya,
R.drawable.cnstractors,R.drawable.doors,R.drawable.etom,
R.drawable.floring,R.drawable.foxy,R.drawable.garnolet,
R.drawable.gaz,R.drawable.geves,R.drawable.ginoon,
R.drawable.halbashateven,R.drawable.hashmal,R.drawable.instltsya,
R.drawable.kvesatshatyah,R.drawable.mazgan,R.drawable.meshlowah,
R.drawable.pargolot,R.drawable.pegom,R.drawable.pinoyafpa,
R.drawable.robah,R.drawable.tyah,
};


}

Answer Source

layoutInflater is never initialized. Add this in the CareerImageAdapter constructor:

layoutInflater = LayoutInflater.from(c);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download