Fragment showing up blank

时间:2019-05-31 11:51:11

标签: android android-fragments

I am creating an app that has markers on a map representing bus stops. When the user clicks on the bus stop, a new fragment is shown with few details regarding buses at that stop.

It was working until I made some changes, a couple of minutes before. And now when you click on the bus stop, it just takes me to a blank fragment (not show my XML file layout anymore).

I tried to undo my new changes and compare it to what I had, when it was working fine but still no luck. I also tried cleaning and rebuilding the build but still works nothing. I'd appreciate any help I can get on this!

public class RealtimeFragment extends Fragment {
     private int stopId;
     //int stopId= 1354;
     private RecyclerView recyclerView;
     private RecyclerView.Adapter adapter;
     private ArrayList<Bus> results = new ArrayList<>();
     private SwipeRefreshLayout swipeRefreshLayout;
     private String endpoint;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.fragment_realtime, container, false);
        recyclerView = view.findViewById(R.id.bus_realtime_view);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);

        System.out.println("made it to view");

        endpoint = getRealtimeInfo(stopId);
        getData();

        swipeRefreshLayout = view.findViewById(R.id.card_container_layout);
        swipeRefreshLayout.setOnRefreshListener(this::getData);
        return view;
    }

    public void setStopId(int id) {

        this.stopId = id;
    }

    public static String getRealtimeInfo(int stopId) {

        return "api link";
    }

    public void getData(){
        RestClient.get(endpoint, new BaseJsonHttpResponseHandler<JSONArray>(){
            @Override
            public void onSuccess(int statusCode, Header[] headers, String rawJsonResponse, JSONArray response){
                results.clear();
                for(int i=0; i<response.length(); i++) {
                    try {
                        JSONObject o = response.getJSONObject(i);
                        Bus bus = new Bus(o);
                        results.add(bus);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                adapter = new BusAdapter(getActivity(), results);
                recyclerView.setAdapter(adapter);
                swipeRefreshLayout.setRefreshing(false);

            }
            @Override
            public void onFailure (int statusCode, Header[] headers, Throwable throwable, String rawJsonData, JSONArray errorResponse){
                swipeRefreshLayout.setRefreshing(false);
            }

            @Override
            protected JSONArray parseResponse(String rawJsonData, boolean isFailure) throws Throwable{
                return new JSONObject(rawJsonData).getJSONArray("results");
            }

        });
    }
}

Adapter Class:



    public class BusAdapter extends RecyclerView.Adapter<BusAdapter.ViewHolder>{
        private List<Bus> dataset;
        private Context context;
        private static OnItemClickListener onItemClickListener;

        public BusAdapter(Context context) {
            this.context=context;
        }
        public BusAdapter() {
        }
        public void setOnItemClickListener(BusAdapter.OnItemClickListener onItemClickListener) {
        }

        public interface OnItemClickListener{
            void onItemClick(View view, int pos);
        }

        public static class ViewHolder extends RecyclerView.ViewHolder {
            TextView timeTextView, destinationTextView, routeTextView, minsTextView;

            ViewHolder(View v) {
                super(v);
                this.timeTextView = v.findViewById(R.id.due);
                this.destinationTextView = v.findViewById(R.id.terminus);
                this.routeTextView = v.findViewById(R.id.route);
                this.minsTextView = v.findViewById(R.id.due_mins_text);
                v.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //String test = routeTextView.getText().toString();
                        Intent intent = new Intent(v.getContext(),BusTrackerActivity.class);
                        v.getContext().startActivity(intent);
                    }
                });
            }
        }

        public BusAdapter(Context context, ArrayList<Bus> dataset){
            this.context = context;
            this.dataset = dataset;
        }

        @Override
        public BusAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
            View view = LayoutInflater.from(context).inflate(R.layout.bus_card, parent, false);
            return new BusAdapter.ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(BusAdapter.ViewHolder holder, int position){
            Bus bus = dataset.get(position);
            holder.timeTextView.setText(String.valueOf(bus.getMinsUntilBus()));
            holder.destinationTextView.setText(String.valueOf(bus.getDestination()));
            holder.routeTextView.setText(String.valueOf(bus.getRoute()));
            holder.minsTextView.setText(bus.getMinsUntilBus()==1? "min" : "mins");
        }

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

XML for fragment

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools">

    <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_container_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="stuff here">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/bus_realtime_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>


Card View Holder XML


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="96dp"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="2dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="16dp"
        android:elevation="6dp">


        <LinearLayout
            android:id="@+id/search_term_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="8dp">

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.5">

                <TextView
                    android:id="@+id/due"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:text="Time"
                    android:textSize="24sp" />

                <TextView
                    android:id="@+id/due_mins_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentEnd="true"
                    android:text="mins" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1">

                <TextView
                    android:id="@+id/terminus"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:text="Terminus" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.5">

                <TextView
                    android:id="@+id/route"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:text="Route"
                    android:textSize="18sp" />
            </RelativeLayout>
        </LinearLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>

No error messages, just no UI! It even prints the S.O.P in the terminal that I code in the onCreate method.

1 个答案:

答案 0 :(得分:0)

偶然(而且非常愚蠢)删除了我将上一个片段中的stopId传递到我的API密钥中的位置。