我在我的应用程序中使用ButterKnife 6.0.0,但在旋转片段后我得到一个NullPointerException:
我的代码:
public class AddFriendFragment extends Fragment {
@InjectView(R.id.userSearchAddFriend)
ImageButton addFriendBtn;
@InjectView(R.id.userSearchName)
TextView name;
@InjectView(R.id.userSearchEmail)
TextView email;
@InjectView(R.id.userSearchProfilePicture)
ImageView profile;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_userfragment, container,
false);
ButterKnife.inject(this, v);
return v;
}
@Override
public void onDestroyView() {
super.onDestroyView();
ButterKnife.reset(this);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final String id = getArguments().getString("user");
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.fromLocalDatastore();
query.whereEqualTo("objectId",id);
query.getFirstInBackground(new GetCallback<ParseUser>() {
@Override
public void done(final ParseUser parseUser, ParseException e) {
if(e != null || parseUser == null)
{
//logar erro
return;
}
name.setText(parseUser.get("firstName") + " " + parseUser.get("lastName"));
email.setText(parseUser.getUsername());
ParseFile file =(ParseFile) parseUser.get("profileImg");
if(file != null) {
Picasso.with(getActivity()).load(file.getUrl()).into(profile);
}
addFriendBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
addFriend(parseUser);
}
});
}
});
}
堆栈跟踪:
11-18 17:03:41.240 8468-8468/com.maddogs.mymoney E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.maddogs.mymoney, PID: 8468
java.lang.NullPointerException
at com.maddogs.mymoney.fragments.AddFriendFragment$1.done(AddFriendFragment.java:101)
at com.maddogs.mymoney.fragments.AddFriendFragment$1.done(AddFriendFragment.java:92)
at com.parse.GetCallback.internalDone(GetCallback.java:43)
at com.parse.GetCallback.internalDone(GetCallback.java:29)
at com.parse.Parse$6$1.run(Parse.java:940)
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:5086)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
----编辑----
第101行
name.setText(parseUser.get("firstName") + " " + parseUser.get("lastName"));
活动中的电话:
AddFriendFragment userFragm = new AddFriendFragment();
Bundle bundle = new Bundle();
bundle.putString("user",event.getUser().getObjectId());
userFragm.setArguments(bundle);
getSupportFragmentManager().beginTransaction()
.replace(R.id.root_frame, userFragm)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.addToBackStack(null).commit();
}
非常感谢任何帮助!
答案 0 :(得分:2)
在方向更改时,应用程序将调用onDestroy()。在你的情况下,这将通过调用'ButterKnife.reset'来重置ButterKnife注入。
但onCreateView不会在方向更改生命周期中调用。
如果将ButterKnife注入调用从onCreateView移到onViewCreated,问题可能会得到解决。
答案 1 :(得分:1)
实际上我使用https://github.com/greenrobot/EventBus中的EventBus解决了这个问题。 我只是在回调上使用post事件并将其放在片段本身上,然后我管理视图更改。
像这样:
query.fromLocalDatastore();
query.whereEqualTo("objectId", id);
query.getFirstInBackground(new GetCallback<ParseUser>() {
@Override
public void done(final ParseUser parseUser, ParseException e) {
EventBus.getDefault().post(new AddFriendEvent(e == null, parseUser));
}
});
然后:
public void onEventMainThread(final AddFriendEvent event) {
if (!event.isResult())
return;
name.setText(event.getUser().get("firstName") + " " + event.getUser().get("lastName"));
email.setText(event.getUser().getUsername());
ParseFile file = (ParseFile) event.getUser().get("profileImg");
if (file != null) {
Picasso.with(getActivity()).load(file.getUrl()).into(profile);
}
addFriendBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addFriend(event.getUser());
}
});
addFriendBtn.setOnClickListener(null);
addFriendBtn.setImageResource(R.drawable.ic_action_accept);
}
谢谢!