【问题标题】:Custom Layout in Preference gives Nullpointerexception [duplicate]首选项中的自定义布局给出 Nullpointerexception [重复]
【发布时间】:2017-04-16 08:43:30
【问题描述】:

我正在使用首选项中的自定义布局:

<PreferenceCategory
    android:title="Intolleranze">
    <Preference android:layout="@layout/intolerance_settings" />

</PreferenceCategory>

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
    android:id="@+id/editTextIntolerance"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:hint="Aggiungi intolleranza..." />


<Button
    android:id="@+id/addIntoleranceButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:background="@drawable/oval_button"
    android:text="@string/add"
    android:textColor="@android:color/white"
    style="?android:attr/borderlessButtonStyle"/>

</LinearLayout>

当我尝试从 Preference Fragment 添加 OnClick 侦听器时,我得到一个 NullPointerException,因为我无法获得对我在 PreferenceScreen 中设置的自定义布局的引用。我该如何解决?

偏好片段:

public class SettingsFragment extends PreferenceFragment {
    private Set<String> set;
    private Set<String> in;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = super.onCreateView(inflater, container, savedInstanceState);
    getActivity().setTitle("Impostazioni");
    return view;
}

@Override
public void onViewCreated(View view, Bundle savedIstanceState){

    Button addBtn = (Button) findViewById(R.id.addIntoleranceButton);
    final EditText editText = (EditText) findViewById(R.id.editTextIntolerance);
    addBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           //NullPointerException
         }
    });
}

【问题讨论】:

  • 我知道 NullPointerException 是什么。我在问如何在这种特殊情况下避免它。
  • 发布您的完整 PreferenceFragment 类代码
  • 我已经发布了整个片段

标签: android nullpointerexception preferencefragment


【解决方案1】:

尝试使用关联视图获取id:

 Button addBtn = (Button) view.findViewById(R.id.addIntoleranceButton);

在你的onViewCreated(View view, Bundle savedIstanceState)

编辑

将您的代码移动到onCreateView() 并扩展自定义布局:

  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.intolerance_settings, container, false);
        Button addBtn = (Button) rootView.findViewById(R.id.addIntoleranceButton);
        final EditText editText = (EditText) rootView.findViewById(R.id.editTextIntolerance);
        addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

             }
        });
        return rootView;
    }

如果这也不起作用,您可以使用PreferenceScreen 检查答案Here

【讨论】:

  • 我得到同样的错误
  • 我已经编辑了答案...
【解决方案2】:
  1. onCreateView() 中,使用:

    View view = inflater.inflate(R.layout.intolerance_settings, container, false);
    

    而不是

    View view = super.onCreateView(inflater, container, savedInstanceState);
    
  2. ButtonEditText 初始化和onClick 侦听器代码移动到onCreateView()

更新您的PreferenceFragment 如下:

public class SettingsFragment extends PreferenceFragment {
    private Set<String> set;
    private Set<String> in;

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.intolerance_settings, container, false);     
        getActivity().setTitle("Impostazioni");

        Button addBtn = (Button) view.findViewById(R.id.addIntoleranceButton);
        final EditText editText = (EditText) view.findViewById(R.id.editTextIntolerance);
        addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                 //Do something
            }
        });

        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedIstanceState){

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 2011-04-03
    相关资源
    最近更新 更多