0

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

如果我在视图中添加一个额外的按钮并尝试启动 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;
    }

清单.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>

主菜单.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>

编辑

设置片段

public class SettingsFragment extends PreferenceFragment{

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

设置活动

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);
        }
    }
}

首选项.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>
4

1 回答 1

0

唯一的可能是 Eclipse 弄乱了元素的 ID。这发生在我更新了 SDK 而不是 ADT 之后。所以,这里有一个建议:

1-尝试清理项目。在 Eclipse 中,它位于 Project-> Clean 下。使用调试语句,检查您点击的元素的 id (R.id.settings, R.id.search) 是否与中定义的相同R.java

2- 如果这不起作用,请将 SDK 更新到最新版本(截至本文发布时为 22)。另外,更新 ADT。检查此答案以了解如何执行此操作:如何在 Eclipse 中更新我的 ADT?

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

于 2013-06-08T11:34:25.867 回答