Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Self-Destructing Message Android App Sending Messages Sending the Message

ahmet yuva
ahmet yuva
17,595 Points

i send image or video but when i check at parse.com in my recipientIds arrray seems like null how is that possible ?

why i get null even i choose recipients

package com.ahmetyuva.ribbit;

import android.app.AlertDialog; import android.app.ListActivity; import android.net.Uri; import android.support.v4.app.NavUtils; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListAdapter; import android.widget.ListView;

import com.parse.FindCallback; import com.parse.ParseException; import com.parse.ParseFile; import com.parse.ParseObject; import com.parse.ParseQuery; import com.parse.ParseRelation; import com.parse.ParseUser;

import java.util.ArrayList; import java.util.List;

import android.app.AlertDialog; import android.app.ListActivity; import android.support.v4.app.NavUtils; import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.Window; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast;

import com.parse.FindCallback; import com.parse.ParseException; import com.parse.ParseQuery; import com.parse.ParseRelation; import com.parse.ParseUser; import com.parse.SaveCallback;

import java.util.List;

public class RecipientsActivity extends ActionBarActivity{

public static final String TAG = RecipientsActivity.class.getSimpleName();

protected List<ParseUser> mFriends;
protected ParseRelation<ParseUser> mFriendsRelation;
protected ParseUser mCurrentUser;
protected MenuItem mSendMenuItem;
protected ListView recipientsListView;

protected Uri mMediaUri;
protected String mFileType;


// burasını silebilirim sora
private ListView mListView;

protected ListView getListView() {
    if (mListView == null) {
        mListView = (ListView) findViewById(android.R.id.list);
    }
    return mListView;
}

protected void setListAdapter(ListAdapter adapter) {
    getListView().setAdapter(adapter);
}

//buraya kadarını tabe


@Override
protected void onCreate(Bundle savedInstanceState) {
    //requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recipients);



    mMediaUri = getIntent().getData();
    mFileType = getIntent().getExtras().getString(ParseConstants.KEY_FILE_TYPE);


    recipientsListView = (ListView) findViewById(R.id.recipientListView);
    recipientsListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    recipientsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (recipientsListView.getCheckedItemCount() > 0){
                mSendMenuItem.setVisible(true);
            } else {
                mSendMenuItem.setVisible(false);
            }
        }
    });
}

public void onResume() {
    super.onResume();
    mCurrentUser = ParseUser.getCurrentUser();
    mFriendsRelation = mCurrentUser.getRelation(ParseConstants.KEY_FRIENDS_RELATION);

    //setProgressBarIndeterminateVisibility(true);

    ParseQuery<ParseUser> query = mFriendsRelation.getQuery();
    query.addAscendingOrder(ParseConstants.KEY_USERNAME);
    query.findInBackground(new FindCallback<ParseUser>() {
        @Override
        public void done(List<ParseUser> friends, ParseException e) {
            // setProgressBarIndeterminateVisibility(false);

            if (e == null) {
                mFriends = friends;

                String[] usernames = new String[mFriends.size()];
                int i = 0;
                for (ParseUser user : mFriends) {
                    usernames[i] = user.getUsername();
                    i++;
                }
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(recipientsListView.getContext(),
                        android.R.layout.simple_list_item_checked,
                        usernames);
                recipientsListView.setAdapter(adapter);
            } else {
                Log.e(TAG, e.getMessage());
                AlertDialog.Builder builder = new AlertDialog.Builder(RecipientsActivity.this);
                builder.setMessage(e.getMessage())
                        .setTitle(R.string.error_title)
                        .setPositiveButton(android.R.string.ok, null);
                AlertDialog dialog = builder.create();
                dialog.show();
            }
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_recipients, menu);
    mSendMenuItem = menu.getItem(0);
    return (super.onCreateOptionsMenu(menu));
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case android.R.id.home:

            NavUtils.navigateUpFromSameTask(this);
           return true;
        case R.id.action_send:
            ParseObject message = createMessage();
            if(message == null){
                //error
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage(R.string.error_selecting_file)
                        .setTitle(R.string.error_selecting_file_title)
                        .setPositiveButton(android.R.string.ok, null);
                        AlertDialog dialog = builder.create();
                        dialog.show();
            }
            else{
                send(message);
                finish();
            }


            return true;
    }
    return super.onOptionsItemSelected(item);
}

protected ParseObject createMessage(){
    ParseObject message = new ParseObject(ParseConstants.CLASS_MESSAGES);
    message.put(ParseConstants.KEY_SENDER_ID, ParseUser.getCurrentUser().getObjectId());
    message.put(ParseConstants.KEY_SENDER_NAME, ParseUser.getCurrentUser().getUsername());
    message.put(ParseConstants.KEY_RECIPIENT_IDS, getRecipientIds());
    message.put(ParseConstants.KEY_FILE_TYPE, mFileType);

    byte[] fileBytes = FileHelper.getByteArrayFromFile(this, mMediaUri);

    if(fileBytes == null){
        return null;

    }
    else{
        if(mFileType.equals(ParseConstants.TYPE_IMAGE)){
            fileBytes = FileHelper.reduceImageForUpload(fileBytes);

        }

        String fileName = FileHelper.getFileName(this, mMediaUri, mFileType);
        ParseFile file = new ParseFile(fileName, fileBytes);
        message.put(ParseConstants.KEY_FILE,file);

        return message;
    }

}

protected ArrayList<String> getRecipientIds(){
    ArrayList<String> recipientIds = new ArrayList<String>();
    for(int i = 0; i < getListView().getCount(); i++){
        if(getListView().isItemChecked(i)){
            recipientIds.add(mFriends.get(i).getObjectId());
        }
    }
    return recipientIds;

}


protected void send(ParseObject message){
    message.saveInBackground(new SaveCallback() {
        @Override
        public void done(ParseException e) {
            if(e == null){
                // success!
                Toast.makeText(RecipientsActivity.this, R.string.success_message, Toast.LENGTH_LONG);
            }
            else{
                AlertDialog.Builder builder = new AlertDialog.Builder(RecipientsActivity.this);
                builder.setMessage(R.string.error_sending_message)
                        .setTitle(R.string.error_selecting_file_title)
                        .setPositiveButton(android.R.string.ok, null);
                AlertDialog dialog = builder.create();
                dialog.show();
            }

        }
    });
}

}

ahmet yuva
ahmet yuva
17,595 Points

and my ParseConstants class is here package com.ahmetyuva.ribbit;

/**

  • Created by ahmetyuva on 07/04/15. */ public final class ParseConstants { // Class name public static final String CLASS_MESSAGES = "Messages";

    // Field names public static final String KEY_USERNAME = "username"; public static final String KEY_FRIENDS_RELATION = "friendsRelation"; public static final String KEY_RECIPIENT_IDS = "recipientIds"; public static final String KEY_SENDER_ID = "senderId"; public static final String KEY_SENDER_NAME = "senderName"; public static final String KEY_FILE= "file"; public static final String KEY_FILE_TYPE = "fileType";

public static final String TYPE_IMAGE = "image"; public static final String TYPE_VIDEO = "video";

}

1 Answer

So my first thought is that parse does not have the device registered on their site. Parse has an installation object and I believe it will not interact with any device that has not been registered.

@Override
    public void onCreate() {
        super.onCreate();

        Parse.enableLocalDatastore(this);
        Parse.initialize(this, getString(R.string.parse_application_id), getString(R.string.parse_client_key));
        ParseInstallation.getCurrentInstallation().saveInBackground();
    }

public static void UpdateParseInstallation(ParseUser user) {
        ParseInstallation installation = ParseInstallation.getCurrentInstallation();
        installation.put(Constants.PARSE_KEY_USER_ID, user.getObjectId());
        installation.saveInBackground();
    }

This UpdateParseInstallation gets called in my LoginActivity each time a user logs in. That Parse.SaveInBackground is what will save device information to that object.

ahmet yuva
ahmet yuva
17,595 Points

I have other objects, it gets sender id, image everything except recipientIds, its registered device indeed

in this block:

protected ArrayList<String> getRecipientIds(){
    ArrayList<String> recipientIds = new ArrayList<String>();
    for(int i = 0; i < getListView().getCount(); i++){
        if(getListView().isItemChecked(i)){
            recipientIds.add(mFriends.get(i).getObjectId());
        }
    }
    return recipientIds;

}

If you put a break point on the for loop line, can you see what the value is for getListView().getCount()?