0

meanwhile I have no idea why my navigation is not working. In my mainmenu there are 2 icons in order to switch to different activities: searching and settings. But everytime I am clicking on search, settingsActivity is opened. I checked the IDs of search, activities are entered in the Manifest ...

If I am adding an additional button to a view and trying to start the searchActivity, everything works fine. But even if I am commenting out the case R.id.settingsMenu-thing, the settingsActivity is opened if I am clicking on the search symbol ... Any ideas? :( Thanks in advance!

MainActivity.java

public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.mainmenu, menu);
    return super.onCreateOptionsMenu(menu);
     }

public boolean onOptionsItemSelected(MenuItem item){
        Intent intent = null;

        switch(item.getItemId()){

        case android.R.id.home:
            intent = new Intent(this,MainActivity.class);
            startActivity(intent);
            break;

        case R.id.searchMenu:
            intent = new Intent(this,SearchActivity.class);
            startActivity(intent);
            break;

        case R.id.settingsMenu:
            intent = new Intent(this,SettingsActivity.class);
            startActivity(intent);
            break;

        default:
            return super.onOptionsItemSelected(item);
        }
        return true;
    }

Manifest.xml

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.anothertest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:debuggable="true">
        <activity
            android:name="android.app.ui.MainActivity"
            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="android.app.ui.SearchActivity" android:label="@string/search"></activity>
        <activity android:name="android.app.ui.SettingsActivity" android:label="@string/settings"></activity>
        <activity android:name="android.app.ui.NoArticleInListActivity"></activity>
    </application>

</manifest>

mainmenu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item 
        android:id="@+id/searchMenu"
        android:title="@string/search"
        android:icon="@android:drawable/ic_menu_search"
        android:orderInCategory="1"
        android:showAsAction="always">
        </item>

    <item
        android:id="@+id/settingsMenu"
        android:orderInCategory="2"
        android:showAsAction="always"
        android:title="@string/action_settings"
        android:icon="@android:drawable/ic_menu_preferences">
        </item>

</menu>

EDIT

SettingsFragment

public class SettingsFragment extends PreferenceFragment{

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }
}

SettingsActivity

public class SettingsActivity extends Activity{
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit();
    }

    protected void onStart(){
        super.onStart();
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

    public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId()){
        case android.R.id.home:
            Intent intent = new Intent(this,MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);

        default:
            return super.onOptionsItemSelected(item);
        }
    }
}

preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory 
        android:title="@string/settingsSummaryCalendar">
        android:key="pref_calendar_settings">
        <CheckBoxPreference
            android:key="prefArticleAsCalendarEntry"
            android:summary="@string/settingsSummaryCalendarEntry"
            android:title="@string/settingCreateEntryForEach"
            android:defaultValue="true"/>
        <CheckBoxPreference 
            android:key="prefCreateOwnCalendar"
            android:summary="@string/settingsSummaryCalendar"
            android:title="@string/settingCreateOwnCalendar"
            android:defaultValue="true"/>
    </PreferenceCategory>

    <PreferenceCategory 
        android:title="@string/settingsSummaryNotification">
        android:key="pref_notification_settings">
        <CheckBoxPreference
            android:key="prefNotification"
            android:summary="@string/settingsSummaryNotification"
            android:title="@string/settingsNotification"
            android:defaultValue="true" >
        </CheckBoxPreference>
        <ListPreference 
            android:key="prefNotificationTime"
            android:entries="@array/notificationTime"
            android:entryValues="@array/notificationTimeValues"
            android:summary="@string/settingsSummaryNotificationTime"
            android:title="@string/settingsNotificationTime"
            android:defaultValue="@string/settingsNotificationTime_default" 
            android:dependency="prefNotification">
        </ListPreference>
    </PreferenceCategory>
</PreferenceScreen>
Manuel
  • 1
  • 1
  • A suggestion: add a `Log.debug` statement within every clause of the break, so that you know which branch of your code gets executed. Also, posting the logcat here might help. BUt yeah, at first glance, seems OK – verybadalloc Jun 06 '13 at 20:42
  • After adding Log.d(Tag, text), the same numbers/ids are showed in the console as I did with System.out.println. But when I am trying to start the Search activity, the ID displayed twice in the console ... seems like the debug command is called twice?! but why? Even after changing the switch command to a simple if-else, the settingsActivity is always called :( – Manuel Jun 07 '13 at 13:34
  • Any other ideas? I don't get it ... :( – Manuel Jun 08 '13 at 11:03

1 Answers1

0

The only possibility is that Eclipse is messing up the ids of your elements. This happened to me after I updated the SDK but not ADT. So, here is a suggestion:

1- Try to clean the project. In Eclipse, it is under Project-> Clean . Using the debug statement, check that the ids of the elements you click on (R.id.settings, R.id.search) are the same as the ones defined in R.java

2- If that does not work, update the SDK to the latest (as of this posting, 22). Also, update ADT. Check this answer for how to do so: How can I update my ADT in Eclipse?

3- Final resort: delete the project, and reimport it.

Community
  • 1
  • 1
verybadalloc
  • 5,768
  • 2
  • 33
  • 49
  • unfortunalety, this didn't help either ... if i'm trying to reimport the project, i receive the message that the project description is invalid. So I created a new project and used the same code etc. --> still the same error. So, in my opinion, the error must come with the settings. I will add them in my first post. Maybe you find the error here ... cause meanwhile I'm totatlly frustrated :( – Manuel Jun 09 '13 at 15:57