NullPointerException - ParseImageView

时间:2014-08-23 14:41:53

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

尝试从Parse中检索信息时,如字符串,数字和图像,我收到以下错误: 特别是,我从ParseImageView接收错误,其中字符串和数字被很好地检索。 logcat消息

08-23 13:25:28.269: E/AndroidRuntime(1377): FATAL EXCEPTION: main
08-23 13:25:28.269: E/AndroidRuntime(1377): Process: com.dooba.beta, PID: 1377
08-23 13:25:28.269: E/AndroidRuntime(1377): java.lang.NullPointerException
08-23 13:25:28.269: E/AndroidRuntime(1377):     at com.dooba.beta.Fragment1$1$1.done(Fragment1.java:184)
08-23 13:25:28.269: E/AndroidRuntime(1377):     at com.parse.ParseImageView$1.done(ParseImageView.java:157)
08-23 13:25:28.269: E/AndroidRuntime(1377):     at com.parse.GetDataCallback.internalDone(GetDataCallback.java:20)
08-23 13:25:28.269: E/AndroidRuntime(1377):     at com.parse.GetDataCallback.internalDone(GetDataCallback.java:1)
08-23 13:25:28.269: E/AndroidRuntime(1377):     at com.parse.Parse$6$1.run(Parse.java:888)
08-23 13:25:28.269: E/AndroidRuntime(1377):     at android.os.Handler.handleCallback(Handler.java:733)
08-23 13:25:28.269: E/AndroidRuntime(1377):     at android.os.Handler.dispatchMessage(Handler.java:95)
08-23 13:25:28.269: E/AndroidRuntime(1377):     at android.os.Looper.loop(Looper.java:136)

XML布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bac_blue"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:layout_below="@+id/userlistactivityname"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="28dp"
        android:alpha="0.7"
        android:background="#ADD8E6"
        android:text="Confirm"
        android:textColor="#000000"
        android:textSize="22sp"
        android:typeface="serif" />

    <ListView
        android:id="@+id/userlistage"
        android:layout_width="220dp"
        android:layout_height="50dp"
        android:layout_below="@+id/profilePictureresult"
        android:layout_marginTop="19dp"
        android:layout_toLeftOf="@+id/imageView2"
        android:divider="@null" >
    </ListView>

    <com.parse.ParseImageView
        android:id="@+id/profilePictureresult"
        android:layout_width="132dp"
        android:layout_height="120dp"
        android:layout_below="@+id/userlistheadline"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="17dp"
        android:alpha="1"
        android:background="@drawable/border_image"
        android:cropToPadding="true"
        android:padding="3dp"
        android:scaleType="centerCrop" />

    <ListView
        android:id="@+id/userlistname"
        android:layout_width="220dp"
        android:layout_height="50dp"
        android:layout_alignParentTop="true"
        android:layout_marginTop="25dp"
        android:layout_toRightOf="@+id/imageView1"
        android:divider="@null" >
    </ListView>

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="70dp"
        android:layout_height="50dp"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/profilePictureresult"
        android:layout_marginTop="16dp"
        android:alpha="0.7"
        android:src="@drawable/left_right" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="70dp"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignTop="@+id/imageView2"
        android:alpha="0.7"
        android:src="@drawable/left_arrow2" />

    <ListView
        android:id="@+id/userlistheadline"
        android:layout_width="220dp"
        android:layout_height="50dp"
        android:layout_below="@+id/userlistname"
        android:layout_marginTop="17dp"
        android:layout_toRightOf="@+id/imageView1"
        android:divider="@null" >
    </ListView>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/userlistage"
        android:layout_centerHorizontal="true"
        android:text="Activity Name"
        android:textColor="#a2a2a2"
        android:textSize="22sp" />

    <ListView
        android:id="@+id/userlistactivityname"
        android:layout_width="220dp"
        android:layout_height="50dp"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="7dp"
        android:layout_toLeftOf="@+id/imageView2"
        android:divider="@null" >
    </ListView>

</RelativeLayout>

活动代码

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;

    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("Age");

    @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);

        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("Age", minimumAge);
        // query.whereLessThanOrEqualTo("Age", 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("Age", minimumAge);
        // query.whereLessThanOrEqualTo("Age", maximumAge);
        query1.orderByDescending("Name");

        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("Age", minimumAge);
        // query.whereLessThanOrEqualTo("Age", maximumAge);
        query11.orderByDescending("Name");

        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("Age", minimumAge);
                    // query.whereLessThanOrEqualTo("Age", 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) {
                         Log.i("ParseImageView",
                             "Fetched! Data length: " + data.length + ", or exception: " + e.getMessage());
                       }


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


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

            public void done(List<ParseUser> userlistname, ParseException e) {
                if (e == null) {
                    for (int i = 0; i < userlistname.size(); i++) {
                        names.add(userlistname.get(i).get("Name").toString());
                    }
                    usersListView = (ListView) getActivity().findViewById(
                            R.id.userlistname);
                    namesArrayAdapter = new ArrayAdapter<String>(getActivity()
                            .getApplicationContext(), R.layout.user_list_item,
                            names);
                    usersListView.setAdapter(namesArrayAdapter);

                    usersListView
                            .setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> a,
                                                        View v, int i, long l) {
                                    openConversation(names, i);
                                }
                            });

                } else {
                    Toast.makeText(getActivity().getApplicationContext(),
                            "Error loading user list", Toast.LENGTH_LONG)
                            .show();
                }
                }
            });


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

            public void done(List<ParseUser> userlistheadline, ParseException e) {
                if (e == null) {
                    for (int i = 0; i < userlistheadline.size(); i++) {
                        headline.add(userlistheadline.get(i).get("Headline")
                                .toString());
                    }
                    usersListView = (ListView) getActivity().findViewById(
                            R.id.userlistheadline);
                    namesArrayAdapter = new ArrayAdapter<String>(getActivity()
                            .getApplicationContext(), R.layout.user_list_all,
                            headline);
                    usersListView.setAdapter(namesArrayAdapter);

                    usersListView
                            .setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> a,
                                                        View v, int i, long l) {
                                    openConversation(names, i);
                                }
                            });

                } else {
                    Toast.makeText(getActivity().getApplicationContext(),
                            "Error loading user list", Toast.LENGTH_LONG)
                            .show();
                }
            }
        });

    }

    public void openConversation(ArrayList<String> names, int pos) {
        ParseQuery<ParseUser> query = ParseUser.getQuery();
        query.whereEqualTo("Name", names.get(pos));
        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("RECIPIENT_ID", user.get(0).getObjectId());
                    startActivity(intent);
                } else {
                    Toast.makeText(getActivity().getApplicationContext(),
                            "Error finding that user", Toast.LENGTH_SHORT)
                            .show();
                }
            }
        });
    }

}

提前致谢

更新

08-23 11:42:32.291: E/AndroidRuntime(1283): FATAL EXCEPTION: main
08-23 11:42:32.291: E/AndroidRuntime(1283): Process: com.dooba.beta, PID: 1283
08-23 11:42:32.291: E/AndroidRuntime(1283): java.lang.ClassCastException: android.widget.ImageView cannot be cast to com.parse.ParseImageView
08-23 11:42:32.291: E/AndroidRuntime(1283):     at com.dooba.beta.Fragment1$1.done(Fragment1.java:174)
08-23 11:42:32.291: E/AndroidRuntime(1283):     at com.parse.FindCallback.internalDone(FindCallback.java:45)
08-23 11:42:32.291: E/AndroidRuntime(1283):     at com.parse.FindCallback.internalDone(FindCallback.java:1)
08-23 11:42:32.291: E/AndroidRuntime(1283):     at com.parse.Parse$6$1.run(Parse.java:888)
08-23 11:42:32.291: E/AndroidRuntime(1283):     at android.os.Handler.handleCallback(Handler.java:733)
08-23 11:42:32.291: E/AndroidRuntime(1283):     at android.os.Handler.dispatchMessage(Handler.java:95)
08-23 11:42:32.291: E/AndroidRuntime(1283):     at android.os.Looper.loop(Looper.java:136)
08-23 11:42:32.291: E/AndroidRuntime(1283):     at android.app.ActivityThread.main(ActivityThread.java:5017)
08-23 11:42:32.291: E/AndroidRuntime(1283):     at java.lang.reflect.Method.invokeNative(Native Method)
08-23 11:42:32.291: E/AndroidRuntime(1283):     at java.lang.reflect.Method.invoke(Method.java:515)
08-23 11:42:32.291: E/AndroidRuntime(1283):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-23 11:42:32.291: E/AndroidRuntime(1283):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-23 11:42:32.291: E/AndroidRuntime(1283):     at dalvik.system.NativeStart.main(Native Method)
08-23 13:25:28.269: E/AndroidRuntime(1377): FATAL EXCEPTION: main
08-23 13:25:28.269: E/AndroidRuntime(1377): Process: com.dooba.beta, PID: 1377
08-23 13:25:28.269: E/AndroidRuntime(1377): java.lang.NullPointerException
08-23 13:25:28.269: E/AndroidRuntime(1377):     at com.dooba.beta.Fragment1$1$1.done(Fragment1.java:184)
08-23 13:25:28.269: E/AndroidRuntime(1377):     at com.parse.ParseImageView$1.done(ParseImageView.java:157)
08-23 13:25:28.269: E/AndroidRuntime(1377):     at com.parse.GetDataCallback.internalDone(GetDataCallback.java:20)
08-23 13:25:28.269: E/AndroidRuntime(1377):     at com.parse.GetDataCallback.internalDone(GetDataCallback.java:1)
08-23 13:25:28.269: E/AndroidRuntime(1377):     at com.parse.Parse$6$1.run(Parse.java:888)
08-23 13:25:28.269: E/AndroidRuntime(1377):     at android.os.Handler.handleCallback(Handler.java:733)
08-23 13:25:28.269: E/AndroidRuntime(1377):     at android.os.Handler.dispatchMessage(Handler.java:95)
08-23 13:25:28.269: E/AndroidRuntime(1377):     at android.os.Looper.loop(Looper.java:136)

1 个答案:

答案 0 :(得分:1)

异常位于Fragment1.java类的第184行,可能就是那行:

Log.i("ParseImageView", "Fetched! Data length: " + data.length + ", or exception: " + e.getMessage());

这将始终抛出NullPointerException: 如果图片正确加载,data将是有效byte[]e将是null。因此e.getMessage()会抛出NullPointerException 如果图片未正确加载,则data将为null,而e将为有效Exception,因此data.length会投放NullPointerException

你可能想要做的是:

if (data != null) {
  Log.i("ParseImageView", "Fetched: " + data.length);
} else {
  Log.e("ParseImageView", "Error fetching: " + e.getMessage());
}