Stumped by Intents


Last Updated:

  1. wige

    wige Well-Known Member This Topic's Starter

    Joined:
    Oct 28, 2009
    Messages:
    46
    Likes Received:
    0
    I am sorry, this is probably a very basic question, but my first Android program keeps getting hung when trying to switch to another activity. I have gone through several tutorials and the documentation, and I am starting to think that I am either leaving something basic out, or I am mixing methods too much. Hopefully someone can spot the problem, and show me where I am messing up.

    So far, the app consists of two classes, Index and Statistics. Index is the default class, and gives the user a list of options. Statistics goes to the web, downloads an XML file of data, and displays it to the user. Both extend Activity and use onCreate and appear to work on their own.

    In the manifest, I have defined a custom action for the Statistics activity:
    Code (Text):
    1.  
    2. <activity
    3.     android:name="Statistics"
    4.     android:label="@string/stats_title">
    5.     <intent-filter>
    6.         <action android:name="com.package.project.action.SHOW_STATISTICS" />
    7.     </intent-filter>
    8. </activity>
    9.  
    In the index class, I have a method that receives a button click and tries to start the Activity by creating a new Intent:
    Code (Text):
    1.  
    2. if (R.id.statsButton == v.getId()) {
    3.     // The user wants to view the statistics, create an Intent
    4.     try {
    5.         startActivity(new Intent("com.package.project.action.SHOW_STATISTICS"));
    6.     } catch (ActivityNotFoundException e) {
    7.         showDialog(DIALOG_TESTING);
    8.     }
    9. }
    10.  
    When this code runs, the test dialog is displayed, indicating an ActivityNotFoundException occurred. Is there somewhere else that com.package.project.activity.SHOW_STATISTICS needs to be declared for example?

    Any thoughts?

    Thanks in advance for reading, and any suggestions/guidance you can offer.
     

    Advertisement
  2. regulatre

    regulatre Well-Known Member

    Joined:
    Nov 14, 2009
    Messages:
    46
    Likes Received:
    1
    Did you remember to define your activity in the manifest?
    The first one is added for you automatically

    Code (Text):
    1. They go within the <activity></activity> tags.
     
  3. DaveInNYC

    DaveInNYC Well-Known Member

    Joined:
    Nov 7, 2009
    Messages:
    52
    Likes Received:
    3
    Try adding the following to your intent filter:
    <category android:name="android.intent.category.DEFAULT" />

    Alternatively, you could use Intent.setComponent or Intent.setClass to explicitly say what class/component you want to handle the intent.
     
  4. wige

    wige Well-Known Member This Topic's Starter

    Joined:
    Oct 28, 2009
    Messages:
    46
    Likes Received:
    0
    regulatre, yes, I added it to the manifest using the code in the first snippet. Does the syntax look correct?

    Dave, thanks, I will try adding the category. I ended up using the setClass method, since I think that is probably more appropriate in this instance, but as the application becomes more complex, being able to use an implicit Intent will probably become necessary, so I hope to be able to get it working. I will try the category and post back with the results.
     
  5. regulatre

    regulatre Well-Known Member

    Joined:
    Nov 14, 2009
    Messages:
    46
    Likes Received:
    1
    The stacktrace (use adb logcat to obtain) should help shed some light on what's going on.
     
  6. wige

    wige Well-Known Member This Topic's Starter

    Joined:
    Oct 28, 2009
    Messages:
    46
    Likes Received:
    0
    Ok, apparently it was a very stupid error on my part.... the manifest was missing a period...

    android:name="Statistics" should have been android:name=".Statistics"

    Thanks for the help and suggestions.
     

Share This Page

Loading...