4

我在我的应用程序中使用 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();
    }

任何帮助将不胜感激!

4

2 回答 2

2

在方向改变时,应用程序将调用 onDestroy()。在您的情况下,这将通过调用“ButterKnife.reset”来重置 ButterKnife 注入。

但是 onCreateView 不会在方向更改生命周期中调用。

如果您将 ButterKnife 注入调用从 onCreateView 移至 onViewCreated,该问题可能会得到解决。

于 2014-11-25T06:11:14.850 回答
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);
        }

谢谢 !

于 2014-11-25T21:33:18.920 回答