PreferenceFragment没有被夸大,该怎么办?

时间:2016-05-05 17:05:34

标签: android android-preferences

我有一个AppCompatActivity和一个PreferenceFragment,但在de AppCompatActivity的onCreate上,PreferenceFragment不会膨胀。代码和错误:

我不知道是否需要导入不同的内容或获取其他库或使用PreferenceFragmentCompat,但我认为它是相同的。

import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.BaseAdapter;

import com.google.analytics.tracking.android.EasyTracker;
import com.leucotron.softphone.android.R;
import com.leucotron.softphone.android.service.JService;


public class JAccountPreference extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_account_preference);//Error in here

    setSupportActionBar((Toolbar) findViewById(R.id.tool_bar));
    getSupportActionBar().setTitle("");
    getSupportActionBar().setIcon(R.drawable.app_logo);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_action_back);

    getFragmentManager().beginTransaction().replace(R.id.fragment_account_preference, new AccountPreferenceFragment()).commit();
}

public static class AccountPreferenceFragment extends PreferenceFragment implements Preference.OnPreferenceChangeListener, SharedPreferences.OnSharedPreferenceChangeListener, ServiceConnection, Runnable {

    private JService mService;
    private boolean serviceIsBind;
    private ServiceConnection mConnection = this;

    private ProgressDialog progressDialog;

    private CheckBoxPreference accountActivated;
    private CheckBoxPreference accountShowPassword;
    private EditTextPreference accountUser;
    private EditTextPreference accountPassword;
    private EditTextPreference accountDomain;
    private EditTextPreference accountPort;

    private String stringTeste;

    private SharedPreferences mSharedPreferences;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.account_preference);

        mSharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(getActivity());

        accountActivated = (CheckBoxPreference) findPreference("pref_sip_activate_register");
        accountShowPassword = (CheckBoxPreference) findPreference("pref_sip_show_password");
        accountUser = (EditTextPreference) findPreference("pref_sip_user");
        accountPassword = (EditTextPreference) findPreference("pref_sip_password");
        accountDomain = (EditTextPreference) findPreference("pref_sip_domain");
        accountPort = (EditTextPreference) findPreference("pref_sip_port");

        if (accountPort.getText() == null || accountPort.getText().equals("")) {
            accountPort.setText("5060");
        }

        setSummaryFields(accountUser, accountUser.getText());
        setSummaryFields(accountPassword, accountPassword.getText());
        setSummaryFields(accountDomain, accountDomain.getText());
        setSummaryFields(accountPort, accountPort.getText());

        //accountActivated.setOnPreferenceChangeListener(this);
        accountUser.setOnPreferenceChangeListener(this);
        accountPassword.setOnPreferenceChangeListener(this);
        accountDomain.setOnPreferenceChangeListener(this);
        accountPort.setOnPreferenceChangeListener(this);

        setFieldsEnable(!accountActivated.isChecked());
        accountActivated.setEnabled(validateValues());
        progressDialog = new ProgressDialog(getActivity());

        accountShowPassword.setChecked(false);

        if (!serviceIsBind) {
            Class<JService> serviceClass = JService.class;
            getActivity().bindService(new Intent(getActivity(), serviceClass),
                    mConnection, Context.BIND_AUTO_CREATE);
            serviceIsBind = true;
        }
    }

xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.myproject.JAccountPreference">

<include layout="@layout/toolbar" />

<fragment class="com.myproject.JAccountPreference$AccountPreferenceFragment"
    android:id="@+id/fragment_account_preference"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:padding="3dp"
    android:background="@color/primary_color"
    android:textColor="@android:color/white"
    android:textSize="12sp"
    tools:ignore="HardcodedText" />

错误:

FATAL EXCEPTION: main
                                                                             Process: com.myproject, PID: 31728
                                                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myproject/com.myproject.JAccountPreference}: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2314)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
                                                                                 at android.app.ActivityThread.access$800(ActivityThread.java:148)
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                 at android.os.Looper.loop(Looper.java:135)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5312)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
                                                                              Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
                                                                                 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
                                                                                 at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
                                                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
                                                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
                                                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
                                                                                 at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
                                                                                 at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
                                                                                 at com.leucotron.softphone.android.preference.JAccountPreference.onCreate(JAccountPreference.java:37)
                                                                                 at android.app.Activity.performCreate(Activity.java:5953)
                                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1128)
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388) 
                                                                                 at android.app.ActivityThread.access$800(ActivityThread.java:148) 
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292) 
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                 at android.os.Looper.loop(Looper.java:135) 
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5312) 
                                                                                 at java.lang.reflect.Method.invoke(Native Method) 

1 个答案:

答案 0 :(得分:0)

首先将您的preferenceFragment放在课外 第二,你可以在活动中的xml布局中添加prefrencefragment