Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: Android app working on emulator but not real device

Ebet:

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 fins on Emulator but it stop working after i have pick the source name when i try to run the app on a real device.

Can someone help me understand why?

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 p = new ArrayList();
String sourceName = "";
String destinationName = "";
ArrayList 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 rideTemp = new ArrayList();

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

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 rideTemp = new ArrayList();

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 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 rides) {
final String[] myarray = new String[10];
List> itemList = new ArrayList>();
for (int i = 0; i
final Ride ride = rides.get(i);
final Map item = new HashMap();
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



android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

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



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

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" />




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

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" />











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" />


Logcat when running on emulator https://drive.google.com/open?id=1uS9Ma-ROZdkmTRN-Jc1MyaoJC26jmjJ3

  • Emulator API Level:23:5554
  • Real Device API Level: 24


Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: Android app working on emulator but not real device

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×