2017-04-16 49 views
-3

我使用的是自定義佈局從一個偏好:自定義佈局在首選項使NullPointerException異常

<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> 

當我嘗試從首片段,我得到添加一個onClick監聽器NullPointerException,因爲我無法獲得對PreferenceScreen中設置的自定義佈局的引用。我該如何解決這個問題?

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 = 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 
     } 
    }); 
} 
+0

我知道一個NullPointerException是什麼。我在這個特殊情況下要問如何避免它。 –

+0

發佈您的完整PreferenceFragment類代碼 – FAT

+0

我已經發布了整個片段 –

回答

0

嘗試使用相關的視圖來獲取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

+0

我得到了同樣的錯誤 –

+0

我編輯了答案... – rafsanahmad007

0
  1. onCreateView(),使用:

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

    ,而不是

    View view = super.onCreateView(inflater, container, savedInstanceState); 
    
  2. Move Button,EditText initialization and onClick listener code to 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){ 

    } 
}