0

J'essaie d'implémenter une page de paramètres pour mon application sur Android. Il a bien fonctionné si je mets le fichier de mise en page comme ceci:Android PreferenceActivity gonfle-exception

<?xml version="1.0" encoding="utf-8"?> 

<CheckBoxPreference 
    android:defaultValue="true" 
    android:key="autoBack" 
    android:summaryOff="Let strange calls in" 
    android:summaryOn="Unlisted calls will be blocked" 
    android:title="Block Strange Calls" /> 

<PreferenceCategory android:title="Blocking Options" > 
    <CheckBoxPreference 
     android:defaultValue="false" 
     android:key="smsSilence" 
     android:summaryOff="Off" 
     android:summaryOn="On" 
     android:title="Send Blocked caller message" /> 

    <PreferenceScreen android:title="More Options" > 
     <CheckBoxPreference 
      android:defaultValue="true" 
      android:key="cb21" 
      android:summaryOff="关闭" 
      android:summaryOn="开启" 
      android:title="功能1" /> 
     <CheckBoxPreference 
      android:defaultValue="true" 
      android:key="cb22" 
      android:summaryOff="停用" 
      android:summaryOn="使用" 
      android:title="功能2" /> 
    </PreferenceScreen> 
</PreferenceCategory> 

Dans les programmes, les codes d'appel de la mise en page est:

Intent j = new Intent("android.intent.action.PREFS"); 
startActivity(j); 

public class Prefs extends PreferenceActivity { 
@SuppressWarnings("deprecation") 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    super.onCreate(savedInstanceState); 
    addPreferencesFromResource(R.xml.preference); 
     } 
} 

Et la le résultat fonctionne comme la photo ci-dessous:

enter image description hereenter image description here

Mais en réalité, je veux que mes paramètres page peut prendre tout l'écran, donc j'ajouter un « barre de titre » (comme la hausse de pic - je l'imaginais) par moi-même dans la mise en page xml, et je aussi ajouter une fonction "note notre application", en ajoutant un RatingBarto ma mise en page. Alors xml est changé ci-dessous:

<!-- language: lang-xml --> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="35sp" 
    android:background="@drawable/titlebg" > 

    <ImageView 
     android:id="@+id/titleIc" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:src="@drawable/ic_launcher" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_toRightOf="@id/titleIc" 
     android:text="RoboGuard" 
     android:textColor="#ffffffff" 
     android:textSize="16sp" /> 

    <ImageButton 
     android:id="@+id/imagebutton1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:src="@drawable/setting" /> 

</RelativeLayout> 

<CheckBoxPreference 
    android:defaultValue="true" 
    android:key="autoBack" 
    android:summaryOff="Let strange calls in" 
    android:summaryOn="Unlisted calls will be blocked" 
    android:title="Block Strange Calls" /> 

<PreferenceCategory android:title="Blocking Options" > 
    <CheckBoxPreference 
     android:defaultValue="false" 
     android:key="smsSilence" 
     android:summaryOff="Off" 
     android:summaryOn="On" 
     android:title="Send Blocked caller message" /> 

    <PreferenceScreen android:title="More Options" > 
     <CheckBoxPreference 
      android:defaultValue="true" 
      android:key="cb21" 
      android:summaryOff="off" 
      android:summaryOn="on" 
      android:title="" /> 
     <CheckBoxPreference 
      android:defaultValue="true" 
      android:key="cb22" 
      android:summaryOff="stop" 
      android:summaryOn="stop" 
      android:title="function2" /> 
    </PreferenceScreen> 
</PreferenceCategory> 
<PreferenceCategory android:title="Rating the App" > 
    <RatingBar 
     android:id="@+id/rating" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:max="255" 
     android:numStars="5" 
     android:progress="255" 
     android:stepSize="0.5" /> 
</PreferenceCategory> 

Mais quand je courais le programme, il a généré une erreur fatale:

java.lang.RuntimeException: Unable to start activity ComponentInfo{xxx}: android.view.InflateException: Binary XML file line #4: Error inflating class RelativeLayout 

java.lang.RuntimeException: Unable to start activity ComponentInfo{xxx}: android.view.InflateException: Binary XML file line #36: Error inflating class RatingBar 

Quelqu'un pourrait-il s'il vous plaît aidez-moi en dehors? De plus, comment faire en sorte que la PreferenceActivity prenne le plein écran pour montrer? Merci à tous!

Répondre

1
// try this 
**preferences_header.xml** 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 
    <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center_vertical"> 
     <ImageView 
       android:id="@+id/titleIc" 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:src="@drawable/ic_launcher" 
       android:adjustViewBounds="true" 
       android:scaleType="fitXY"/> 

     <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="RoboGuard" 
       android:textColor="#ffffffff" 
       android:textSize="16sp" /> 

     <LinearLayout 
       android:layout_width="0dp" 
       android:layout_weight="1" 
       android:gravity="right" 
       android:layout_height="wrap_content"> 
      <ImageButton 
        android:id="@+id/imagebutton1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/ic_launcher" 
        android:adjustViewBounds="true" 
        android:scaleType="fitXY"/> 
     </LinearLayout> 
    </LinearLayout> 

    <ListView 
      android:id="@android:id/list" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1"/> 
</LinearLayout> 

**preferences.xml** 
<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 

    <CheckBoxPreference 
      android:defaultValue="true" 
      android:key="autoBack" 
      android:summaryOff="Let strange calls in" 
      android:summaryOn="Unlisted calls will be blocked" 
      android:title="Block Strange Calls" /> 

    <PreferenceCategory android:title="Blocking Options" > 
     <CheckBoxPreference 
       android:defaultValue="false" 
       android:key="smsSilence" 
       android:summaryOff="Off" 
       android:summaryOn="On" 
       android:title="Send Blocked caller message" /> 

     <PreferenceScreen android:title="More Options" > 
      <CheckBoxPreference 
        android:defaultValue="true" 
        android:key="cb21" 
        android:summaryOff="off" 
        android:summaryOn="on" 
        android:title="" /> 
      <CheckBoxPreference 
        android:defaultValue="true" 
        android:key="cb22" 
        android:summaryOff="stop" 
        android:summaryOn="stop" 
        android:title="function2" /> 
     </PreferenceScreen> 
    </PreferenceCategory> 
</PreferenceScreen> 

**MainActivity** 
public class MainActivity extends PreferenceActivity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.preferences_header); 
     addPreferencesFromResource(R.layout.preferences); 
    } 
} 

// PreferenceCategory not have RatingBar as child view so you can not declare. 
+0

Merci @Haresh, je trouve aussi la solution tout comme votre dernière nuit :) –

+0

Oh Haresh, une question, pensez-vous qu'il est impossible de mettre un RatingBar dans l'écran de préférence? Je veux dire, et si je le mets dans le, disons le fichier preferences_header.xml que vous venez de définir? Merci! –

Questions connexes