0

我在android中使用了下面的代碼,但它不能在android模擬器中工作。PreferenceActivity牛軋糖不工作

給出以下錯誤。

 --------- beginning of crash 
04-17 17:53:48.076 4802-4802/com.package E/AndroidRuntime: FATAL EXCEPTION: main 
                   Process: com.package, PID: 4802 
                   android.view.InflateException: Binary XML file line #30: Error inflating class <unknown> 
                    at android.view.LayoutInflater.createView(LayoutInflater.java:633) 
                    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55) 
                    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682) 
                    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741) 
                    at android.view.LayoutInflater.inflate(LayoutInflater.java:482) 
                    at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
                    at android.preference.Preference.onCreateView(Preference.java:510) 
                    at android.preference.Preference.getView(Preference.java:487) 
                    at android.preference.PreferenceGroupAdapter.getView(PreferenceGroupAdapter.java:246) 
                    at android.widget.AbsListView.obtainView(AbsListView.java:2347) 
                    at android.widget.ListView.makeAndAddView(ListView.java:1864) 
                    at android.widget.ListView.fillDown(ListView.java:698) 
                    at android.widget.ListView.fillFromTop(ListView.java:759) 
                    at android.widget.ListView.layoutChildren(ListView.java:1673) 
                    at android.widget.AbsListView.onLayout(AbsListView.java:2151) 
                    at android.view.View.layout(View.java:15671) 
                    at android.view.ViewGroup.layout(ViewGroup.java:5038) 
                    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703) 
                    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557) 
                    at android.widget.LinearLayout.onLayout(LinearLayout.java:1466) 
                    at android.view.View.layout(View.java:15671) 
                    at android.view.ViewGroup.layout(ViewGroup.java:5038) 
                    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) 
                    at android.widget.FrameLayout.onLayout(FrameLayout.java:514) 
                    at android.view.View.layout(View.java:15671) 
                    at android.view.ViewGroup.layout(ViewGroup.java:5038) 
                    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703) 
                    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557) 
                    at android.widget.LinearLayout.onLayout(LinearLayout.java:1466) 
                    at android.view.View.layout(View.java:15671) 
                    at android.view.ViewGroup.layout(ViewGroup.java:5038) 
                    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) 
                    at android.widget.FrameLayout.onLayout(FrameLayout.java:514) 
                    at android.view.View.layout(View.java:15671) 
                    at android.view.ViewGroup.layout(ViewGroup.java:5038) 
                    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) 
                    at android.widget.FrameLayout.onLayout(FrameLayout.java:514) 
                    at android.view.View.layout(View.java:15671) 
                    at android.view.ViewGroup.layout(ViewGroup.java:5038) 
                    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703) 
                    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557) 
                    at android.widget.LinearLayout.onLayout(LinearLayout.java:1466) 
                    at android.view.View.layout(View.java:15671) 
                    at android.view.ViewGroup.layout(ViewGroup.java:5038) 
                    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) 
                    at android.widget.FrameLayout.onLayout(FrameLayout.java:514) 
                    at android.view.View.layout(View.java:15671) 
                    at android.view.ViewGroup.layout(ViewGroup.java:5038) 
                    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) 
                    at android.widget.FrameLayout.onLayout(FrameLayout.java:514) 
                    at android.view.View.layout(View.java:15671) 
                    at android.view.ViewGroup.layout(ViewGroup.java:5038) 
                    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703) 
                    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557) 
                    at android.widget.LinearLayout.onLayout(LinearLayout.java:1466) 
                    at android.view.View.layout(View.java:15671) 
                    at android.view.ViewGroup.layout(ViewGroup.java:5038) 
                    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) 
                    at android.widget.FrameLayout.onLayout(FrameLayout.java:514) 
                    at android.view.View.layout(View.java:15671) 
                    at android.view.ViewGroup.layout(ViewGroup.java:5038) 
                    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2086) 
                    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1843) 
                   at android.view.ViewRootImpl 
04-17 17:53:48.077 1965-2042/system_process W/ActivityManager: Force finishing activity 1 com.package/.MyTabActivity 

而下面是我用過的代碼。

import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.preference.ListPreference; 
import android.preference.Preference; 
import android.preference.PreferenceActivity; 
import android.preference.PreferenceCategory; 
import android.preference.PreferenceFragment; 
import android.util.Log; 

import com.google.android.gms.analytics.GoogleAnalytics; 

import org.jraf.android.backport.switchwidget.SwitchPreference; 

public class SettingsActivity extends PreferenceActivity { 
    private static final String TAG = "SettingsActivity"; 

    SharedPreferences mPrefs; 

    SharedPreferences.Editor editor; 

    PreferenceCategory prefCategoryGenerals; 

    ListPreference pitchLevel, recordingType, visualizerType; 

    SwitchPreference showQuestionTextSwitch, visualizerSwitch; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     //addPreferencesFromResource(R.xml.settings); 
     //getSherlock().getActionBar().setDisplayHomeAsUpEnabled(true); 
     getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit(); 

     //mPrefs = getSharedPreferences(Util.PREFS_SETTINGS, MODE_PRIVATE); 
     //editor = mPrefs.edit(); 

    } 
    public static class MyPreferenceFragment extends PreferenceFragment 
    { 
     @Override 
     public void onCreate(final Bundle savedInstanceState) 
     { 
      super.onCreate(savedInstanceState); 
      addPreferencesFromResource(R.xml.settings); 
     } 
    } 
    @Override 
    protected void onResume() { 
     //init(); 
     super.onResume(); 
    } 
    @Override 
    protected void onStart() { 
     //EasyTracker.getInstance().activityStart(this); // Add this method. 
     GoogleAnalytics.getInstance(this).reportActivityStart(this); 
     super.onStart(); 
    } 
    @Override 
    public void onStop() { 
     super.onStop(); 
     // The rest of your onStop() code. 
     //EasyTracker.getInstance().activityStop(this); // Add this method. 
     GoogleAnalytics.getInstance(this).reportActivityStop(this); 
    } 
    Preference.OnPreferenceChangeListener changeListener = new Preference.OnPreferenceChangeListener() { 
     public boolean onPreferenceChange(Preference preference, Object newValue) { 
      if (newValue instanceof String) { 
       editor.putString(preference.getKey(), (String)newValue); 
       editor.commit(); 
      } else if (newValue instanceof Boolean) { 
       editor.putBoolean(preference.getKey(), (Boolean)newValue); 
       editor.commit(); 
      } 
      Log.e(preference.getKey(),newValue.toString()); 
      updatePreferences(); 
      return true; 
     } 
    }; 

    private void init() { 
     prefCategoryGenerals = (PreferenceCategory)findPreference(getString(R.string.settings_general)); 

     pitchLevel = (ListPreference)findPreference(getString(R.string.pitch_duration)); 
     pitchLevel.setOnPreferenceChangeListener(changeListener); 

     recordingType = (ListPreference)findPreference(getString(R.string.recording_type)); 
     recordingType.setOnPreferenceChangeListener(changeListener); 

     visualizerSwitch = (SwitchPreference)findPreference(getString(R.string.visualizer)); 
     visualizerSwitch.setOnPreferenceChangeListener(changeListener); 

     showQuestionTextSwitch = (SwitchPreference)findPreference(getString(R.string.question_text)); 
     showQuestionTextSwitch.setOnPreferenceChangeListener(changeListener); 

     visualizerType = (ListPreference)findPreference(getString(R.string.visualizer_type)); 
     visualizerType.setOnPreferenceChangeListener(changeListener); 

     updatePreferences(); 

    } 

    private void updatePreferences() { 
     boolean recordingTypeVideo = mPrefs.getString(Util.PREFS_RECORDING_TYPE, Util.PREFS_RECORDING_A).equals(Util.PREFS_RECORDING_V); 
     boolean visualizerEnabled = mPrefs.getBoolean(Util.PREFS_SHOW_VISUALIZER, false); 
     boolean visualizerTypeEnabled = !recordingTypeVideo && visualizerEnabled; 
     visualizerType.setEnabled(visualizerTypeEnabled); 
     visualizerSwitch.setEnabled(!recordingTypeVideo); 
    } 

} 

而下面是代碼Settings.xml文件。

<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:switchpref="http://schemas.android.com/apk/res-auto" 
    android:persistent="true" > 

    <PreferenceCategory 
     android:key="@string/settings_general" 
     android:title="@string/settings_general" > 
     <ListPreference 
      android:key="@string/pitch_duration" 
      android:title="@string/pitch_duration" 
      android:defaultValue="@string/pitch_standard" 
      android:entries="@array/pitch" 
      android:entryValues="@array/pitch" 
      android:summary="@string/summary_pitch_duration" 
      /> 
     <ListPreference 
      android:key="@string/recording_type" 
      android:title="@string/recording_type" 
      android:defaultValue="@string/audio_recording" 
      android:entries="@array/recording_type" 
      android:entryValues="@array/recording_type" 
      android:summary="@string/summary_recording_type" 
      /> 

     <org.jraf.android.backport.switchwidget.SwitchPreference 
      android:key="@string/question_text" 
      android:title="@string/question_text" 
      switchpref:asb_summaryOff="@string/switch_prefs_question_text" 
      switchpref:asb_summaryOn="@string/switch_prefs_question_text" 
      switchpref:asb_switchTextOff="@string/off" 
      switchpref:asb_switchTextOn="@string/on"/> 

     <org.jraf.android.backport.switchwidget.SwitchPreference 
      android:key="@string/visualizer" 
      android:title="@string/visualizer" 
      android:defaultValue="false" 
      switchpref:asb_summaryOff="@string/switch_prefs_visualizer" 
      switchpref:asb_summaryOn="@string/switch_prefs_visualizer" 
      switchpref:asb_switchTextOff="@string/off" 
      switchpref:asb_switchTextOn="@string/on" /> 

     <ListPreference 
      android:key="@string/visualizer_type" 
      android:title="@string/visualizer_type" 
      android:defaultValue="@string/visualizer_line" 
      android:entries="@array/visualizer_type" 
      android:entryValues="@array/visualizer_type" 
      android:summary="@string/summary_visualizer_type" 
      /> 
     <com.iq.android.preferences.AboutDialogPref 
      android:dialogLayout="@layout/dialog_about_layout" 
      android:key="funThing" 
      android:negativeButtonText="Cancel" 
      android:positiveButtonText="OK" 
      android:title="About" /> 
    </PreferenceCategory> 

</PreferenceScreen> 

上面的整個代碼在Android 4.4.4模擬器和設備上也能正常工作。

+2

我建議您編輯問題併發布整個Java堆棧跟蹤。我還建議您臨時評論第三方首選項,並確定其中一項是否是問題的根源。 – CommonsWare

+0

是的我已嘗試刪除所有庫並檢查,但仍然無法正常工作。我將更新完整的堆棧跟蹤。 –

+2

你確定'org.jraf.android.backport.switchwidget.SwitchPreference'和'com.iq.android.preferences.AboutDialogPref'是有效的類嗎?仔細檢查包名稱。 – azizbekian

回答

相關問題