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

class message in not building on parse

To make it work on android sdk with ActionbarActivity I splitted the Recipient class into two classes. I created log as every function to find out the problem and i think there may b some problem in send function in RecipientFragment but not able to solve it.

RecipentActivity.java
public class RecipientsActivity extends ActionBarActivity {

    protected  static final String TAG = RecipientsActivity.class.getSimpleName();
    protected static MenuItem mSendMenuItem;
    protected static Uri mMediaUri;
    protected static String mFileType;

    RecipientFragment list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recipients);
        mMediaUri = getIntent().getData();
        mFileType = getIntent().getExtras().getString(ParseConstants.KEY_FILE_TYPE);

    }
    @Override
    protected void onResume() {
        super.onResume();
        FragmentManager fm = getSupportFragmentManager();
        list = new RecipientFragment();
        fm.beginTransaction().add(android.R.id.content, list).commit();
    }

    @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 true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_send) {
            ParseObject message = list.createMessage();
            if(message==null){
                //error
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage(getString(R.string.error_selecting_file_error))
                        .setTitle(getString(R.string.error_selecting_file_title))
                        .setPositiveButton(android.R.string.ok, null);
                AlertDialog dialog = builder.create();
                dialog.show();
            }
            else {
                Log.i(TAG, "Sending message and Finist");
                list.send(message);
                finish();
            }
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
RecipientFragment.java
public class RecipientFragment extends ListFragment {
    public static final String TAG = RecipientsActivity.class.getSimpleName();

    protected List<ParseUser> mFriends;
    protected ParseRelation<ParseUser> mFriendsRelation;
    protected ParseUser mCurrentUser;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

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

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

                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>(
                            getListView().getContext(),
                            android.R.layout.simple_list_item_checked,
                            userNames);
                    setListAdapter(adapter);
                    getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
                } else {
                    Log.e(TAG, e.getMessage());
                    AlertDialog.Builder builder = new AlertDialog.Builder(getListView().getContext());
                    builder.setMessage(e.getMessage())
                            .setTitle(getString(R.string.query_error_title))
                            .setPositiveButton(android.R.string.ok, null);

                    AlertDialog dialog = builder.create();
                    dialog.show();
                }

            }
        });
    }

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

    protected ParseObject createMessage() {
        ParseObject message = new ParseObject(ParseConstants.CLASS_MESSAGES);
        message.put((ParseConstants.KEY_SENDER_IDs), 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,RecipientsActivity.mFileType);

        Log.i(TAG, "Creating Message");

        byte[] fileBytes = FileHelper.getByteArrayFromFile(getListView().getContext(), RecipientsActivity.mMediaUri);
        if(fileBytes==null){
            return null;
        }
        else{
            if(RecipientsActivity.mFileType.equals(ParseConstants.TYPE_IMAGE)){
                fileBytes = FileHelper.reduceImageForUpload(fileBytes);
            }
            String fileName = FileHelper.getFileName(getListView().getContext(), RecipientsActivity.mMediaUri,
                    RecipientsActivity.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());
            }
        }
        Log.i(TAG,"getting ReceipientID");

        return recipientIds;
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        if (l.getCheckedItemCount() > 0) {
            RecipientsActivity.mSendMenuItem.setVisible(true);
        } else {
            RecipientsActivity.mSendMenuItem.setVisible(false);
        }
    }
}

Hi,

What works and what doesn't? What functionalty are you expecting that you're not getting and do you receive an error or does it just not work?

Do you have a Github account? Perhaps push your application to that and we can try your code out.

Thanks,

Steve.

Thankx Steve Hunter for your reply. App is building the message and prepare it to send but after debugging I noticed that it build the message and prepare everything to send but at the time of sending it does not interacting with parse. It is not showing any kind of error.

I want to ask you one more thing Parse told us to add Parse.enableLocalDatastore(this); in our onCreate but it causing my app to crash so I removed it. Is absence of this code making my app not working?

Here is my app https://github.com/vipulgarg099/ConBre

Steve Hunter. I don't know but after adding Parse.enableLocalDatastore(this); and removed it later because it crashing my app it saving my data sometimes and still fails most of the times to store my pics to the back end