小编Lak*_*uri的帖子

PreferenceFragmentCompat自定义布局

我需要PreferenceFragmentCompat的自定义布局.在PreferenceFragmentCompat的文档中,您似乎可以在onCreateView()中膨胀并返回一个视图.

然而,NPE结果: -

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.v7.preference.PreferenceFragmentCompat.bindPreferences(PreferenceFragmentCompat.java:511)
                                                                       at android.support.v7.preference.PreferenceFragmentCompat.onActivityCreated(PreferenceFragmentCompat.java:316)
                                                                       at com.cls.example.MyPrefFrag.onActivityCreated(MyPrefFrag.java:42) 
Run Code Online (Sandbox Code Playgroud)

在我检查了PreferenceFragmentCompat:onCreateView的源代码后,我发现了以下代码: -

 RecyclerView listView = this.onCreateRecyclerView(themedInflater, listContainer, savedInstanceState);
 if(listView == null) {
    throw new RuntimeException("Could not create RecyclerView");
 } else {
    this.mList = listView;   //problem
    ...
    return view;
 }
Run Code Online (Sandbox Code Playgroud)

因此,如果覆盖onCreateView()并返回自定义布局,则不会调用onCreateRecyclerView(),并且不会设置RecyclerView私有字段mList.因此,setAdapter()上的NPE结果.

我是否应该假设自定义布局对PreferenceFragmentCompat不可行?

android android-support-library preferencefragment

12
推荐指数
1
解决办法
5202
查看次数

使用Handler Android

哪个是使用处理程序的更好方法.任何优点.我遇到的所有示例似乎都给出了内联版本.

Handler.Callback在类中使用implements 并实现接口方法.

要么

使用内联代码版本

private Handler mHandler = new Handler(){ ....};
Run Code Online (Sandbox Code Playgroud)

android handler

8
推荐指数
1
解决办法
2万
查看次数

Android MVP策略

我正在将我的应用程序迁移到MVP.已经从这个konmik上获得了静态演示者模式的提示

这是我简短的MVP策略.为简洁起见,删除了大部分样板和MVP监听器.这个策略帮助我定位变更证明我的后台流程.与完成活动的暂停相比,活动正常恢复正常暂停.此外,Presenter只有应用程序上下文,因此它不会保留活动上下文.

我不是一名java专家,这是我第一次涉足MVP并使用静态演示者让我感到不舒服.我错过了什么吗?我的应用程序工作正常,响应速度更快.

视图

public class MainActivity extends Activity{
    private static Presenter presenter;

    protected void onResume() {
        if (presenter == null)
            presenter = new Presenter(this.getApplicationContext());
        presenter.onSetView(this);
        presenter.onResume();
    }

    protected void onPause() {
        presenter.onSetView(null);
        if(isFinishing())presenter.onPause();
    }    
}
Run Code Online (Sandbox Code Playgroud)

主持人

public class Presenter {
    private MainActivity view;
    Context context;
    public Model model;

    public Presenter(Context context) {
        this.context = context;
        model = new Model(context);
    }

    public void onSetView(MainActivity view) {
        this.view = view;
    }

    public void onResume(){
        model.resume();
    }
    public void onPause(){ …
Run Code Online (Sandbox Code Playgroud)

mvp android

6
推荐指数
1
解决办法
3143
查看次数

RecyclerView wrap_content

layout_widthwrap_content而没有成功时,我试图将RecyclerView作为中心

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_schemes"
        android:scrollbars="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

当RecyclerView给出任何明确的layout_width,例如200dp时,它确实居中,否则它只是左对齐.

如何在其layout_width为wrap_content时使RecyclerView为center_horizo​​ntal?

xml android android-layout android-studio android-recyclerview

3
推荐指数
2
解决办法
2万
查看次数