进入Android新活动时出错

时间:2015-06-23 23:41:38

标签: java android eclipse android-activity settings

我试图开始新的设置活动。不幸的是我遇到了这些错误:

06-24 01:18:17.347: E/AndroidRuntime(16017): FATAL EXCEPTION: main
06-24 01:18:17.347: E/AndroidRuntime(16017): Process: com.stoper.moneystopwatch, PID: 16017
06-24 01:18:17.347: E/AndroidRuntime(16017): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.stoper.moneystopwatch/com.stoper.moneystopwatch.Settings}: android.view.InflateException: Binary XML file line #1: Error inflating class RelativeLayout
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2252)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2301)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.app.ActivityThread.access$800(ActivityThread.java:144)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.os.Handler.dispatchMessage(Handler.java:106)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.os.Looper.loop(Looper.java:136)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.app.ActivityThread.main(ActivityThread.java:5212)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at java.lang.reflect.Method.invokeNative(Native Method)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at java.lang.reflect.Method.invoke(Method.java:515)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at dalvik.system.NativeStart.main(Native Method)
06-24 01:18:17.347: E/AndroidRuntime(16017): Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class RelativeLayout
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:441)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.preference.GenericInflater.inflate(GenericInflater.java:317)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.preference.GenericInflater.inflate(GenericInflater.java:263)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:272)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.preference.PreferenceFragment.addPreferencesFromResource(PreferenceFragment.java:285)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at com.stoper.moneystopwatch.Settings$MyPreferenceFragment.onCreate(Settings.java:28)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.app.Fragment.performCreate(Fragment.java:1683)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:863)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1066)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.app.BackStackRecord.run(BackStackRecord.java:684)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1461)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.app.Activity.performStart(Activity.java:5249)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225)
06-24 01:18:17.347: E/AndroidRuntime(16017):    ... 11 more
06-24 01:18:17.347: E/AndroidRuntime(16017): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.preference.RelativeLayout" on path: DexPathList[[zip file "/data/app/com.stoper.moneystopwatch-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.stoper.moneystopwatch-1, /vendor/lib, /system/lib]]
06-24 01:18:17.347: E/AndroidRuntime(16017):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.preference.GenericInflater.createItem(GenericInflater.java:375)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.preference.GenericInflater.onCreateItem(GenericInflater.java:417)
06-24 01:18:17.347: E/AndroidRuntime(16017):    at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:428)
06-24 01:18:17.347: E/AndroidRuntime(16017):    ... 23 more

我的活动开始:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
         Intent i= new Intent(Main.this, Settings.class);
         startActivity(i);
      return true;
    }
}

我的设置类:

public class Settings extends PreferenceActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
    }

    public static class MyPreferenceFragment extends PreferenceFragment
    {
        @Override
        public void onCreate(final Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.settings);
        }
    }
}

我的Android Manifest:

<activity
    android:name=".Settings"
    android:label="@string/action_settings" >
</activity>

我已经尝试清理并重建项目了。我正在使用Eclipse。

如果有人可以帮我解决这个问题,那将是非常好的,因为我已经与它斗争了好几个小时。

2 个答案:

答案 0 :(得分:0)

你无法直接替换android.R.id.content布局,你需要有一个自己的布局作为孩子。这在Xml中有一个粗略的布局(比如act_settings.xml),在你的oncreate中使用setContentView(R.layout.act_settings);并在你的片段事务中替换这个布局的根视图

答案 1 :(得分:0)

考虑到您的logcat,您的首选项xml中存在问题。 req_konfirmasi

您的xml不应包含任何RelativeLayout或任何常用小部件,但特定于首选项的标记除外 as described in here.

对于小测试,从nikah中删除所有xml并粘贴

android.view.InflateException: Binary XML file line #1: Error inflating class RelativeLayout

再试一次。