• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps startActivity crashing

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:
public class MyClass extends Activity 
{

   @Override
   public boolean onOptionsItemSelected(MenuItem item) 
   {
      switch (item.getItemId())
      {
      case MENU_SETTINGS:
          startActivity(new Intent(this, TheSettings.class));
          break;
      }
      return super.onOptionsItemSelected(item);
   }
};

Is there any other code I should post in order to figure out the problem?
 
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:
    public boolean onOptionsItemSelected(MenuItem item) {
    	int id = item.getItemId() ;
    	
    	if( id==MENU_ABOUT ) {
    		showAbout() ;
    	}
    	else 
    	if( id==MENU_SETTINGS ) {
    		showSettings() ;
    	}
    	else
    	if( id==MENU_GALLERY ) {
    		showGallery() ;
    	}
        
        return false ;
    }
    
    private void showAbout() {
    	Intent intent = new Intent(this, AboutActivity.class);
    	startActivity(intent) ;    	
    }
    
    private void showSettings() {
    	Intent intent = new Intent(this, SettingsActivity.class);
    	startActivity(intent) ;
    }

My AboutActivity and SettingsActivity classes extend Activity, like this:

Code:
public class AboutActivity extends Activity {
	private WebView m_webview = null ;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.about) ;
		
		m_webview = (WebView) findViewById(R.id.aboutview);
		m_webview.getSettings().setJavaScriptEnabled(false);
		m_webview.loadUrl("file:///android_asset/about.html") ;
	}
}

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.
 
Upvote 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:
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class TheSettings extends Activity
{
	@Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        TextView tv = new TextView(this);
        tv.setText("test");
        setContentView(tv);
    }
};
 
Upvote 0
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:
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".SoundCam" android:screenOrientation="landscape" 
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SettingsActivity" android:screenOrientation="landscape"
                  android:label="Settings" android:theme="@android:style/Theme.Dialog">
        </activity>
        <activity android:name=".AboutActivity" android:screenOrientation="landscape"
                  android:label="@string/about_title">
        </activity>
    </application>
 
  • Like
Reactions: ballju and Leolicos
Upvote 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))
<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones