单击设置按钮时出错

时间:2013-01-17 14:27:05

标签: android android-intent

我是Android开发人员的新手,并试图让我的应用程序显示“设置”菜单。

首选项的XML文件位于:

<?xml version="1.0" encoding="UTF-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/settings">
<SwitchPreference
    android:title="Switch Preference"
    android:key="switchpref"
    android:switchTextOn="ON"
    android:switchTextOff="OFF"
    android:summary="Night Mode"
    android:defaultValue="false"
    android:disableDependentsState="false"
    />

 </PreferenceScreen>

Main Class文件在这里,我尝试切换到Settings Activity

package com.example.notepad;

import java.util.zip.Inflater;

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

public class HomeScreen extends Activity {

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


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

public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId())
    {
    case R.id.menu_settings:
         Intent intent = new Intent(HomeScreen.this,      PreferencesFragment.class);
            startActivity(intent);
            return true;

            default:
                return super.onOptionsItemSelected(item);
    }

}
}

显示“设置”菜单的首选项Java文件在此处

package com.example.notepad;

import android.app.Activity;
import android.os.Bundle;

public class PreferencesFragment extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.xml.preferences);
}

}

我无法弄清楚错误发生的原因。当我单击“设置”按钮

时,它仅显示通用“不幸的是,应用程序已停止”

日志文件在这里

01-17 19:49:28.795: E/AndroidRuntime(756): FATAL EXCEPTION: main
01-17 19:49:28.795: E/AndroidRuntime(756): java.lang.RuntimeException: Unable to start    activity ComponentInfo{com.example.notepad/com.example.notepad.PreferencesFragment}:  android.view.InflateException: Binary XML file line #2: Error inflating class  PreferenceScreen
01-17 19:49:28.795: E/AndroidRuntime(756):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
01-17 19:49:28.795: E/AndroidRuntime(756):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
01-17 19:49:28.795: E/AndroidRuntime(756):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
01-17 19:49:28.795: E/AndroidRuntime(756):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
01-17 19:49:28.795: E/AndroidRuntime(756):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-17 19:49:28.795: E/AndroidRuntime(756):  at android.os.Looper.loop(Looper.java:137)
01-17 19:49:28.795: E/AndroidRuntime(756):  at android.app.ActivityThread.main(ActivityThread.java:4745)
 01-17 19:49:28.795: E/AndroidRuntime(756):     at java.lang.reflect.Method.invokeNative(Native Method)
01-17 19:49:28.795: E/AndroidRuntime(756):  at java.lang.reflect.Method.invoke(Method.java:511)
01-17 19:49:28.795: E/AndroidRuntime(756):  at      com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-17 19:49:28.795: E/AndroidRuntime(756):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-17 19:49:28.795: E/AndroidRuntime(756):  at dalvik.system.NativeStart.main(Native Method)
01-17 19:49:28.795: E/AndroidRuntime(756): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class PreferenceScreen
01-17 19:49:28.795: E/AndroidRuntime(756):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698)
01-17 19:49:28.795: E/AndroidRuntime(756):  at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
01-17 19:49:28.795: E/AndroidRuntime(756):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-17 19:49:28.795: E/AndroidRuntime(756):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-17 19:49:28.795: E/AndroidRuntime(756):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
01-17 19:49:28.795: E/AndroidRuntime(756):  at android.app.Activity.setContentView(Activity.java:1867)
01-17 19:49:28.795: E/AndroidRuntime(756):  at com.example.notepad.PreferencesFragment.onCreate(PreferencesFragment.java:12)
01-17 19:49:28.795: E/AndroidRuntime(756):  at android.app.Activity.performCreate(Activity.java:5008)
01-17 19:49:28.795: E/AndroidRuntime(756):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
01-17 19:49:28.795: E/AndroidRuntime(756):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
01-17 19:49:28.795: E/AndroidRuntime(756):  ... 11 more
01-17 19:49:28.795: E/AndroidRuntime(756): Caused by: java.lang.ClassNotFoundException: android.view.PreferenceScreen
01-17 19:49:28.795: E/AndroidRuntime(756):  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
01-17 19:49:28.795: E/AndroidRuntime(756):  at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
 01-17 19:49:28.795: E/AndroidRuntime(756):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
01-17 19:49:28.795: E/AndroidRuntime(756):  at android.view.LayoutInflater.createView(LayoutInflater.java:552)
01-17 19:49:28.795: E/AndroidRuntime(756):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:643)
01-17 19:49:28.795: E/AndroidRuntime(756):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
01-17 19:49:28.795: E/AndroidRuntime(756):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
01-17 19:49:28.795: E/AndroidRuntime(756):  at          android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
01-17 19:49:28.795: E/AndroidRuntime(756):  ... 20 more

编辑 - 我的AndroidManifest.xml文件:

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

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.notepad.HomeScreen"
        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=".PreferencesFragment" />
</application>

1 个答案:

答案 0 :(得分:1)

您无法在Activity(PreferencesFragment extends Activity)中使用PreferenceScreen。阅读文档并正确使用它。

  

PreferenceScreen可以出现在两个地方:

     

当PreferenceActivity指向此时,它将用作根并且不显示(仅显示包含的首选项)。

     

当它出现在另一个首选项层次结构中时,它会显示并作为另一个首选项屏幕的网关(通过显示另一个首选项屏幕作为对话框或通过getIntent(通过startActivity(android.content.Intent)显示) ))。此PreferenceScreen的子项不会显示在显示此PreferenceScreen的屏幕中。而是在单击此首选项时将显示单独的屏幕。