它适用于模拟器和真实设备(Android版本4),但它不适用于真实设备(android version7.0)
相关代码: ```
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_card_action, container);
application = (LaftelApplication) getActivity().getApplication();
userId = application.getUserInfo().id;
ratingBar = (RatingBar) view.findViewById(R.id.rating_bar);
ImageView btnDeleteRating = (ImageView) view.findViewById(R.id.btn_delete_rating);
btnSubscribe = (CheckableLinearLayout) view.findViewById(R.id.btn_subscribe);
btnWish = (CheckableLinearLayout) view.findViewById(R.id.wish_item);
btnRecommend = (CheckableLinearLayout) view.findViewById(R.id.btn_recommend);
btnBanRecommend = (CheckableLinearLayout) view.findViewById(R.id.ban_recommend);
mContext = getContext();
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
mItem = (Item) getArguments().getSerializable("item");
ImageView itemTypeView = (ImageView) view.findViewById(R.id.item_type);
if (mItem.type.equals(Constants.DB_ITEM_TYPE_ANIMATION)) {
itemTypeView.setImageResource(R.drawable.label_ani_round);
} else if (mItem.type.equals(Constants.DB_ITEM_TYPE_WEBTOON)) {
itemTypeView.setImageResource(R.drawable.label_webtoon_round);
} else if (mItem.type.equals(Constants.DB_ITEM_TYPE_COMICS)) {
itemTypeView.setImageResource(R.drawable.label_comics_round);
} else if (mItem.type.equals(Constants.DB_ITEM_TYPE_LIGHTNOVEL)) {
itemTypeView.setImageResource(R.drawable.label_lnovel_round);
}
TextView itemNameView = (TextView) view.findViewById(R.id.item_name);
itemNameView.setText(mItem.name);
// CardAction Setting
Call<RatingList> userPreferenceCall = application.service.getUserPreference(userId);
userPreferenceCall.enqueue(new Callback<RatingList>() {
@Override
public void onResponse(Call<RatingList> call, Response<RatingList> response) {
userRating = response.body();
setUserPreferences();
}
@Override
public void onFailure(Call<RatingList> call, Throwable t) {
}
});
Call<SubscriptionList> userSubcriptionCall = application.service.getUserSubcriptionSet("application/json", userId);
userSubcriptionCall.enqueue(new Callback<SubscriptionList>() {
@Override
public void onResponse(Call<SubscriptionList> call, Response<SubscriptionList> response) {
userSubcriptionInfo = response.body();
setUserSusbscription();
}
@Override
public void onFailure(Call<SubscriptionList> call, Throwable t) {
}
});
```
Log cat: ```
05-23 10:31:39.464 17342-17342 / laftel.net.laftel E / AndroidRuntime:FATAL EXCEPTION:main流程:laftel.net.laftel,PID:17342 java.lang.NullPointerException:尝试从字段中读取&#39; int laftel.net.laftel.models.UserInfo.id&#39;在null对象引用上 在laftel.net.laftel.views.CardActionFragment.onCreateView(CardActionFragment.java:61) 在android.support.v4.app.Fragment.performCreateView(Fragment.java:2192) 在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299) 在android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528) 在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595) 在android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758) 在android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363) 在android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149) 在android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103) 在android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013) 在android.support.v4.app.FragmentManagerImpl $ 1.run(FragmentManager.java:710) 在android.os.Handler.handleCallback(Handler.java:751) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:6247) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:872) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
```
答案 0 :(得分:0)
将application = (LaftelApplication) getActivity().getApplication();
从onCreateView
移至onActivityCreated
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
application = (LaftelApplication) getActivity().getApplication();
}