[英]Retrofit - app crashes when I call intent to open new Activity
I am relatively new to android, and I just started playing with retrofit. 我是android的新手,我刚开始玩翻新游戏。 Currently I am making a restaurant app.
目前,我正在制作餐厅应用。 At first I was using volley to get the restaurant list from the server.
起初,我使用排球从服务器获取餐厅清单。 I downloaded the restaurant info to local database and added it to my mapFragment and restaurantListFragment.
我将餐厅信息下载到本地数据库,并将其添加到我的mapFragment和restaurantListFragment中。 Then I decided to start using retrofit instead of volley.
然后,我决定开始使用翻新而不是排球。 I succeeded to do the same with Retrofit - display restaurants in my two fragments.
我成功完成了Retrofit的操作-在两个片段中显示餐厅。
But now I have a weird problem that, I think, should not be related to retrofit - if I try to open an intent my app crashes . 但是,现在我有一个怪异的问题,我认为这不应该与改造有关—如果我试图打开一个意图,则我的应用程序崩溃了 。 I have a navdrawer that crashes when I try to open activity from it.
我有一个navdrawer,当我尝试从中打开活动时会崩溃。 Also I had an option to add a restaurant on longclick on map.
我也可以选择在地图上的longclick上添加餐厅。 Longclick would call an intent and open setRestaurantActivity.
Longclick会调用一个意图并打开setRestaurantActivity。 It also crashes.
它也会崩溃。
This is what I get when I try to open a navdrawer activity. 这是我尝试打开navdrawer活动时得到的。
02-06 11:45:31.862 18777-18777/? D/AndroidRuntime: Shutting down VM
02-06 11:45:31.862 18777-18777/? W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x5cc97b20)
02-06 11:45:31.862 7010-7080/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-Application: Application crash has been observed.
02-06 11:45:31.862 7010-7080/com.bluestacks.BstCommandProcessor W/BstCommandProcessor-Application: in sendHttpRequest, requestType is of CRASH_APP type but one of the requiredInfo is NULL, crashedApp = com.bluestacks.BstCommandProcessor.BstCrashedAppInfo@2e75f9cc
02-06 11:45:31.862 18777-18777/? I/Process: Sending signal. PID: 18777 SIG: 9
02-06 11:45:31.862 18777-18777/? D/AndroidRuntime: procName from cmdline: com.weekendcoder.kemo.restaurant
02-06 11:45:31.862 18777-18777/? E/AndroidRuntime: in writeCrashedAppName, pkgName :com.weekendcoder.kemo.restaurant
02-06 11:45:31.862 18777-18777/? D/AndroidRuntime: file written successfully with content: com.weekendcoder.kemo.restaurant StringBuffer : ;com.weekendcoder.kemo.restaurant
02-06 11:45:31.862 18777-18777/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.weekendcoder.kemo.restaurant, PID: 18777
java.lang.RuntimeException: Parcel: unable to marshal value com.weekendcoder.kemo.restaurant.model.Restaurant@2e7b673c
at android.os.Parcel.writeValue(Parcel.java:1266)
at android.os.Parcel.writeList(Parcel.java:653)
at android.os.Parcel.writeValue(Parcel.java:1226)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:618)
at android.os.Bundle.writeToParcel(Bundle.java:1692)
at android.os.Parcel.writeBundle(Parcel.java:636)
at android.support.v4.app.FragmentState.writeToParcel(Fragment.java:148)
at android.os.Parcel.writeTypedArray(Parcel.java:1133)
at android.support.v4.app.FragmentManagerState.writeToParcel(FragmentManager.java:563)
at android.os.Parcel.writeParcelable(Parcel.java:1285)
at android.os.Parcel.writeValue(Parcel.java:1204)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:618)
at android.os.Bundle.writeToParcel(Bundle.java:1692)
at android.os.Parcel.writeBundle(Parcel.java:636)
at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:2474)
at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3098)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5021)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
at dalvik.system.NativeStart.main(Native Method)
02-06 11:45:31.872 6820-12714/system_process I/ActivityManager: Process com.weekendcoder.kemo.restaurant (pid 18777) has died.
02-06 11:45:31.872 6820-12714/system_process W/ActivityManager: Force removing ActivityRecord{2e93b954 u0 com.weekendcoder.kemo.restaurant/.navdraweractivities.MyProfileActivity t11}: app died, no saved state
02-06 11:45:31.872 6820-12714/system_process V/WindowManager: isVisibleLw false for win : Window{2e9a6c20 u0 com.weekendcoder.kemo.restaurant/com.weekendcoder.kemo.restaurant.navdraweractivities.MyProfileActivity}
02-06 11:45:31.872 6820-12714/system_process W/ActivityManager: Force removing ActivityRecord{2e7170c8 u0 com.weekendcoder.kemo.restaurant/.MainActivity t11}: app died, no saved state
02-06 11:45:31.872 6820-12714/system_process D/ActivityManager: TopActivityInfo, pkgName: com.bluestacks.gamepophome activityName: com.bluestacks.gamepophome/com.uncube.launcher3.Launcher callingPackage: bstSpecialAppKeyboardHandlingEnabled = false
02-06 11:45:31.872 6820-7036/system_process I/WindowState: WIN DEATH: Window{2e9a6c20 u0 com.weekendcoder.kemo.restaurant/com.weekendcoder.kemo.restaurant.navdraweractivities.MyProfileActivity}
02-06 11:45:31.872 6820-7008/system_process I/WindowState: WIN DEATH: Window{2ea83b54 u0 com.weekendcoder.kemo.restaurant/com.weekendcoder.kemo.restaurant.MainActivity}
02-06 11:45:31.872 7023-7023/com.bluestacks.appguidance D/GuidanceScreen: event === app_launch
02-06 11:45:31.872 7023-7023/com.bluestacks.appguidance D/GuidanceScreen: hiding guidance
02-06 11:45:31.872 7023-7023/com.bluestacks.appguidance D/GuidanceScreen: hardKeyboard = 1
02-06 11:45:31.872 7023-7023/com.bluestacks.appguidance D/GuidanceScreen: controllerType === Keyboard
02-06 11:45:31.872 6820-12714/system_process D/ActivityManager: Showing guidance for pkgName: com.bluestacks.gamepophome
02-06 11:45:31.882 6810-6810/? W/SurfaceFlinger: couldn't log to binary event log: overflow.
02-06 11:45:31.882 7023-7023/com.bluestacks.appguidance D/GuidanceScreen: appName: Torque Launcher, currentPkg: com.bluestacks.gamepophome, event: app_launch, controller: Keyboard
02-06 11:45:31.882 7023-7023/com.bluestacks.appguidance D/GuidanceScreen: appName: Torque Launcher
02-06 11:45:31.892 7010-19051/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-httpd: command: ping
02-06 11:45:31.892 7010-19051/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-httpd: response: {"result":"ok"}
02-06 11:45:31.892 7010-19052/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-httpd: command: getdefaultlauncher
02-06 11:45:31.892 7010-19052/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-httpd: response: {"defaultLauncher":"com.bluestacks.gamepophome","result":"ok"}
02-06 11:45:31.902 7010-19053/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-httpd: command: ping
02-06 11:45:31.902 7010-19053/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-httpd: response: {"result":"ok"}
02-06 11:45:31.902 7010-19055/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-httpd: command: ping
02-06 11:45:31.902 7010-19055/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-httpd: response: {"result":"ok"}
02-06 11:45:31.912 7010-19054/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-httpd: command: StopApp com.weekendcoder.kemo.restaurant
02-06 11:45:31.912 7010-19054/com.bluestacks.BstCommandProcessor D/BstCommandProcessor-Application: in isSystemReady, isBootCompleted true External storage status: mounted External storage dir :/storage/sdcard isExternalStorageRemovable:true
This is how my navdrawer looks like (was not changed after I started using retrofitting), and worked before: 这是我的navdrawer的样子(在我开始使用改造后没有改变),并且在以下方面起作用:
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_account) {
// Handle the account action
Intent accountIntent = new Intent(MainActivity.this, MyProfileActivity.class);
startActivity(accountIntent);
} else if (id == R.id.nav_contacts) {
Intent contactsIntent = new Intent(MainActivity.this, FavouriteRestaurantsListActivity.class);
startActivity(contactsIntent);
} else if (id == R.id.nav_blocked) {
Intent blockedIntent = new Intent(MainActivity.this, BlockedRestaurantsListActivity.class);
startActivity(blockedIntent);
} else if (id == R.id.nav_logout) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.logging_out)
.setMessage(R.string.logout_yes_no)
.setCancelable(false)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
logoutUser();
}
})
.setNegativeButton(R.string.no, null);
AlertDialog alert = builder.show();
TextView messageText = (TextView) alert.findViewById(android.R.id.message);
if (messageText != null) {
messageText.setGravity(Gravity.CENTER);
}
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
And this is a call to setRestaurantActivity called from my map fragment: 这是从我的地图片段调用的setRestaurantActivity的调用:
mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
@Override
public void onMapLongClick(final LatLng arg0) {
RequestQueue queue = Volley.newRequestQueue(getActivity());
String url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + String.valueOf(arg0.latitude) + "," + String.valueOf(arg0.longitude) + "&key=myKey ";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray jObj = new JSONObject(response).getJSONArray("results").getJSONObject(0).getJSONArray("address_components");
Intent intent = new Intent(getActivity(), SetRestaurantActivity.class);
for (int i = 0; i < jObj.length(); i++) {
String componentName = new JSONObject(jObj.getString(i)).getJSONArray("types").getString(0);
if (componentName.equals("postal_code") || componentName.equals("locality") || componentName.equals("street_number") || componentName.equals("route")
|| componentName.equals("neighborhood") || componentName.equals("sublocality") || componentName.equals("administrative_area_level_2")
|| componentName.equals("administrative_area_level_1") || componentName.equals("country")) {
intent.putExtra(componentName, new JSONObject(jObj.getString(i)).getString("short_name"));
}
}
intent.putExtra("latitude", arg0.latitude);
intent.putExtra("longitude", arg0.longitude);
startActivity(intent);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
int x = 1;
}
});
queue.add(stringRequest);
}
});
My Restaurant model: 我的餐厅型号:
import java.io.Serializable;
import com.google.gson.annotations.SerializedName;
import java.util.ArrayList;
import java.util.List;
public class Restaurant {
@SerializedName("user_id")
private String userId;
@SerializedName("name")
private String userName;
@SerializedName("message")
private String message;
@SerializedName("image")
private String thumbnailUrl;
@SerializedName("latitude")
private String lat;
@SerializedName("longitude")
private String lon;
@SerializedName("event_date")
private String date;
@SerializedName("event_time")
private String time;
@SerializedName("id")
private String id;
@SerializedName("created_datetime")
private String createdDateTime;
public Restaurant() {
}
public Restaurant(String name, String thumbnailUrl, String date, String time, String lat, String lon) {
this.userName = name;
this.thumbnailUrl = thumbnailUrl;
this.date = date;
this.time = time;
this.lat = lat;
this.lon = lon;
}
public String getUserName() {
return userName;
}
public void setUserName(String name) {
this.userName = name;
}
public String getThumbnailUrl() {
return thumbnailUrl;
}
public void setThumbnailUrl(String thumbnailUrl) {
this.thumbnailUrl = thumbnailUrl;
}
public String getLat() {
return lat;
}
public void setLat(String latitude) {
this.lat = latitude;
}
public String getLon() {
return lon;
}
public void setLon(String longitude) {
this.lon = longitude;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getCreatedDateTime() {
return createdDateTime;
}
public void setCreatedDateTime(String createdDateTime) {
this.createdDateTime = createdDateTime;
}
}
onSavedInstanceState: onSavedInstanceState:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
// save the tutorial page (or something else)
savedInstanceState.putInt("TutPage", tutorialPage);
savedInstanceState.putBoolean("tutUsed", tutorialUsed);
// more additions possible
}
Edited Restaurant model - working version: 编辑的餐厅模型-工作版本:
import android.os.Parcel;
import android.os.Parcelable;
import java.io.Serializable;
import com.google.gson.annotations.SerializedName;
import java.util.ArrayList;
import java.util.List;
public class Restaurant implements Parcelable{
@SerializedName("user_id")
private String userId;
@SerializedName("name")
private String userName;
@SerializedName("message")
private String message;
@SerializedName("image")
private String thumbnailUrl;
@SerializedName("latitude")
private String lat;
@SerializedName("longitude")
private String lon;
@SerializedName("event_date")
private String date;
@SerializedName("event_time")
private String time;
@SerializedName("id")
private String id;
@SerializedName("created_datetime")
private String createdDateTime;
public Restaurant() {
}
public Restaurant(String name, String thumbnailUrl, String date, String time, String lat, String lon) {
this.userName = name;
this.thumbnailUrl = thumbnailUrl;
this.date = date;
this.time = time;
this.lat = lat;
this.lon = lon;
}
protected Restaurant(Parcel in) {
userId = in.readString();
userName = in.readString();
message = in.readString();
thumbnailUrl = in.readString();
lat = in.readString();
lon = in.readString();
date = in.readString();
time = in.readString();
id = in.readString();
createdDateTime = in.readString();
}
public static final Creator<Restaurant> CREATOR = new Creator<Restaurant>() {
@Override
public Restaurant createFromParcel(Parcel in) {
return new Restaurant(in);
}
@Override
public Restaurant[] newArray(int size) {
return new Restaurant[size];
}
};
public String getUserName() {
return userName;
}
public void setUserName(String name) {
this.userName = name;
}
public String getThumbnailUrl() {
return thumbnailUrl;
}
public void setThumbnailUrl(String thumbnailUrl) {
this.thumbnailUrl = thumbnailUrl;
}
public String getLat() {
return lat;
}
public void setLat(String latitude) {
this.lat = latitude;
}
public String getLon() {
return lon;
}
public void setLon(String longitude) {
this.lon = longitude;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getCreatedDateTime() {
return createdDateTime;
}
public void setCreatedDateTime(String createdDateTime) {
this.createdDateTime = createdDateTime;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(userId);
parcel.writeString(userName);
parcel.writeString(message);
parcel.writeString(thumbnailUrl);
parcel.writeString(lat);
parcel.writeString(lon);
parcel.writeString(date);
parcel.writeString(time);
parcel.writeString(id);
parcel.writeString(createdDateTime);
}
}
From line: 从行:
java.lang.RuntimeException: Parcel: unable to marshal value com.weekendcoder.kemo.restaurant.model.Restaurant@2e7b673c
one may deduct, that Restaurant
object wants to be written into a parcel, but it can't happen, because Restaurant
is not parcelable. 可以推断出,
Restaurant
对象想被写入包裹中,但是这种情况不会发生,因为Restaurant
不可包裹。
Making Restaurant
class implement Parcelable
interface would solve the issue. 使
Restaurant
类实现Parcelable
接口可以解决此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.