I have searched a lot this question and nothing work for me. i am trying to set forecast data into recyclerview and want set that data in a list via adapter here is a piece of code for my adapter...
public Adapter(Context context, List<ForecastCondition> data) {
this.context = context;
inflater = LayoutInflater.from(context);
this.data = data;
}
i have set my adapter in a fragment and in asynctask method of postExecute to set the list in adapter i am using this code...
mAdapter = new Adapter(getContext(), datalist);
mRVFishPrice.setAdapter(mAdapter);
final LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRVFishPrice.setLayoutManager(layoutManager);
my logcat is
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.om.weahterapp, PID: 19218 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
at android.view.LayoutInflater.from(LayoutInflater.java:219)
at data.Adapter.<init>(Adapter.java:38)
at Fragments.ForecastFragment$ForecastTask.onPostExecute(ForecastFragment.java:153)
at Fragments.ForecastFragment$ForecastTask.onPostExecute(ForecastFragment.java:101)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5253)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
EDIT
I have tried getActivity() and as well as FragmentActivity() but it doesn't work for me.
FRAGMENT
public class ForecastFragment extends Fragment {
private RecyclerView mRVFishPrice;
private Adapter mAdapter;
Context mContext;
List<ForecastCondition> datalist = new ArrayList<>();
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
View view;
private OnFragmentInteractionListener mListener;
public ForecastFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment ForecastFragment.
*/
// TODO: Rename and change types and number of parameters
public static ForecastFragment newInstance(String param1, String param2) {
ForecastFragment fragment = new ForecastFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
mContext = getActivity();
CityPreference cityPreference = new CityPreference(getActivity());
renderForecastData(cityPreference.getCity());
}
public void renderForecastData(String city) {
ForecastTask forecastTask = new ForecastTask();
forecastTask.execute(new String[]{city});
}
private class ForecastTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
try {
String data = ((new ForecastHttpClient()).getForecast(params[0]));
Log.v("doInBackground:" ,data);
return data;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String data) {
super.onPostExecute(data);
try {
Log.v("postExecute:",data);
JSONObject jObj = new JSONObject(data);
JSONArray jArr = jObj.getJSONArray("list");
for (int i = 0; i < jArr.length(); i++) {
JSONObject jDayForecast = jArr.getJSONObject(i);
ForecastCondition forecastCondition = new ForecastCondition();
forecastCondition.lastupdateForecast = jDayForecast.getLong("dt");
JSONObject tempObj = jDayForecast.getJSONObject("temp");
forecastCondition.dayTempForecast = (float) tempObj.getDouble("day");
forecastCondition.minTempForecast = (float) tempObj.getDouble("min");
forecastCondition.maxTempForecast = (float) tempObj.getDouble("max");
forecastCondition.nightTempForecast = (float) tempObj.getDouble("night");
forecastCondition.eveTempForecast = (float) tempObj.getDouble("eve");
forecastCondition.morningTempForecast = (float) tempObj.getDouble("morn");
forecastCondition.pressureForecast = (float) jDayForecast.getDouble("pressure");
forecastCondition.humidityForecast = (float) jDayForecast.getDouble("humidity");
JSONArray weatherArray = jDayForecast.getJSONArray("weather");
JSONObject jsonWeather = weatherArray.getJSONObject(0);
forecastCondition.weatherIdForecast = jsonWeather.getInt("id");
forecastCondition.conditionForecast = jsonWeather.getString("main");
forecastCondition.descriptionForecast = jsonWeather.getString("description");
forecastCondition.iconForecast = jsonWeather.getString("icon");
datalist.add(forecastCondition);
}
mAdapter = new Adapter(mContext, datalist);
mRVFishPrice.setAdapter(mAdapter);
final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRVFishPrice.setLayoutManager(layoutManager);
// mRVFishPrice.setLayoutManager(new LinearLayoutManager(getContext()));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_forecast, container, false);
mRVFishPrice = (RecyclerView) view.findViewById(R.id.fishPriceList);
// Inflate the layout for this fragment
return view;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
My app is crash whenever i call this line from MainActivity
new ForecastFragment().renderForecastData(newCity);