我在我的应用程序中使用 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();
}
任何帮助将不胜感激!