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

Apps If, else causing errors

cr5315

Android Enthusiast
I've got an app that uses a toggle button to decide whether to launch an activity or the browser on a button click, but when ever I start the activity, Eclipse gives me errors in the Debug tab.
The toggle button is also in a different activity than the one giving me troubles.

The trouble code
Code:
final ToggleButton togglebutton = (ToggleButton) findViewById(R.id.toggleButton1);
  @Override
  protected void onListItemClick(ListView l, View v, int position, long id) {
   // TODO Auto-generated method stub
	  if (togglebutton.isChecked()) {
		  Intent intent = new Intent(this,ShowDetails.class);
   	   Bundle bundle = new Bundle();
   	   bundle.putString("keyTitle", myRssFeed.getItem(position).getTitle());
   	   bundle.putString("keyDescription", myRssFeed.getItem(position).getDescription());
   	   bundle.putString("keyLink", myRssFeed.getItem(position).getLink());
   	   bundle.putString("keyPubdate", myRssFeed.getItem(position).getPubdate());
   	   intent.putExtras(bundle);
   	        startActivity(intent);
      } else {
    	  Bundle bundle = new Bundle();
    	  bundle.putString("keyLink", myRssFeed.getItem(position).getLink());
    	  Intent i = new Intent(Intent.ACTION_VIEW); 
    	  i.setData(Uri.parse(bundle.getString("keyLink"))); 
    	  startActivity(i);
      }

I know it's this code because when I take it out, the app works just fine.

The Eclipse errors
2u59lzs.png


I cant seem to think of any solutions to this.
 
But... What do you mean "The toggle button is also in a different activity"?

Is all this code in the activity that gives you the problem? Or is the toggle button initiated in the other activity?

Because, if it is only placed in the other activity, you cannot call the findViewById in this activity and get the right toggle button. findViewById only works on the currently loaded layout (from xml, or programatically added) of the current activity.

So if you need for the user to be able to go into some activity to set that toggle button, like a settings activity, and then reference that setting somewhere else, you need to have som kind of static boolean set when the user flips that toggle button instead of referencing the toggle button itself.
 
Showdetails is in the manifest. The app workes just fine without the togglebutton stuff but i am trying to add the option to turn the preview activity (showdetails) on or off.
 
Can you post your manifest file? And can you provide more info about the exception? (like message, etc).

Have you stepped through your code in order to see on which line the runtime exception occurs?
 
Manifest:
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.bba.supercross"
      android:versionCode="1" android:versionName="0.9">
    <uses-sdk android:minSdkVersion="3" />
    <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
        <activity android:name=".AndroidRssReader"
                  android:label="@string/app_name"
                  android:theme="@android:style/Theme.NoTitleBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="ShowDetails" />
        <activity android:name="aboutActivity" 
        		  android:theme="@android:style/Theme.NoTitleBar" >
        		  </activity>
        <activity android:name="raceActivity"
        		  android:theme="@android:style/Theme.NoTitleBar" >
        		  </activity>
    </application>
    <uses-permission android:name="android.permission.INTERNET" />
</manifest>

Whenever I debug, Eclipse gives me this in the debug tab:
11bl107.png


I took out the togglebutton part of the code and the app worked just fine, minus what the togglebutton would allow it to do.
 
Looks okay when the ShowDetails class is in your root package..

Can you provide details about the exception (Logcat..) This will explain why the activity cannot be displayed.

By the way, teach yourself to start your classnames with a capital..
 
Back
Top Bottom