活动没有在android中开放

时间:2013-09-15 11:43:52

标签: android android-activity

我刚开始使用android app开发 所以我在eclipse中制作了一个应用程序,以android 10为基础(姜饼) 然后我编辑了我的主要活动(main.java)以显示一个菜单,其中包含两个选项A)设置B)about 然后我在/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 Manifest

<?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>

1 个答案:

答案 0 :(得分: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;
    }