Nullpointer异常 - 片段中的意外错误

时间:2014-09-01 22:55:03

标签: java android android-activity android-fragments parse-platform

尝试加载应用程序时,我收到了意外的nullpoint异常错误。我不确定出了什么问题,并且花了一段时间试图解决这个问题。 我在logcat消息中包含了以下内容:

09-01 22:26:44.311: E/AndroidRuntime(5805):     at com.dooba.beta.Fragment1$3.done(Fragment1.java:280)
09-01 22:44:46.383: E/AndroidRuntime(6035): java.lang.NullPointerException
09-01 22:44:46.383: E/AndroidRuntime(6035):     at com.dooba.beta.Fragment1$3.done(Fragment1.java:281)
09-01 22:44:46.383: E/AndroidRuntime(6035):     at com.parse.FindCallback.internalDone(FindCallback.java:45)
09-01 22:44:46.383: E/AndroidRuntime(6035):     at com.parse.FindCallback.internalDone(FindCallback.java:31)
09-01 22:44:46.383: E/AndroidRuntime(6035):     at com.parse.Parse$5$1.run(Parse.java:891)
09-01 22:44:46.383: E/AndroidRuntime(6035):     at android.os.Handler.handleCallback(Handler.java:733)
09-01 22:44:46.383: E/AndroidRuntime(6035):     at android.os.Handler.dispatchMessage(Handler.java:95)
09-01 22:44:46.383: E/AndroidRuntime(6035):     at android.os.Looper.loop(Looper.java:136)
09-01 22:44:46.383: E/AndroidRuntime(6035):     at android.app.ActivityThread.main(ActivityThread.java:5017)
09-01 22:44:46.383: E/AndroidRuntime(6035):     at java.lang.reflect.Method.invokeNative(Native Method)
09-01 22:44:46.383: E/AndroidRuntime(6035):     at java.lang.reflect.Method.invoke(Method.java:515)
09-01 22:44:46.383: E/AndroidRuntime(6035):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-01 22:44:46.383: E/AndroidRuntime(6035):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-01 22:44:46.383: E/AndroidRuntime(6035):     at dalvik.system.NativeStart.main(Native Method)

下面是活动代码

public class Fragment1 extends Fragment {

    public interface Constants {
        String LOG = "com.dooba.beta";
    }

    private String currentUserId;
    private ArrayAdapter<String> namesArrayAdapter;
    private ArrayList<String> names;
    private ArrayList<String> age;
    private ArrayList<String> headline;
    private ArrayList<String> activityname;

    protected TextView mUserRetrieved;
    protected TextView mUserActivityNameRetrieved;
    protected TextView mUserNameRetrieved;
    protected TextView mUserAgeRetrieved;
    protected TextView mUserHeadlineRetrieved;



    private ArrayList<Images> alProfilePicture;
    private ListView usersListView;
    private Button logoutButton;
    String userGender = ParseUser.getCurrentUser().getString("Gender");
    String activityName = ParseUser.getCurrentUser().getString("ActivityName");
    Number maxDistance = ParseUser.getCurrentUser().getNumber(
            "Maximum_Distance");
    String userLookingGender = ParseUser.getCurrentUser().getString(
            "Looking_Gender");
    Number minimumAge = ParseUser.getCurrentUser().getNumber("Minimum_Age");
    Number maximumAge = ParseUser.getCurrentUser().getNumber("Maximum_Age");
    Number userage = ParseUser.getCurrentUser().getNumber("UserAge");

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        setConversationsList();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment1_layout, container,
                false);

        //mUserRetrieved = (TextView) getActivity().findViewById(R.id.userlistname);


        Button newPage = (Button)view.findViewById(R.id.btnMatchConfirm);
        newPage.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
            //    ParseUser currentUser = ParseUser.getCurrentUser();

          //      currentUser.put("UserMatchName", mUserRetrieved);
                Intent intent = new Intent(getActivity(), matchOptionActivity.class);
                startActivity(intent);
            }
        });



        return view;
    }

    private void setConversationsList() {
        currentUserId = ParseUser.getCurrentUser().getObjectId();
        names = new ArrayList<String>();
        age = new ArrayList<String>();
        headline = new ArrayList<String>();
        activityname = new ArrayList<String>();

        alProfilePicture = new ArrayList<Images>();

        // String userActivitySelectionName = null;

        ParseQuery<ParseUser> query = ParseUser.getQuery();

        // query.whereEqualTo("ActivityName",userActivitySelectionName);

        query.whereNotEqualTo("objectId", ParseUser.getCurrentUser()
                .getObjectId());
        // users with Gender = currentUser.Looking_Gender
        query.whereEqualTo("Gender", userLookingGender);
        // users with Looking_Gender = currentUser.Gender
        query.whereEqualTo("Looking_Gender", userGender);
        query.setLimit(1);
        query.whereEqualTo("ActivityName", activityName);
        query.whereGreaterThanOrEqualTo("UserAge", minimumAge);
        query.whereLessThanOrEqualTo("UserAge", maximumAge);
        query.orderByDescending("Name");


        ParseQuery<ParseUser> query1 = ParseUser.getQuery();

        // query.whereEqualTo("ActivityName",userActivitySelectionName);

        query1.whereNotEqualTo("objectId", ParseUser.getCurrentUser()
                .getObjectId());
        // users with Gender = currentUser.Looking_Gender
        query1.whereEqualTo("Gender", userLookingGender);
        // users with Looking_Gender = currentUser.Gender
        query1.whereEqualTo("Looking_Gender", userGender);
        query1.setLimit(1);
        query1.whereEqualTo("ActivityName", activityName);
        query.whereGreaterThanOrEqualTo("UserAge", minimumAge);
        query.whereLessThanOrEqualTo("UserAge", maximumAge);

        ParseQuery<ParseUser> query11 = ParseUser.getQuery();

        //query.whereEqualTo("ActivityName",userActivitySelectionName);

        query11.whereNotEqualTo("objectId", ParseUser.getCurrentUser()
                .getObjectId());
        // users with Gender = currentUser.Looking_Gender
        query11.whereEqualTo("Gender", userLookingGender);
        // users with Looking_Gender = currentUser.Gender
        query11.whereEqualTo("Looking_Gender", userGender);
        query11.setLimit(1);
        query11.whereEqualTo("ActivityName", activityName);
        query.whereGreaterThanOrEqualTo("UserAge", minimumAge);
        query.whereLessThanOrEqualTo("UserAge", maximumAge);

        ParseQuery<ParseUser> query4 = ParseUser.getQuery();

        // query.whereEqualTo("ActivityName",userActivitySelectionName);

        query4.whereNotEqualTo("objectId", ParseUser.getCurrentUser()
                .getObjectId());
        // users with Gender = currentUser.Looking_Gender
        query4.whereEqualTo("Gender", userLookingGender);
        // users with Looking_Gender = currentUser.Gender
        query4.whereEqualTo("Looking_Gender", userGender);
        query4.setLimit(1);
        query4.whereEqualTo("ActivityName", activityName);
        query.whereGreaterThanOrEqualTo("UserAge", minimumAge);
        query.whereLessThanOrEqualTo("UserAge", maximumAge);

        ParseQuery<ParseUser> query5 = ParseUser.getQuery();

        // query.whereEqualTo("ActivityName",userActivitySelectionName);

        query5.whereNotEqualTo("objectId", ParseUser.getCurrentUser()
                .getObjectId());
        // users with Gender = currentUser.Looking_Gender
        query5.whereEqualTo("Gender", userLookingGender);
        // users with Looking_Gender = currentUser.Gender
        query5.whereEqualTo("Looking_Gender", userGender);
        query5.setLimit(1);
        query5.whereEqualTo("ActivityName", activityName);
       query.whereGreaterThanOrEqualTo("UserAge", minimumAge);
       query.whereLessThanOrEqualTo("UserAge", maximumAge);


      //  whereWithinKilometers("Maximum_distance", point, double maxDistance);




        Log.d(Constants.LOG, "");




        query.findInBackground(new FindCallback<ParseUser>() {

            @Override
            public void done(List<ParseUser> objects,ParseException e) {

                for(int i=0;i<objects.size();i++){
                    // Do whatever you need to extract object from "users"
                    ParseQuery<ParseObject> query = ParseQuery.getQuery("User");
                    query.whereNotEqualTo("objectId", ParseUser.getCurrentUser()
                            .getObjectId());
                    // users with Gender = currentUser.Looking_Gender
                    query.whereEqualTo("Gender", userLookingGender);
                    // users with Looking_Gender = currentUser.Gender
                    query.whereEqualTo("Looking_Gender", userGender);
                    query.setLimit(1);
                    query.whereEqualTo("ActivityName", activityName);
                    query.whereGreaterThanOrEqualTo("UserAge", minimumAge);
                    query.whereLessThanOrEqualTo("UserAge", maximumAge);
                    query.orderByDescending("Name");

                    ParseFile image = objects.get(i).getParseFile("ProfilePicture");


                    ParseImageView imageView = (ParseImageView) getView().findViewById(R.id.profilePictureresult);

                     // The placeholder will be used before and during the fetch, to be replaced by the fetched image
                     // data.
                     imageView.setPlaceholder(getResources().getDrawable(R.drawable.profile_pict));
                     imageView.setParseFile(image);
                     imageView.loadInBackground(new GetDataCallback() {
                       @Override
                       public void done(byte[] data, ParseException e) {
                           if (data != null) {
                               Log.i("ParseImageView", "Fetched: " + data.length);
                             } else {
                               Log.e("ParseImageView", "Error fetching: " + e.getMessage());
                             }
                       }


                 });
                }
    }//for loop
            });


        query1.findInBackground(new FindCallback<ParseUser>() {
            @Override
            public void done(List<ParseUser> objects,ParseException e) {

                for(int i=0;i<objects.size();i++){
                    // Do whatever you need to extract object from "users"
                    ParseQuery<ParseObject> query = ParseQuery.getQuery("User");
                    query.whereNotEqualTo("objectId", ParseUser.getCurrentUser()
                            .getObjectId());
                    // users with Gender = currentUser.Looking_Gender
                    query.whereEqualTo("Gender", userLookingGender);
                    // users with Looking_Gender = currentUser.Gender
                    query.whereEqualTo("Looking_Gender", userGender);
                    query.setLimit(1);
                    query.whereEqualTo("ActivityName", activityName);
                    query.whereGreaterThanOrEqualTo("UserAge", minimumAge);
                    query.whereLessThanOrEqualTo("UserAge", maximumAge);

                    mUserNameRetrieved = (TextView) getActivity().findViewById(R.id.userlistname);

                    mUserNameRetrieved.setText(objects.get(i).get("Name").toString()); 



                }
    }//for loop
            });

        query11.findInBackground(new FindCallback<ParseUser>() {
            @Override
            public void done(List<ParseUser> objects,ParseException e) {

                for(int i=0;i<objects.size();i++){
                    // Do whatever you need to extract object from "users"
                    ParseQuery<ParseObject> query = ParseQuery.getQuery("User");
                    query.whereNotEqualTo("objectId", ParseUser.getCurrentUser()
                            .getObjectId());
                    // users with Gender = currentUser.Looking_Gender
                    query.whereEqualTo("Gender", userLookingGender);
                    // users with Looking_Gender = currentUser.Gender
                    query.whereEqualTo("Looking_Gender", userGender);
                    query.setLimit(1);
                    query.whereEqualTo("ActivityName", activityName);
                    query.whereGreaterThanOrEqualTo("UserAge", minimumAge);
                    query.whereLessThanOrEqualTo("UserAge", maximumAge);

                    mUserHeadlineRetrieved = (TextView) getActivity().findViewById(R.id.userlistheadline);

                    mUserHeadlineRetrieved.setText(objects.get(i).get("Headline").toString()); 



                }
    }//for loop
            });




            query4.findInBackground(new FindCallback<ParseUser>() {

                @Override
                public void done(List<ParseUser> objects,ParseException e) {

                    for(int i=0;i<objects.size();i++){
                        // Do whatever you need to extract object from "users"
                        ParseQuery<ParseObject> query = ParseQuery.getQuery("User");
                        query.whereNotEqualTo("objectId", ParseUser.getCurrentUser()
                                .getObjectId());
                        // users with Gender = currentUser.Looking_Gender
                        query.whereEqualTo("Gender", userLookingGender);
                        // users with Looking_Gender = currentUser.Gender
                        query.whereEqualTo("Looking_Gender", userGender);
                        query.setLimit(1);
                        query.whereEqualTo("ActivityName", activityName);
                        query.whereGreaterThanOrEqualTo("UserAge", minimumAge);
                        query.whereLessThanOrEqualTo("UserAge", maximumAge);

                        mUserActivityNameRetrieved = (TextView) getActivity().findViewById(R.id.userlistactivityname);

                        mUserActivityNameRetrieved.setText(objects.get(i).get("ActivityName").toString()); 



                    }
        }//for loop
                });

            query5.findInBackground(new FindCallback<ParseUser>() {

                @Override
                public void done(List<ParseUser> objects,ParseException e) {

                    for(int i=0;i<objects.size();i++){
                        // Do whatever you need to extract object from "users"
                        ParseQuery<ParseObject> query = ParseQuery.getQuery("User");
                        query.whereNotEqualTo("objectId", ParseUser.getCurrentUser()
                                .getObjectId());
                        // users with Gender = currentUser.Looking_Gender
                        query.whereEqualTo("Gender", userLookingGender);
                        // users with Looking_Gender = currentUser.Gender
                        query.whereEqualTo("Looking_Gender", userGender);
                        query.setLimit(1);
                        query.whereEqualTo("ActivityName", activityName);
                        query.whereGreaterThanOrEqualTo("UserAge", minimumAge);
                        query.whereLessThanOrEqualTo("UserAge", maximumAge);

                        mUserAgeRetrieved = (TextView) getActivity().findViewById(R.id.userlistage);

                        mUserAgeRetrieved.setText(objects.get(i).get("Age").toString()); 



                    }
        }//for loop
                });


    }

    public void openConversation(ArrayList<String> names, int pos) {
        ParseQuery<ParseUser> query = ParseUser.getQuery();
        query.whereNotEqualTo("objectId", ParseUser.getCurrentUser()
                .getObjectId());
        // users with Gender = currentUser.Looking_Gender
        query.whereEqualTo("Gender", userLookingGender);
        // users with Looking_Gender = currentUser.Gender
        query.whereEqualTo("Looking_Gender", userGender);
        query.setLimit(1);
        query.whereEqualTo("ActivityName", activityName);
        query.whereGreaterThanOrEqualTo("UserAge", minimumAge);
        query.whereLessThanOrEqualTo("UserAge", maximumAge);
        query.findInBackground(new FindCallback<ParseUser>() {
            public void done(List<ParseUser> user, ParseException e) {
                if (e == null) {
                    Intent intent = new Intent(getActivity()
                            .getApplicationContext(), MessagingActivity.class);
                    intent.putExtra("Name", user.get(0).getObjectId());
                    startActivity(intent);
                } else {
                    Toast.makeText(getActivity().getApplicationContext(),
                            "Error finding that user", Toast.LENGTH_SHORT)
                            .show();
                }
            }
        });
    }


}

提前致谢

1 个答案:

答案 0 :(得分:0)

此行中NullPointerException(一个未初始化的对象)可能有很多原因:

mUserNameRetrieved.setText(objects.get(i).get("Name").toString()); 

我建议您添加以下支票:

ParseObject object = objects.get(i);
if (object != null) {
    String name = object.get("Name");
    mUserNameRetrieved.setText(name);
}

无论如何,您正在尝试将“Name”的值设置为循环内的mUserNameRetrieved。这将只显示姓氏。如果你正在调试代码,你应该使用类似的东西:

Log.d("Name value", name);

我希望能帮到你!