2

我試圖使用android.support.v14.preference庫中的PreferenceFragment在Android應用中顯示設置。但是,當我嘗試打開首選項時,應用程序崩潰,出現一個奇怪的NullPointerException。如果我在PreferenceFragment中忽略addPreferenceFromResource(R.xml.preferences)的呼叫,則不會發生這種情況,但當然偏好從不會顯示出來。android支持v14 PreferenceFragment崩潰

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hardboard.preferencetest/com.hardboard.preferencetest.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)' on a null object reference 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442) 
      at android.app.ActivityThread.access$800(ActivityThread.java:156) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:211) 
      at android.app.ActivityThread.main(ActivityThread.java:5373) 
      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:1020) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815) 
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)' on a null object reference 
      at android.support.v14.preference.PreferenceFragment.bindPreferences(PreferenceFragment.java:473) 
      at android.support.v14.preference.PreferenceFragment.onActivityCreated(PreferenceFragment.java:278) 
      at android.app.Fragment.performActivityCreated(Fragment.java:2061) 
      at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:912) 
      at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) 
      at android.app.BackStackRecord.run(BackStackRecord.java:834) 
      at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452) 
      at android.app.Activity.performStart(Activity.java:6005) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2342) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442) 
            at android.app.ActivityThread.access$800(ActivityThread.java:156) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:211) 
            at android.app.ActivityThread.main(ActivityThread.java:5373) 
            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:1020) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815) 

我讀過的支持-V14 PreferenceFragment使用RecyclerView內部,以顯示首選項列表,但我在做什麼錯誤導致RecyclerView是null

我的主要活動:

public class MainActivity extends AppCompatActivity implements SettingsFragment.OnFragmentInteractionListener { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     getFragmentManager().beginTransaction() 
       .replace(android.R.id.content, new SettingsFragment()) 
       .addToBackStack(null) 
       .commit(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    @Override 
    public void onFragmentInteraction(Uri uri) { 

    } 
} 

的PreferenceFragment:

public class SettingsFragment extends android.support.v14.preference.PreferenceFragment { 

    private OnFragmentInteractionListener mListener; 

    public SettingsFragment() { 
     // Required empty public constructor 
    } 

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

    } 

    @Override 
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { 
     addPreferencesFromResource(R.xml.preferences); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.fragment_settings, container, false); 
    } 

    // TODO: Rename method, update argument and hook method into UI event 
    public void onButtonPressed(Uri uri) { 
     if (mListener != null) { 
      mListener.onFragmentInteraction(uri); 
     } 
    } 

    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
     try { 
      mListener = (OnFragmentInteractionListener) context; 
     } catch (ClassCastException e) { 
      throw new ClassCastException(context.toString() 
        + " must implement OnFragmentInteractionListener"); 
     } 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
     mListener = null; 
    } 

    public interface OnFragmentInteractionListener { 
     public void onFragmentInteraction(Uri uri); 
    } 

} 
+0

請爲'fragment_settings'發佈'xml' – Tauqir

回答

7

A PreferenceFragment已經膨脹適當的佈局使用 - 通過使用您自己的R.layout.fragment_settings,它無法找到它需要的相應視圖。

如果您想修改RecyclerView本身,請考慮覆蓋onCreateRecyclerView()。相反,如果您想在默認佈局周圍創建一些自定義框架,可以使用如下模式:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    final View view = inflater.inflate(R.layout.fragment_settings, container, false); 
    // The frame you want to embed the parent layout in. 
    final ViewGroup innerContainer = (ViewGroup) view.findViewById(R.id.main_frame); 
    final View innerView = super.onCreateView(inflater, innerContainer, savedInstanceState); 
    if (innerView != null) { 
     innerContainer.addView(innerView); 
    } 
    return view; 
} 
0

評論你的onCreateView()方法。

或者,鏈接到超類實現onCreateView(),並將其響應嵌入到自己的包裝中,如果由於某種原因,您希望圍繞股票PreferenceFragment UI添加其他內容。

PreferenceFragment可能期望其onCreateView()創建RecyclerView,這是您的堆棧跟蹤中缺少的。