Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 应用程序在尝试使用对tablayout中片段中的recyclerView进行改造来加载Json时崩溃_Android_Android Fragments_Android Recyclerview_Retrofit2_Android Tablayout - Fatal编程技术网

Android 应用程序在尝试使用对tablayout中片段中的recyclerView进行改造来加载Json时崩溃

Android 应用程序在尝试使用对tablayout中片段中的recyclerView进行改造来加载Json时崩溃,android,android-fragments,android-recyclerview,retrofit2,android-tablayout,Android,Android Fragments,Android Recyclerview,Retrofit2,Android Tablayout,因此,更具体地说,我有一个包含tablayout的活动,每个选项卡基本上都应该加载一个包含回收器视图的独立片段,我希望使用改型从api检索的数据根据用户操作加载到回收器视图中 这就是活动: public class RestaurantLogoView extends AppCompatActivity implements LiteFragment.OnFragmentInteractionListener, RegularFragment.OnFragmentInteractionListe

因此,更具体地说,我有一个包含tablayout的活动,每个选项卡基本上都应该加载一个包含回收器视图的独立片段,我希望使用改型从api检索的数据根据用户操作加载到回收器视图中
这就是活动:

public class RestaurantLogoView extends AppCompatActivity implements LiteFragment.OnFragmentInteractionListener, RegularFragment.OnFragmentInteractionListener, View.OnClickListener {
    private static final String TAG = "RestaurantLogoView";
        public static ProgressDialog progressDialog;
    //TODO
    public static View.OnClickListener myOnClickListener;

    private String chosenArea;
    TextView locationTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        ViewPager mViewPager = (ViewPager) findViewById(R.id.container);
        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);
        SectionsPagerAdapter pagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(),getApplicationContext());
        //remove this if you still didnt get data in the fragment
        mViewPager.setAdapter(pagerAdapter);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(view -> Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                .setAction("Action", null).show());

        locationTv = (TextView) findViewById(R.id.location_tv);
        chosenArea = (String) getIntent().getSerializableExtra("Object");
        locationTv.setText(chosenArea);
        locationTv.setOnClickListener(this);
    }

    @Override
    public void onFragmentInteraction(Uri uri) {
        //TODO i added this lately not sure what its supposed to do but consider removing it
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */

//
//
//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.menu_restaurant_main_view, menu);
//        return true;
//    }
    @Override
    public void onClick(View v) {
        if (v == locationTv) {
            Intent o = new Intent(getApplicationContext(), AreaList.class);
            startActivity(o);
            finish();
        } else {
            Toast.makeText(getApplicationContext(), "What do you want to eat today?", Toast.LENGTH_LONG).show();
        }
    }

    private class SectionsPagerAdapter extends FragmentStatePagerAdapter {

        String tabTitles[] = {getString(R.string.regular), getString(R.string.lite)};
        Context context;


        SectionsPagerAdapter(FragmentManager fm, Context context) {
            super(fm);
            this.context = context;
        }


        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    new RegularFragment();
                    return RegularFragment.newInstance(TAG, position);
                case 1:
                    new LiteFragment();
                    return LiteFragment.newInstance(TAG, position);

                default:
                    return null;
            }
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            // Generate title based on item position
            return tabTitles[position];
        }

    }

}
`
这是第一个片段,应该显示在我的第一个选项卡中

public class RegularFragment extends Fragment {

    OnFragmentInteractionListener mListener;

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    public static final String REGULAR_PARAM1 = "lite";
    View RootView;
    List<RestaurantModel> regularRestaurants;
    private RecyclerView recyclerView;
    RecyclerViewAdapter adapter;
    private Call<RestaurantElKbeer> callbackCall;
    private String TAG = "RegularFragment";
    //new
    public static final String ARG_PAGE = "ARG_PAGE";

    private int mPage;

    public RegularFragment() {
    }

    // TODO: Rename and change types and number of parameters
    public static Fragment newInstance(String param, int position) {

        //TODO it might be important i didnt know why i created it
        Fragment fragment = new RegularFragment();
        Bundle args = new Bundle();
        args.putInt(param, position);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        regularRestaurants = requestRegularList();
        mPage = getArguments().getInt(ARG_PAGE);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        //new
        RootView = inflater.inflate(R.layout.fragment_regular, container, false);
        recyclerView = (RecyclerView) RootView.findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        initializeAdapter();
        LinearLayoutManager llm = new LinearLayoutManager(this.getContext());
        recyclerView.setLayoutManager(llm);
        return RootView;
    }

    private void initializeAdapter() {
        adapter = new RecyclerViewAdapter(getActivity(), regularRestaurants);
        recyclerView.setAdapter(adapter);
    }

    // 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 onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }


    private List<RestaurantModel> requestRegularList() {
        List<RestaurantModel> myRestaurantsList = new ArrayList<>();
        RestAdapter restAdapter = new RestAdapter();
        API api = restAdapter.getApi();
        // URL: https://betaweb.jeebley.com/services_new/services.php?action=search&langId=1&countryId=21&cuisineType=1&cuisineId=all&areaId=1&delvType=1

        Call<RestaurantElKbeer> call = api.getRestaurants("search", "1", "21", "all", "1", "1");
        call.enqueue(new Callback<RestaurantElKbeer>() {
            @Override
            public void onResponse(@NonNull Call<RestaurantElKbeer> call, @NonNull Response<RestaurantElKbeer> response) {
                if (response.isSuccessful()) {
                    ArrayList<String> names = new ArrayList<String>();
                    RestaurantElKbeer restaurantResponse = response.body();
                   List<RestaurantModel> list = restaurantResponse.getRestaurantModel();
                if(list != null && !list.isEmpty()){
                    for (int i = 0; i < list.size(); i++) {
                        String name;
                        name = list.get(i).getRName();
                        names.add(name);
                        Log.i(TAG, "onResponse: " + names.get(i));
                    }

                }
                else
                    {
                        Log.i(TAG, "onResponse(regular): RestaurantModelList is null");
                    }
                    RecyclerViewAdapter adapter = new RecyclerViewAdapter(getContext(), list);
                    recyclerView.setAdapter(adapter);
                    recyclerView.setOnClickListener(v -> {
                        Intent i = new Intent(getContext(), ChooseYourLanguage.class);
                        i.putExtra("Object", getId());
                        startActivityForResult(i, 0);
                    });
                }
            }

            @Override
            public void onFailure(@NonNull Call<RestaurantElKbeer> call, Throwable t) {
                Log.i(TAG, "onFailure: failed RegularFragmentCall");
            }
        });
        return myRestaurantsList;
    }


    interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }

    /**
     * Initialize the contents of the Fragment host's standard options menu.  You
     * should place your menu items in to <var>menu</var>.  For this method
     * to be called, you must have first called {@link #setHasOptionsMenu}.  See
     * {@link RestaurantLogoView#onCreateOptionsMenu(Menu) Activity.onCreateOptionsMenu}
     * for more information.
     *
     * @param menu     The options menu in which you place your items.
     * @param inflater
     * @see #setHasOptionsMenu
     * @see #onPrepareOptionsMenu
     * @see #onOptionsItemSelected
     */
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public void setHasOptionsMenu(boolean hasMenu) {
        super.setHasOptionsMenu(hasMenu);
    }
}
公共类RegularFragment扩展了片段{
OnFragmentInteractionListener;
//TODO:重命名参数参数,选择匹配的名称
//片段初始化参数,例如ARG_ITEM_NUMBER
公共静态最终字符串正则_PARAM1=“lite”;
视图根视图;
列出正规餐厅;
私人回收站;
循环水适配器;
私人电话回拨;
私有字符串TAG=“RegularFragment”;
//新的
公共静态最终字符串ARG\u PAGE=“ARG\u PAGE”;
私人公寓;
公共规则片段(){
}
//TODO:重命名和更改参数的类型和数量
公共静态片段newInstance(字符串参数,int位置){
//TODO这可能很重要我不知道为什么要创建它
Fragment Fragment=新的RegularFragment();
Bundle args=新Bundle();
参数putInt(参数、位置);
fragment.setArguments(args);
返回片段;
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
regularRestaurants=requestRegularList();
mPage=getArguments().getInt(ARG_页);
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//新的
RootView=充气机。充气(R.layout.fragment\u常规,容器,假);
recyclerView=(recyclerView)RootView.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
初始化为apter();
LinearLayoutManager llm=新的LinearLayoutManager(this.getContext());
recyclerView.setLayoutManager(llm);
返回RootView;
}
private void initializedapter(){
适配器=新的RecycleServiceAdapter(getActivity(),regularRestaurants);
recyclerView.setAdapter(适配器);
}
//TODO:重命名方法、更新参数并将方法挂接到UI事件中
public void onButtonPressed(Uri){
if(mListener!=null){
onFragmentInteraction(uri);
}
}
@凌驾
已创建视图上的公共void(视图,@Nullable Bundle savedInstanceState){
super.onViewCreated(视图,savedInstanceState);
}
私有列表请求RegularList(){
List myRestaurantsList=new ArrayList();
RestAdapter RestAdapter=新RestAdapter();
API=restAdapter.getApi();
//网址:https://betaweb.jeebley.com/services_new/services.php?action=search&langId=1&countryId=21&cuisineType=1&cuisineId=all&areaId=1&delvType=1
Call Call=api.getRestaurants(“搜索”、“1”、“21”、“全部”、“1”、“1”);
call.enqueue(新回调(){
@凌驾
public void onResponse(@NonNull调用,@NonNull响应){
if(response.issusccessful()){
ArrayList name=新的ArrayList();
RestaurantElKbeer restaurantResponse=response.body();
List List=restaurantResponse.getRestaurantModel();
if(list!=null&&!list.isEmpty()){
对于(int i=0;i{
Intent i=新的Intent(getContext(),选择yourlanguage.class);
i、 putExtra(“Object”,getId());
startActivityForResult(i,0);
});
}
}
@凌驾
public void onFailure(@NonNull Call,Throwable t){
Log.i(标记“onFailure:failed RegularFragmentCall”);
}
});
返回myRestaurantsList;
}
FragmentInteractionListener接口{
//TODO:更新参数类型和名称
void onFragmentInteraction(Uri);
}
/**
*初始化片段主机的标准选项菜单的内容
*应将菜单项放在“到”菜单中。对于此方法
*要调用,必须先调用{@link#sethaspoptionsMenu}。请参阅
*{@link RestaurantLogoView#onCreateOptions菜单(菜单)活动。onCreateOptions菜单}
*了解更多信息。
*
*@param menu用于放置项目的选项菜单。
*@param充气机
*@请参阅#设置选项菜单
*@see#onprepareOptions菜单
*@see#onOptionsItemSelected
*/
@凌驾
创建选项菜单(菜单菜单,菜单充气机){
super.onCreateOptions菜单(菜单,充气机);
}
@凌驾
public void SetHasOptions菜单(布尔hasMenu){
super.setHasOptions菜单(hasMenu);
}
}
这是我的再生水

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
    private List<RestaurantModel> items;
    private Context ctx;

    public RecyclerViewAdapter(Context context, List<RestaurantModel> items) {
        this.ctx = context;
        this.items=items;
    }


    private OnItemClickListener mOnItemClickListener;
    private SharedPref sharedPref;

    public interface OnItemClickListener {
        void onItemClick(View view, RestaurantModel obj, int position);
    }

    public void setOnItemClickListener(final OnItemClickListener mItemClickListener) {
        this.mOnItemClickListener = mItemClickListener;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        CardView cv;
        TextView title;
        TextView description;
        ImageView imageView;

        ViewHolder(View itemView) {
            super(itemView);
            cv = (CardView) itemView.findViewById(R.id.restaurant_card_view);
            title = (TextView) itemView.findViewById(R.id.restaurantName_section_label);
            description = (TextView) itemView.findViewById(R.id.section_label2);
            imageView = (ImageView) itemView.findViewById(R.id.restaurantImageView);
        }
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.restaurant_card_layout, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position, List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);
        RestaurantModel p = (RestaurantModel) payloads.get(position);
        holder.title.setText(p.getRName());
        if (!p.getDelvType().equals("1")) {
            holder.description.setText(p.getJDeliveryTime() + " " + p.getRMinOrderAmt() + " " + p.getJDeliveryCharge());
        } else {
            holder.description.setText(p.getRDeliveryTime() + " " + p.getRMinOrderAmt() + " " + p.getRDeliveryCharge());
            holder.imageView.setImageResource(R.mipmap.ic_launcher);
        }
       holder.cv.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Log.i("RecyclerViewAdapter", "onClick: replace with intent to send Restaurant data");
           }
       });
    }

    @Override
    public void onBindViewHolder(ViewHolder holder,int position) {

        final RestaurantModel p = items.get(position);
        holder.title.setText(p.getRName());
        if (!p.getDelvType().equals("1")) {
            holder.description.setText(p.getJDeliveryTime() + " " + p.getRMinOrderAmt() + " " + p.getJDeliveryCharge());
        } else {
            holder.description.setText(p.getRDeliveryTime() + " " + p.getRMinOrderAmt() + " " + p.getRDeliveryCharge());

        }
        holder.cv.setOnClickListener(view -> {
                mOnItemClickListener.onItemClick(view, p, position);

        });
    }


    @Override
    public int getItemCount() {
        return items.size();
    }

    public interface OnLoadMoreListener {
        void onLoadMore(int current_page);
    }
}
公共类RecycleServiceAdapter扩展了RecyclerView.Adapter{
私人清单项目;
私有上下文ctx;
公共RecycleServiceAdapter(上下文、列表项){
this.ctx=co
List<RestaurantModel> list = restaurantResponse.getRestaurantModel();
for (int i = 0; i < list.size(); i++) {
    String name;
    name = list.get(i).getRName();
    names.add(name);
    Log.i(TAG, "onResponse: " + names.get(i));
}
...
List<RestaurantModel> list = restaurantResponse.getRestaurantModel();
if(list != null && !list.isEmpty()){
    for (int i = 0; i < list.size(); i++) {
        String name;
        name = list.get(i).getRName();
        names.add(name);
        Log.i(TAG, "onResponse: " + names.get(i));
    }
}
...
@Override
public int getItemCount() {
    return items!=null ? items.size() : 0;
}
List<RestaurantModel> list = restaurantResponse.getRestaurantModel();
if(list == null){
    enter code here
}