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
Vipul Garg
354 PointsToast is crashing my app
My app is crashing because of the Toast message i set in the send function in RecipientFragment. Plz help Here is my code
RecipientActivity
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
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
Log.e(TAG, "checking why toast is not working");
Toast.makeText(getListView().getContext(), getString(R.string.success_message), Toast.LENGTH_LONG).show(); //Toast is causing error thread exiting with uncaught exception (group=0x415cddb8)
}
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);
}
}
}