2015-06-23 77 views
1

我正在嘗試啓動新的設置活動。不幸的是我得到了這些錯誤:進入新活動時出錯Android

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清單:

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

我已經嘗試過清理並重建項目噸。我正在使用Eclipse。

如果有人能夠幫助我解決這個問題,那將是非常好的事,因爲我已經和它打了幾個小時了。

回答

0

考慮到你的logcat你有一個問題在你的偏好xml。 android.view.InflateException: Binary XML file line #1: Error inflating class RelativeLayout

你的XML不應包含任何的RelativeLayout或任何常用的小工具,除了偏愛特定的標記 as described in here.

至於小試從R.xml.settings刪除所有XML和粘貼

<PreferenceScreen 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:key="first_preferencescreen"> 
<CheckBoxPreference 
     android:key="wifi enabled" 
     android:title="WiFi" /> 
<PreferenceScreen 
     android:key="second_preferencescreen" 
     android:title="WiFi settings"> 
    <CheckBoxPreference 
      android:key="prefer wifi" 
      android:title="Prefer WiFi" /> 
    </PreferenceScreen> </PreferenceScreen> 


和再試一次。

+0

現在完美。謝謝你的幫助。我不知道我在這裏不能使用RelativeLayout。 – KamCho

+0

太棒了!很高興爲您效勞 –

0

你不能直接替換android.R.id.content佈局,你需要有一個你自己的佈局作爲一個孩子。這是在Xml(說act_settings.xml),並在你的oncreate使用setContentView(R.layout.act_settings);粗略佈局,並取代你的片段交易中的這種佈局的根視圖