朋友们,我正在尝试将数据从活动传递到选项卡片段但是它给了我空指针异常我使用json将数据存储到getter和setter方法并尝试将该列表传递给frgament方法< / p>
Display.class
public static void toast(Context context, String display) { Toast.makeText(context, display, Toast.LENGTH_LONG).show(); }
MyError
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
这是我的主要活动
public class HotelDetails extends AppCompatActivity {
@BindView(R.id.hoteldetailedtoolbar)
Toolbar hoteldetailedtoolbar;
@BindView(R.id.hotelamentiestab)
TabLayout hotelamentiestab;
@BindView(R.id.hotelpager)
ViewPager hotelpager;
ArrayList<Hotel_image> hotelimg = new ArrayList<Hotel_image>();
@BindView(R.id.hotelname)
TextView hotelname;
@BindView(R.id.hotelprice)
TextView hotelprice;
@BindView(R.id.roomtype)
TextView roomtype;
@BindView(R.id.whatsmakes)
TextView whatsmakes;
@BindView(R.id.needtoknow)
TextView needtoknow;
HotelImages hoteladapter;
@BindView(R.id.indicator)
InkPageIndicator indicator;
@BindView(R.id.bannerpager)
ViewPager bannerpager;
@BindView(R.id.roomtypes)
Spinner roomtypes;
List<Hotel_TabBeans> listhotelbeans=new ArrayList<Hotel_TabBeans>();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hotel_detail);
ButterKnife.bind(this);
setSupportActionBar(hoteldetailedtoolbar);
assert getSupportActionBar() != null;
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
hoteldetailedmethod();
setupviewpager(hotelpager);
hotelpager.setOffscreenPageLimit(1);
hotelamentiestab.setupWithViewPager(hotelpager);
hoteladapter = new HotelImages(getApplicationContext(), hotelimg);
bannerpager.setAdapter(hoteladapter);
ArrayList<String> roomlist = new ArrayList<>();
roomlist.add("Rooms Types");
roomlist.add("Standard");
roomlist.add("Deluxe");
roomlist.add("Luxury");
ArrayAdapter<String> roomadapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.spinner_detailed, R.id.spinnertext, roomlist);
roomadapter.setDropDownViewResource(R.layout.spinner_dropdown);
roomtypes.setAdapter(roomadapter);
}
@OnClick(R.id.spinnerbutton)
public void spinnerbuttonclick() {
roomtypes.performClick();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.hoteldetailed_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.call:
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:0123456789"));
startActivity(intent);
return true;
case R.id.share:
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "CYStay");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Here live url will come");
startActivity(Intent.createChooser(shareIntent,
"Share The Post"));
return true;
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void setupviewpager(ViewPager pager) {
TabsFragments tabsFragments = new TabsFragments(getSupportFragmentManager());
tabsFragments.addfragment(new Hotel_tab(), "Hotel");
tabsFragments.addfragment(new Hotel_tab(), "Room");
pager.setAdapter(tabsFragments);
}
private void hoteldetailedmethod() {
Display.showLoadingDialog(HotelDetails.this, "Loading...");
final String hoteldetaildurl = Jsonurl.url + "hotel_details.php?rid=" + getIntent().getStringExtra("Roomid");
Display.log(hoteldetaildurl);
JsonObjectRequest hoteldetailedreq = new JsonObjectRequest(Request.Method.GET, hoteldetaildurl, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray hotelimagesarray = response.getJSONArray("hotel_images");
for (int i = 0; i < hotelimagesarray.length(); i++) {
JSONObject hotelimagearrayobj = hotelimagesarray.getJSONObject(i);
Hotel_image hotelimage = new Hotel_image();
hotelimage.setHotelimages(hotelimagearrayobj.getString("image"));
hotelimg.add(hotelimage);
}
int number = bannerpager.getChildCount();
if (number != -1) {
indicator.setViewPager(bannerpager);
}
JSONObject hoteldtails = response.getJSONObject("hotel_details");
getSupportActionBar().setTitle(hoteldtails.getString("hotel_name"));
hotelname.setText(hoteldtails.getString("hotel_name"));
hotelprice.setText(hoteldtails.getString("room_price"));
roomtype.setText(hoteldtails.getString("room_name"));
whatsmakes.setText(hoteldtails.getString("what_makes_special"));
needtoknow.setText(hoteldtails.getString("need_to_know"));
JSONArray ammarray=hoteldtails.getJSONArray("amenities");
for (int i = 0; i <ammarray.length() ; i++) {
JSONObject arrayobj=ammarray.getJSONObject(i);
Hotel_TabBeans tabeans=new Hotel_TabBeans();
tabeans.setAmenitimage(arrayobj.getString("img"));
tabeans.setAmenitiname(arrayobj.getString("name"));
listhotelbeans.add(tabeans);
Hotel_tab hotelTab=new Hotel_tab();
hotelTab.hoteltablist(listhotelbeans);
}
// hoteldatas.hoteldatamethod(listhotelbeans);
// Bundle listbundle=new Bundle();
// listbundle.putStringArrayList("hotelist",listhotelbeans);
// listbundle.putSerializable("hoye", (Serializable) listhotelbeans);
// Hotel_tab hoteltab=new Hotel_tab();
// hoteltab.setArguments(listbundle);
} catch (JSONException e) {
e.printStackTrace();
}
hoteladapter.notifyDataSetChanged();
Display.hideLoadingDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Display.log(error.toString());
Display.hideLoadingDialog();
}
});
hoteldetailedreq.setRetryPolicy(new DefaultRetryPolicy(500000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
AppController.getInstance().addToRequestQueue(hoteldetailedreq);
}
}
这是我的片段活动:
public class Hotel_tab extends Fragment {
public Hotel_tab() {
}
@BindView(R.id.hotel_tab_list)
RecyclerView hotel_tab_list;
Hotel_Tab_Layout hotel_tab_layout;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.hotel_tab,container,false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ButterKnife.bind(this,view);
LinearLayoutManager hotelmanager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
hotel_tab_list.setLayoutManager(hotelmanager);
}
public void hoteltablist(List<Hotel_TabBeans> msg){
Display.toast(getActivity(),msg);//here is the null pointer exeception i am getting
}
}
答案 0 :(得分:0)
问题很明显:
您希望获得Context
而不将Fragment
附加到活动。
在解析来自服务器的成功响应时,您的代码如下:
Hotel_tab hotelTab=new Hotel_tab();
hotelTab.hoteltablist(listhotelbeans);
这意味着,您正在创建新的Fragment
并在未附加到其上的片段上调用方法getActivity()
。
你有3个选择:
Activity
内展示,而不是在Fragment
内展示。Framgent
内执行此操作,请先将其附加到FragmentManager
Context
作为Fragment
函数hoteltablist(...)
参数之一传递。