我有一个recyclerview,我想在clicklerview上单击项目时打开一个新片段。当我在onCreateView
方法上访问它时,getActivity()返回null,即使我从onItemClick
方法访问它以便移动到另一个片段时它返回一个空指针异常。
public class DailyMenuFrag extends Fragment implements
DailyDataAdapter.onItemClickListener {
private List<DailyData> daily_data_list;
public List<DailyData> popular_data_list;
DailyDataAdapter adapter = null;
Activity activity;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
daily_data_list = new ArrayList<>();
popular_data_list = new ArrayList<>();
load_data();
View view = inflater.inflate(R.layout.fragment_daily_menu, container, false);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
adapter = new DailyDataAdapter(getActivity(), daily_data_list);
if(getActivity() == null){
Log.i("info","null");
}
else Log.i("info","notnull");
adapter.setOnItemClickListner(new DailyMenuFrag());
recyclerView.setAdapter(adapter);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(),2);
recyclerView.setLayoutManager(gridLayoutManager);
return view;
}
public void load_data() {
task.execute("http://yemekapp.kuarkdijital.com.tr/home.php");
}
AsyncTask<String, Void, String> task = new AsyncTask<String, Void, String>() {
@Override
protected String doInBackground(String... params) {
URL url;
HttpURLConnection URLConnection = null;
String current = "";
try {
url = new URL(params[0]);
URLConnection = (HttpURLConnection) url.openConnection();
URLConnection.connect();
InputStream inputStream = URLConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(inputStream);
int data = reader.read();
while (data != -1) {
current += (char) data;
data = reader.read();
}
JSONObject dailyObject = null;
JSONObject popularObject = null;
JSONObject jsonObject = new JSONObject(current);
JSONObject banner = jsonObject.getJSONObject("banner");
String daily = jsonObject.getString("daily");
String popular = jsonObject.getString("popular");
JSONArray dailyArray = new JSONArray(daily);
JSONArray popularArray = new JSONArray(popular);
for (int i = 0; i < dailyArray.length(); i++) {
dailyObject = dailyArray.getJSONObject(i);
popularObject = popularArray.getJSONObject(i);
DailyData DailyData = new DailyData(dailyObject.getInt("id"), dailyObject.getString("Servings"), dailyObject.getString("Title"), dailyObject.getString("CookTime"), dailyObject.getString("Image"));
DailyData PopularData = new DailyData(popularObject.getInt("id"), popularObject.getString("Servings"), popularObject.getString("Title"), popularObject.getString("CookTime"), popularObject.getString("Image"));
daily_data_list.add(DailyData);
popular_data_list.add(PopularData);
}
}
catch (JSONException e) {
e.printStackTrace();
}
catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return current;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
adapter.notifyDataSetChanged();
}
};
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.activity = activity;
}
@Override
public void onItemClick(int position) {
Log.i("info","clicked");
if(getActivity() == null){
Log.i("info","null");//why it is null here
}
else Log.i("info","notnull");
DetailViewPager detailViewPager = new DetailViewPager();
// FragmentManager manager = getActivity().getSupportFragmentManager();
// FragmentTransaction transaction = manager.beginTransaction();
// transaction.replace(R.id.fragment_container,detailViewPager);
// transaction.commit();
}
}
}
记录错误
02-22 08:49:26.186 20849-20849/com.nejat.yemektarifiproject E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.nejat.yemektarifiproject, PID: 20849
java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.app.FragmentManager android.support.v4.app.FragmentActivity.getSupportFragmentManager()' on a null object reference
at com.nejat.yemektarifiproject.DailyMenuFrag$override.onItemClick(DailyMenuFrag.java:151)
at com.nejat.yemektarifiproject.DailyMenuFrag$override.access$dispatch(DailyMenuFrag.java)
at com.nejat.yemektarifiproject.DailyMenuFrag.onItemClick(DailyMenuFrag.java:0)
at com.nejat.yemektarifiproject.DailyDataAdapter$ViewHolder$1.onClick(DailyDataAdapter.java:79)
at android.view.View.performClick(View.java:5637)
答案 0 :(得分:1)
adapter.setOnItemClickListner(new DailyMenuFrag());
new DailyMenuFrag()
这里是一个新片段,它没有附加到任何活动,因此getActivity()
返回null。
看起来应该使用
adapter.setOnItemClickListner(this);
而是将当前DailyMenuFrag
实例用作项目点击侦听器。
答案 1 :(得分:0)
似乎来自loadData()的onPostExecute()(AsyncTask)在初始化之前通知适配器。
load_data();
View view = inflater.inflate(R.layout.fragment_daily_menu, container, false);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
adapter = new DailyDataAdapter(getActivity(), daily_data_list);