Android:ActionBar始终打开SettingsActivity

时间:2013-06-06 20:18:10

标签: android android-intent android-activity

同时我不知道为什么我的导航无效。 在我的主菜单中有2个图标,以便切换到不同的活动:搜索和设置。 但每次我点击搜索时,都会打开settingsActivity。我检查了搜索的ID,在Manifest中输入了活动......

如果我在视图中添加一个额外的按钮并尝试启动searchActivity,一切正常。但即使我正在评论案例R.id.settingsMenu-thing,如果我点击搜索符号,settingsActivity也会被打开......任何想法? :(提前致谢!

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>

修改

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>

1 个答案:

答案 0 :(得分:0)

唯一的可能是Eclipse正在搞乱你的元素的ID。在我更新SDK但不更新ADT之后,这发生在我身上。所以,这是一个建议:

1-尝试清理项目。在Eclipse中,它位于Project-&gt;干净。使用debug语句,检查您单击的元素的ID(R.id.settings,R.id.search)是否与R.java

中定义的元素相同

2-如果不起作用,请将SDK更新到最新版本(截至本文,22)。另外,更新ADT。请查看此答案:How can I update my ADT in Eclipse?

3-最终手段:删除项目,然后重新导入。