0

I have wrote this app for searching a ride: input the source name and display the ride in a list view that start place is same as the user source name. It works fine on Emulator but it stop working after I have pick the source name when I try to run the app on a real device.

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private static final String ITEM_TITLE = "Item title";
    private static final String ITEM_SOURCE = "Source";
    private static final String ITEM_DESTINATION = "Destination";
    static int no = 0;
    static int y = 0;
    PlaceAutocompleteFragment sourceAutocompleteFragment;
    PlaceAutocompleteFragment destinationAutocompleteFragment;
    PlaceObj sourcePlace = null;
    PlaceObj destination = null;
    ArrayList<PlaceObj> p = new ArrayList<PlaceObj>();
    String sourceName = "";
    String destinationName = "";
    ArrayList<Ride> rides = new ArrayList<>();
    private ListView lsv_main;
    private ListAdapter mListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lsv_main = (ListView) findViewById(R.id.listView);

        //Source
        sourceAutocompleteFragment = (PlaceAutocompleteFragment)
                getFragmentManager().findFragmentById(R.id.place_source);

        AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
                .setTypeFilter(AutocompleteFilter.TYPE_FILTER_ADDRESS)
                .build();
        sourceAutocompleteFragment.setFilter(typeFilter);

        actionSource();

        //Destination
        //Source
        destinationAutocompleteFragment = (PlaceAutocompleteFragment)
                getFragmentManager().findFragmentById(R.id.place_destination);

        typeFilter = new AutocompleteFilter.Builder()
                .setTypeFilter(AutocompleteFilter.TYPE_FILTER_ADDRESS)
                .build();
        destinationAutocompleteFragment.setFilter(typeFilter);

        actionDestination();


    }


    public void actionSource() {

        final ArrayList<Ride> rideTemp = new ArrayList<Ride>();

        sourceAutocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(com.google.android.gms.location.places.Place place) {

                // TODO: Get info about the selected place.
                Toast.makeText(getApplicationContext(), "Place:" + place.getName(), Toast.LENGTH_SHORT).show();
                sourceName = (String) place.getName();
                Double latitude = place.getLatLng().latitude;
                Double longitude = place.getLatLng().longitude;
                String address = (String) place.getAddress();

                API.getInstance(getApplicationContext()).getRideByStartPlace(latitude, longitude, new ResponseListener() {  //get ride
                            @Override
                            public void onResponse(String str) {
                                super.onResponse(str);
                                JSONArray jsonArray = null;
                                System.out.println(str);
                                try {
                                    jsonArray = new JSONArray(str);
                                    JSONObject jsonData = jsonArray.getJSONObject(0);  //{}->1 object
                                    if (jsonData.has("msg")) {

                                        if (jsonData.getString("msg").equals("No exist ride with the entered start point.")) {
                                            Toast.makeText(getApplicationContext(), "No exist ride with the entered start point.", Toast.LENGTH_SHORT).show();
                                        }
                                    } else {  //get the ride info and store to ride Object
                                        for (int i = 0; i < jsonArray.length(); i++) {


                                            jsonData = jsonArray.getJSONObject(i);
                                            System.out.println(jsonData);
                                            int rideId = Integer.parseInt(jsonData.getString("rideId"));
                                            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                                            Date createOn = format.parse(jsonData.getString("create_on"));
                                            System.out.println(createOn);
                                            String travelDateTime = jsonData.getString("travel_start_time");
                                            int sourceId = Integer.parseInt(jsonData.getString("source_place_id"));


                                            // String sourceName = source.getName();


                                            getPlace(sourceId, new VolleyCallback() {
                                                @Override
                                                public void onSuccessResponse(String result) {
                                                    SharedPreferences sharedPreferences = getSharedPreferences("place", MODE_PRIVATE);
                                                    String json = sharedPreferences.getString("placeObj", "");
                                                    sourcePlace = new Gson().fromJson(json, PlaceObj.class);

                                                }
                                            });

                                            int destinationId = Integer.parseInt(jsonData.getString("destination_place_id"));
                                            getPlace(destinationId, new VolleyCallback() {
                                                @Override
                                                public void onSuccessResponse(String result) {
                                                    SharedPreferences sharedPreferences = getSharedPreferences("place", MODE_PRIVATE);
                                                    String json = sharedPreferences.getString("placeObj", "");
                                                    destination = new Gson().fromJson(json, PlaceObj.class);
                                                }
                                            });


                                            int seats = Integer.parseInt(jsonData.getString("seats_offered"));
                                            String remark = jsonData.getString("remark");
                                            Ride ride = new Ride(rideId, createOn, travelDateTime, sourcePlace, destination, seats, remark, sourceId, destinationId);

                                            rideTemp.add(ride);


                                        }
                                        System.out.println("myArry " + rideTemp);
                                        showRide(rideTemp);
                                    }

                                } catch (JSONException e) {
                                    e.printStackTrace();
                                } catch (ParseException e) {
                                    e.printStackTrace();
                                }
                                System.out.println(str);

                            }

                            @Override
                            public void onError(VolleyError error) {
                                super.onError(error);
                            }
                        }
                );

            }

            @Override
            public void onError(Status status) {
                // TODO: Handle the error.
                Toast.makeText(getApplicationContext(), "error" + status, Toast.LENGTH_SHORT).show();
            }
        });
        System.out.println("Rides array:" + rides);

    }

    public void actionDestination() {

        final ArrayList<Ride> rideTemp = new ArrayList<Ride>();

        destinationAutocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(com.google.android.gms.location.places.Place place) {

                // TODO: Get info about the selected place.
                Toast.makeText(getApplicationContext(), "Place:" + place.getName(), Toast.LENGTH_SHORT).show();
                destinationName = (String) place.getName();
                System.out.println("rSource: " + sourceName);
                Double latitude = place.getLatLng().latitude;
                Double longitude = place.getLatLng().longitude;
                String address = (String) place.getAddress();

                API.getInstance(getApplicationContext()).getRideByStartDestinationPlace(sourceName, latitude, longitude, new ResponseListener() {  //get ride
                            @Override
                            public void onResponse(String str) {
                                super.onResponse(str);
                                JSONArray jsonArray = null;
                                System.out.println(str);
                                try {
                                    jsonArray = new JSONArray(str);
                                    JSONObject jsonData = jsonArray.getJSONObject(0);  //{}->1 object
                                    if (jsonData.has("msg")) {

                                        if (jsonData.getString("msg").equals("No exist ride with the entered start point.")) {
                                            Toast.makeText(getApplicationContext(), "No exist ride with the entered start point.", Toast.LENGTH_SHORT).show();
                                        }
                                    } else {  //get the ride info and store to ride Object
                                        for (int i = 0; i < jsonArray.length(); i++) {
                                            jsonData = jsonArray.getJSONObject(i);
                                            System.out.println(jsonData);
                                            int rideId = Integer.parseInt(jsonData.getString("rideId"));
                                            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                                            Date createOn = format.parse(jsonData.getString("create_on"));
                                            System.out.println(createOn);
                                            String travelDateTime = jsonData.getString("travel_start_time");
                                            int sourceId = Integer.parseInt(jsonData.getString("source_place_id"));

                                            getPlace(sourceId, new VolleyCallback() {
                                                @Override
                                                public void onSuccessResponse(String result) {
                                                    SharedPreferences sharedPreferences = getSharedPreferences("place", MODE_PRIVATE);
                                                    String json = sharedPreferences.getString("placeObj", "");
                                                    sourcePlace = new Gson().fromJson(json, PlaceObj.class);

                                                }
                                            });

                                            int destinationId = Integer.parseInt(jsonData.getString("destination_place_id"));
                                            getPlace(destinationId, new VolleyCallback() {
                                                @Override
                                                public void onSuccessResponse(String result) {
                                                    SharedPreferences sharedPreferences = getSharedPreferences("place", MODE_PRIVATE);
                                                    String json = sharedPreferences.getString("placeObj", "");
                                                    destination = new Gson().fromJson(json, PlaceObj.class);
                                                }
                                            });


                                            int seats = Integer.parseInt(jsonData.getString("seats_offered"));
                                            String remark = jsonData.getString("remark");
                                            Ride ride = new Ride(rideId, createOn, travelDateTime, sourcePlace, destination, seats, remark, sourceId, destinationId);

                                            rideTemp.add(ride);


                                        }
                                        System.out.println("myArry " + rideTemp);
                                        showRide(rideTemp);
                                    }

                                } catch (JSONException e) {
                                    e.printStackTrace();
                                } catch (ParseException e) {
                                    e.printStackTrace();
                                }
                                System.out.println(str);

                            }

                            @Override
                            public void onError(VolleyError error) {
                                super.onError(error);
                            }
                        }
                );

            }

            @Override
            public void onError(Status status) {
                // TODO: Handle the error.
                Toast.makeText(getApplicationContext(), "error" + status, Toast.LENGTH_SHORT).show();
            }
        });
        System.out.println("Rides array:" + rides);

    }

    public void getPlace(int id, final VolleyCallback callback) {
        SharedPreferences pref = getSharedPreferences("place", MODE_PRIVATE);
        final SharedPreferences.Editor editor = pref.edit();

        final PlaceObj[] placeBean = new PlaceObj[1];
        API.getInstance(getApplicationContext()).getPlaceById(id, new ResponseListener() {

            @Override
            public void onResponse(String str) {
                super.onResponse(str);
                JSONArray jsonArray = null;


                try {
                    jsonArray = new JSONArray(str);
                    JSONObject jsonData = jsonArray.getJSONObject(0);
                    int placeId = Integer.parseInt(jsonData.getString("placeId"));
                    String name = jsonData.getString("name");
                    double latitude = Double.parseDouble(jsonData.getString("latitude"));
                    double longitude = Double.parseDouble(jsonData.getString("longitude"));
                    String address = jsonData.getString("address");
                    System.out.println("address" + name);
                    PlaceObj placeObj = new PlaceObj(placeId, name, latitude, longitude, address);
                    Gson gson = new Gson();
                    String json = gson.toJson(placeObj);
                    editor.putString("placeObj" + placeId, json);
                    editor.commit();
                    no++;
                    callback.onSuccessResponse(str);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                System.out.println(str);
            }

            @Override
            public void onError(VolleyError error) {
                super.onError(error);
            }
        });

    }


    public void showRide(final ArrayList<Ride> rides) {
        final String[] myarray = new String[10];
        List<Map<String, Object>> itemList = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < rides.size(); i++) {

            final Ride ride = rides.get(i);
            final Map<String, Object> item = new HashMap<String, Object>();
            item.put(ITEM_TITLE, rides.get(i).getTravelDateTime());
            System.out.println("sourceid: " + rides.get(i).getSourceID());
            SharedPreferences sharedPreferences = getSharedPreferences("place", MODE_PRIVATE);  //test as login info
            String json = sharedPreferences.getString("placeObj" + rides.get(i).getSourceID(), "");
            PlaceObj a = new Gson().fromJson(json, PlaceObj.class);
            String creator_id = a.getName();
            System.out.println("Name:" + creator_id);
            item.put(ITEM_SOURCE, creator_id);

            json = sharedPreferences.getString("placeObj" + rides.get(i).getDestinationID(), "");
            a = new Gson().fromJson(json, PlaceObj.class);
            creator_id = a.getName();
            System.out.println("Name:" + creator_id);
            item.put(ITEM_DESTINATION, creator_id);
            itemList.add(item);
        }

        mListAdapter = new ListAdapter(MainActivity.this, itemList);
        lsv_main.setAdapter(mListAdapter);

    }
}

activity_main.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="match_parent"
    android:orientation="vertical">

    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="10px"
        android:paddingRight="10px"
        android:shrinkColumns="0"
        android:stretchColumns="1">

        <TableRow>

            <TextView
                android:id="@+id/tv_start"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/start" />

            <fragment
                android:id="@+id/place_source"
                android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </TableRow>

        <TableRow>

            <TextView
                android:id="@+id/tv_destination"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/destination" />

            <fragment
                android:id="@+id/place_destination"
                android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </TableRow>

        <TableRow>

            <Button
                android:id="@+id/btn_date"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/pick_date" />

            <TextView
                android:id="@+id/tv_date"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_span="2" />
        </TableRow>

        <TableRow>

            <Button
                android:id="@+id/btn_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/pick_time" />

            <TextView
                android:id="@+id/tv_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_span="2" />
        </TableRow>
    </TableLayout>

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" />
</LinearLayout>

Logcat

05-18 15:44:22.237 7813-7813/fyp.wcm.shareourride I/System.out:  [{"rideId":1,"create_on":"2018-03-07","travel_start_time":"2018-03-07 17:17:00","source_place_id":1,"destination_place_id":2,"seats_offered":2,"creator_id":1,"remark":"music taxi","placeId":1,"name":"Harbour Heights Ko Fung Court","latitude":22.2882477,"longitude":114.1907985,"address":"Harbour Heights Ko Fung Court, 5 Fook Yum Rd, Causeway Bay, Hong Kong"},{"rideId":2,"create_on":"2018-03-10","travel_start_time":"2018-03-10 18:54:00","source_place_id":1,"destination_place_id":3,"seats_offered":3,"creator_id":1,"remark":"good as","placeId":1,"name":"Harbour Heights Ko Fung Court","latitude":22.2882477,"longitude":114.1907985,"address":"Harbour Heights Ko Fung Court, 5 Fook Yum Rd, Causeway Bay, Hong Kong"}]
    05-18 15:44:22.238 7813-7813/fyp.wcm.shareourride I/System.out: {"rideId":1,"create_on":"2018-03-07","travel_start_time":"2018-03-07 17:17:00","source_place_id":1,"destination_place_id":2,"seats_offered":2,"creator_id":1,"remark":"music taxi","placeId":1,"name":"Harbour Heights Ko Fung Court","latitude":22.2882477,"longitude":114.1907985,"address":"Harbour Heights Ko Fung Court, 5 Fook Yum Rd, Causeway Bay, Hong Kong"}
    05-18 15:44:22.238 7813-7813/fyp.wcm.shareourride I/System.out: Wed Mar 07 00:00:00 GMT+08:00 2018
    05-18 15:44:22.240 7813-7813/fyp.wcm.shareourride I/System.out: {"rideId":2,"create_on":"2018-03-10","travel_start_time":"2018-03-10 18:54:00","source_place_id":1,"destination_place_id":3,"seats_offered":3,"creator_id":1,"remark":"good as","placeId":1,"name":"Harbour Heights Ko Fung Court","latitude":22.2882477,"longitude":114.1907985,"address":"Harbour Heights Ko Fung Court, 5 Fook Yum Rd, Causeway Bay, Hong Kong"}
    05-18 15:44:22.241 7813-7813/fyp.wcm.shareourride I/System.out: Sat Mar 10 00:00:00 GMT+08:00 2018
    05-18 15:44:22.241 7813-7813/fyp.wcm.shareourride I/System.out: myArry [fyp.wcm.shareourride.Ride@c945316, fyp.wcm.shareourride.Ride@57c4497]
    05-18 15:44:22.243 7813-7813/fyp.wcm.shareourride I/System.out: sourceid: 1
    05-18 15:44:22.249 7813-7813/fyp.wcm.shareourride D/AndroidRuntime: Shutting down VM


                                                                        --------- beginning of crash
    05-18 15:44:22.250 7813-7813/fyp.wcm.shareourride E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: fyp.wcm.shareourride, PID: 7813
                                                                        java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String fyp.wcm.shareourride.PlaceObj.getName()' on a null object reference
                                                                            at fyp.wcm.shareourride.MainActivity.showRide(MainActivity.java:401)
                                                                            at fyp.wcm.shareourride.MainActivity$2$1.onResponse(MainActivity.java:212)
                                                                            at fyp.wcm.shareourride.CustomJsonObjectRequest$1.onResponse(CustomJsonObjectRequest.java:25)
                                                                            at fyp.wcm.shareourride.CustomJsonObjectRequest$1.onResponse(CustomJsonObjectRequest.java:22)
                                                                            at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
                                                                            at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
                                                                            at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
                                                                            at android.os.Handler.handleCallback(Handler.java:751)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                            at android.os.Looper.loop(Looper.java:159)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:6097)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
    05-18 15:44:22.252 1622-2345/? W/ActivityManager:   Force finishing activity fyp.wcm.shareourride/.MainActivity
    05-18 15:44:22.256 1622-2345/? I/InputDispatcher: Window 'Window{8721876 u0 fyp.wcm.shareourride/fyp.wcm.shareourride.MainActivity}' spent 0.0ms processing input events on average(0/0). Monitor spent 0.0ms processing input events on average(0/0).
    05-18 15:44:22.258 1622-2345/? D/ActivityTrigger: ActivityTrigger activityPauseTrigger 
  • Emulator API Level:23:5554

  • Real Device API Level: 24

Amy Chan
  • 1
  • 2

1 Answers1

0

There is one feature in API level 24 which is android:allowBackup

Whether to allow the application to participate in the backup and restore infrastructure. If this attribute is set to false, no backup or restore of the application will ever be performed, even by a full-system backup that would otherwise cause all application data to be saved via adb. The default value of this attribute is true.

In short if android:allowBackup is true in manifest your Sharedpreferences data will be restored, ie if you logged in and store your userId in Sharedpreferences it will remain stored whether you uninstall or install new Application. So just add android:allowBackup="false" you manifest under <application> tag.

Jyot
  • 540
  • 5
  • 17