1

我刚刚开始使用android应用程序开发所以我在eclipse中制作了一个以android 10为基础的应用程序(姜饼)然后我编辑了我的主要活动(main.java)以显示一个带有两个选项的菜单A)设置B)关于然后我在 /res/xml/settings.xml 创建了一个资源,然后使用 addPreferencesFromResource() 方法制作了 Prefs.java。

但是当我点击设置时,我什么也看不到..我还在我的应用程序的 Android 清单中定义了活动..

这是我的代码://Prefs.java

package com.mhrsolanki2020.thecaller;

import android.preference.PreferenceActivity;
import android.os.Bundle;

public class Prefs extends PreferenceActivity {

    protected void onCreate(Bundle savedInstance)
    {
        super.onCreate(savedInstance);
        addPreferencesFromResource(R.xml.settings);

    }

}

//main.java

package com.mhrsolanki2020.thecaller;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;


import android.view.MenuInflater;

public class Main extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return true;
    }

    public boolean onOptionsItemSecelted(MenuItem item){
        switch(item.getItemId()){
        case R.id.action_settings:
            startActivity(new Intent(this,Prefs.class));
            return true;
        }
        return false;
    }

}

//Android 清单

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

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="10" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.mhrsolanki2020.thecaller.Main"
            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="com.mhrsolanki2020.thecaller.Prefs"
           android:label="@string/prefs_label"> </activity>  </application>

</manifest>

// /res/xml/settings.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android">
    <CheckBoxPreference 
        android:defaultValue="true"
        android:title="@string/activation"
        android:summary="@string/activatoinSummary"/>
</PreferenceScreen>

// /menu/main.xml

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

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"/>
    <item
        android:id="@+id/action_about"
        android:orderInCategory="101"
        android:title="@string/action_about"/>


</menu>
4

1 回答 1

1

你有一个错字:

 public boolean onOptionsItemSecelted(MenuItem item){  // <-- typo onOptionsItemSelected
        switch(item.getItemId()){
        case R.id.action_settings:
            startActivity(new Intent(this,Prefs.class));
            return true;
        }
        return false;
    }
于 2013-09-15T12:30:08.480 回答