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 Retrieving and Viewing Messages Using Picasso to View Images from the Web

Maria Celeste Vallenilla
PLUS
Maria Celeste Vallenilla
Courses Plus Student 1,192 Points

Every time i try to open a photo, app crushes!

I don't know why every time i am in the app an di try to open a photo the app crushes. This doesn't happens to me with the videos. If any could help me or throw me some ideas i would really appreciate it. I have watched all the videos like 3 times trying to see if i skipped some part but i have the same line of codes as in the video. This is my InboxFragment.java : public class InboxFragment extends ListFragment {

protected List<ParseObject> mMessages;

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

    return rootView;
}

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

    getActivity().setProgressBarIndeterminateVisibility(true);

    ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(ParseConstants.CLASS_MESSAGES);
    query.whereEqualTo(ParseConstants.KEY_RECIPIENTS_IDS, ParseUser.getCurrentUser().getObjectId());
    query.addDescendingOrder(ParseConstants.KEY_CREATED_AT);
    query.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> messages, ParseException e) {
            getActivity().setProgressBarIndeterminateVisibility(false);

            if (e == null) {
                //We found messages!
                mMessages = messages;

                String[] usernames = new String[mMessages.size()];
                int i = 0;
                for (ParseObject message : mMessages) {
                    usernames[i] = message.getString(ParseConstants.KEY_SENDER_NAME);
                    i++;
                }
                MessageAdapter adapter = new MessageAdapter(
                        getListView().getContext(),
                        mMessages);
                setListAdapter(adapter);
            }

        }
    });
}

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

    ParseObject message = mMessages.get(position);
    String messageType = message.getString(ParseConstants.KEY_FILE_TYPE);
    ParseFile file = message.getParseFile(ParseConstants.KEY_FILE);
    Uri fileUri = Uri.parse(file.getUrl());

    if (messageType.equals(ParseConstants.TYPE_IMAGE)){
        //view the image
        Intent intent = new Intent(getActivity(), ViewImageActivity.class);
        intent.setData(fileUri);
        startActivity(intent);
    }
    else {
        //view the video
        Intent intent = new Intent(Intent.ACTION_VIEW, fileUri);
        intent.setDataAndType(fileUri, "video/*");
        startActivity(intent);
    }
}

}

and this is my ViewImageActivity.java : public class ViewImageActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_image);

    setupActionBar();

    ImageView imageView = (ImageView)findViewById(R.id.imageView);

    Uri imageUri = getIntent().getData();

    Picasso.with(this).load(imageUri.toString()).into(imageView);

}

private void setupActionBar() {
    getActionBar().setDisplayHomeAsUpEnabled(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.
    switch (item.getItemId()) {
        case android.R.id.home:

            NavUtils.navigateUpFromSameTask(this);
            return true;
    }

    return super.onOptionsItemSelected(item);
}

}

Please someone help me, im going crazy :'(.

2 Answers

Based the error message above, this seems to be your problem (and solution): "You need to use a Theme.AppCompat theme (or descendant) with this activity" I'm a bit out of date, but it looks component you're using relies on a particular theme when rendering images.

Try to change your application's theme to Theme.AppCompat.

Doing a quick search I found this in the documentation:

Note: If you are using the Support Library APIs for the action bar, then you must use (or override) the Theme.AppCompat family of styles (rather than the Theme.Holo family, available in API level 11 and higher). In doing so, each style property that you declare must be declared twice: once using the platform's style properties (the android: properties) and once using the style properties included in the Support Library (the appcompat.R.attr properties—the context for these properties is actually your app). See the examples below for details.

HTH, G

Maria Celeste Vallenilla
Maria Celeste Vallenilla
Courses Plus Student 1,192 Points

Yes thanks, once i send you the error i tried reading the error to find the error and i did change it and my app worked, so thank you a lot. And just one more question do you know how to make the action bar more bigger?? And also, the problem im having is that i can't use AppCompatTheme when talking about the ActionBar, it's like i am forced to use the Theme.Holo

I haven't used the Android SDK in a few years, so bare with me.

I would suggest first opening up the LogCat view in eclipse/ADT and having a look at the console as you crash the app. Post the error message here. Be sure to read it thoroughly and follow the lines of code listed in the error to spot the problematic lines in your code.

It's very hard to say what the issue otherwise (could be a null reference somewhere for some reason, a weird permissions issue, etc.)

Also, you should be able to set a break point at the start of the function that causes the crash and drill down to find the issue.

Maria Celeste Vallenilla
Maria Celeste Vallenilla
Courses Plus Student 1,192 Points

This is the error i am getting overtime i am trying to open a photo.

Process: com.example.nicurat.ribbit, PID: 22377 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nicurat.ribbit/com.example.nicurat.ribbit.ViewImageActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2658) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723) at android.app.ActivityThread.access$900(ActivityThread.java:172) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5832) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. at android.support.v7.app.AppCompatDelegateImplBase.onCreate(AppCompatDelegateImplBase.java:113) at android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.java:146) at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:59) at com.example.nicurat.ribbit.ViewImageActivity.onCreate(ViewImageActivity.java:20) at android.app.Activity.performCreate(Activity.java:6221) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2611) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723) at android.app.ActivityThread.access$900(ActivityThread.java:172) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5832) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)