startActivity crashing


Last Updated:

  1. Leolicos

    Leolicos Well-Known Member This Topic's Starter

    Joined:
    Jun 16, 2010
    Messages:
    54
    Likes Received:
    0
    I'm new to Android development and I'm on my first application. When my problem is, I have a menu option that says "Settings" and when selected, it brings up a new screen with all the settings options. I've looked at the examples and it shows 2 ways: intent filters, and hard-coding the class name; I'm using the latter method.

    Here's my code:

    Code (Text):
    1.  
    2. public class MyClass extends Activity
    3. {
    4.  
    5.    @Override
    6.    public boolean onOptionsItemSelected(MenuItem item)
    7.    {
    8.       switch (item.getItemId())
    9.       {
    10.       case MENU_SETTINGS:
    11.           startActivity(new Intent(this, TheSettings.class));
    12.           break;
    13.       }
    14.       return super.onOptionsItemSelected(item);
    15.    }
    16. };
    17.  
    Is there any other code I should post in order to figure out the problem?
     

    Advertisement
  2. markb

    markb Well-Known Member

    Joined:
    Sep 14, 2009
    Messages:
    148
    Likes Received:
    26
    Hi,

    I have similar code in one of my apps, but I return false instead of returning super.onOptionsItemSelected(item)

    Try changing the return statement so it returns false, and see if that works for you.

    Mark
     
  3. Leolicos

    Leolicos Well-Known Member This Topic's Starter

    Joined:
    Jun 16, 2010
    Messages:
    54
    Likes Received:
    0
    Thanks for the help, but I'm still getting the crash right at startActivity, it doesn't even get to the return. I just ran it in Debug mode and it seems to be throwing the "ActivityNotFoundException". Do I have to somehow include that file or class in the main one? (I'm new to java as well, C++ is my native language)
     
  4. markb

    markb Well-Known Member

    Joined:
    Sep 14, 2009
    Messages:
    148
    Likes Received:
    26
    Is your TheSettings class an Activity?

    Here's a bit more of my code, showing that the classes I refer to are Activity classes.

    Code (Text):
    1.     public boolean onOptionsItemSelected(MenuItem item) {
    2.         int id = item.getItemId() ;
    3.        
    4.         if( id==MENU_ABOUT ) {
    5.             showAbout() ;
    6.         }
    7.         else
    8.         if( id==MENU_SETTINGS ) {
    9.             showSettings() ;
    10.         }
    11.         else
    12.         if( id==MENU_GALLERY ) {
    13.             showGallery() ;
    14.         }
    15.        
    16.         return false ;
    17.     }
    18.    
    19.     private void showAbout() {
    20.         Intent intent = new Intent(this, AboutActivity.class);
    21.         startActivity(intent) ;    
    22.     }
    23.    
    24.     private void showSettings() {
    25.         Intent intent = new Intent(this, SettingsActivity.class);
    26.         startActivity(intent) ;
    27.     }
    28.  
    My AboutActivity and SettingsActivity classes extend Activity, like this:

    Code (Text):
    1. public class AboutActivity extends Activity {
    2.     private WebView m_webview = null ;
    3.    
    4.     @Override
    5.     protected void onCreate(Bundle savedInstanceState) {
    6.         super.onCreate(savedInstanceState);
    7.         setContentView(R.layout.about) ;
    8.        
    9.         m_webview = (WebView) findViewById(R.id.aboutview);
    10.         m_webview.getSettings().setJavaScriptEnabled(false);
    11.         m_webview.loadUrl("file:///android_asset/about.html") ;
    12.     }
    13. }
    14.  
    I'd try creating the Intent in a separate statement before you call startActivity.
    Just to see if the exception is happening when you create the Intent, or in the call to startActivity.

    If that doesn't provide any clues, then you might have to post more of your code here.
    If you post complete classes then I'll try them out for myself to see if I can see anything.
     
  5. Leolicos

    Leolicos Well-Known Member This Topic's Starter

    Joined:
    Jun 16, 2010
    Messages:
    54
    Likes Received:
    0
    I separated the Intent creation and startActivity into 2 separate lines and it appears to create the Intent just fine, but it throws the exception at the startActivity line. Also TheSettings extends Activity.

    Code (Text):
    1.  
    2. import android.app.Activity;
    3. import android.os.Bundle;
    4. import android.widget.TextView;
    5.  
    6. public class TheSettings extends Activity
    7. {
    8.     @Override
    9.     public void onCreate(Bundle savedInstanceState)
    10.     {
    11.         super.onCreate(savedInstanceState);
    12.         TextView tv = new TextView(this);
    13.         tv.setText("test");
    14.         setContentView(tv);
    15.     }
    16. };
    17.  
     
  6. markb

    markb Well-Known Member

    Joined:
    Sep 14, 2009
    Messages:
    148
    Likes Received:
    26
    Have you declared your activity classes in your AndroidManifest.xml file?

    Here's an extract from mine, showing the Activity classes I start from my menu.

    Code (Text):
    1.     <application android:icon="@drawable/icon" android:label="@string/app_name">
    2.         <activity android:name=".SoundCam" android:screenOrientation="landscape"
    3.                   android:label="@string/app_name">
    4.             <intent-filter>
    5.                 <action android:name="android.intent.action.MAIN" />
    6.                 <category android:name="android.intent.category.LAUNCHER" />
    7.             </intent-filter>
    8.         </activity>
    9.         <activity android:name=".SettingsActivity" android:screenOrientation="landscape"
    10.                   android:label="Settings" android:theme="@android:style/Theme.Dialog">
    11.         </activity>
    12.         <activity android:name=".AboutActivity" android:screenOrientation="landscape"
    13.                   android:label="@string/about_title">
    14.         </activity>
    15.     </application>
    16.  
     
    ballju and Leolicos like this.
  7. Leolicos

    Leolicos Well-Known Member This Topic's Starter

    Joined:
    Jun 16, 2010
    Messages:
    54
    Likes Received:
    0
    No... I didn't think I need to put that stuff in there unless I was planning on using intent filters. I'll try putting it in now and see what happens.
     
  8. Leolicos

    Leolicos Well-Known Member This Topic's Starter

    Joined:
    Jun 16, 2010
    Messages:
    54
    Likes Received:
    0
    It works!! Thanks! This has been driving me nuts for days. :)
     
  9. bdtuhin007

    bdtuhin007 New Member

    Joined:
    Nov 17, 2011
    Messages:
    4
    Likes Received:
    0
    hi all,

    I have a code but it doesn't work ! can you please help me on it? it will show graph in android field. but startActivity doesn't properly !! can anyone help me please ?

    <<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    int DataCnt = 6;
    SortedData[][] //This is an 2D array
    ---------------
    [[FASTWEB-2012, -66, 0], [FASTWEB-1-001CA2B8E818, -85, 0], [FASTWEB-2012, -66, 5], [FASTWEB-1-001CA2B8E818, -85, 5], [FASTWEB-2012, -66, 10], [FASTWEB-1-001CA2B8E818, -85, 10]]
    TerminalCount[][] ////This is an 2D array
    -------------------
    [[FASTWEB-2012, 3], [FASTWEB-1-001CA2B8E818, 3]]


    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
    XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();

    switch (TermDimenSnRow) //suppose I have TermDimenSnRow = 2
    {
    case 1:
    break;

    case 2:
    TimeSeries series2 = new TimeSeries("WiFi 1");
    TimeSeries series3 = new TimeSeries("WiFi 2");
    for( int i2 = 0; i2 < DataCnt; i2++)
    {
    if(TerminalCount[0][0].equals(SortedData[i2][0]))
    {
    int WifiRSSIVal = (Integer.parseInt(String.valueOf(SortedData[i2][1])));
    int WifiTimeVal = (Integer.parseInt(String.valueOf(SortedData[i2][2])));
    series2.add(WifiRSSIVal, WifiTimeVal);
    }
    }
    for( int i3 = 0; i3 < DataCnt; i3++)
    {
    if(TerminalCount[1][0].equals(SortedData[i3][0]))
    {
    int WifiRSSIVal = (Integer.parseInt(String.valueOf(SortedData[i3][1])));
    int WifiTimeVal = (Integer.parseInt(String.valueOf(SortedData[i3][2])));
    series3.add(WifiRSSIVal, WifiTimeVal);
    }
    }
    dataset.addSeries(series2);
    XYSeriesRenderer renderer2 = new XYSeriesRenderer();
    mRenderer.addSeriesRenderer(renderer2);

    dataset.addSeries(series3);
    XYSeriesRenderer renderer3 = new XYSeriesRenderer();
    mRenderer.addSeriesRenderer(renderer3);
    break;
    case 3:
    break;
    -
    -
    -
    -
    -


    case 10:
    break;

    Intent intent = ChartFactory.getLineChartIntent(context, dataset, mRenderer, "Graph 1");
    this.startActivity(intent); //I have tried here by this (startActivity(intent))
    <<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
     

Share This Page

Loading...