使用Controller / MVC初始化RecyclerView

时间:2019-06-14 17:42:48

标签: java android android-recyclerview

当前,我正在使用Android中的RecyclerView来显示从Firebase获取的一些数据。我正在使用MVC设计模式,因此必须将一些数据从视图(actViewDB)传递到controller(Controller),然后传递给Model类(DataModel)。不幸的是,我总是收到这样的Java NullPointerException错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.ryzen.pajakcerdas, PID: 5559
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
        at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:117)
        at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:149)
        at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:29)
        at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:54)
        at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:31)
        at android.support.v7.app.AppCompatDelegateImplN.<init>(AppCompatDelegateImplN.java:31)
        at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:198)
        at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:183)
        at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
        at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
        at com.example.ryzen.pajakcerdas.actViewDB.showDatabase(actViewDB.java:23)

我的想法是,如果代码是从不同的类(如实现MVC架构)执行的,则代码无法初始化RecyclerView。 这是代码:

actViewDB.java

public class actViewDB extends AppCompatActivity {

    private List<DataModel> modelList;
    private RecyclerView rc;
    private dataAdapter adapter;

    private actViewDB mMainActivity;

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

        Controller.getData();
    }

    public void showDatabase(List<DataModel> mUsername){

        modelList = new ArrayList<>();
        this.modelList = mUsername;
        Log.d("ISI_SHOWDB", "" + modelList.toString());

        this.rc = findViewById(R.id.recycler_view);
        rc.setLayoutManager(new LinearLayoutManager(this));
        rc.setHasFixedSize(true);

        adapter = new dataAdapter();
        adapter.setNotes(modelList);
        rc.setAdapter(adapter);
    }
}

Controller.java

public class Controller {
    public static void getData(){
        DataModel.getData();
    }

    public static void ifSuccess(List<DataModel> mUsername){
        final actViewDB avd = new actViewDB();
        avd.showDatabase(mUsername);
    }
}

DataModel.java

public class DataModel {
    public static void getData() {
        FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference myRef = database.getReference("dataObjek");
        List<DataModel> mUsername = new ArrayList<>();

        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                mUsername.clear();
                for(DataSnapshot ds : dataSnapshot.getChildren()) {
                    String alamat = ds.child("alamat").getValue(String.class);
                    long harga = (long) ds.child("harga").getValue();
                    long njkp = (long) ds.child("njkp").getValue();
                    Integer pbb = ds.child("pbb").getValue(Integer.class);
                    Log.d("TAG", alamat + " / " + harga);
                    mUsername.add(new DataModel(alamat, harga, njkp, pbb));

                    Controller.ifSuccess(mUsername);
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Controller.ifFail();
            }
        });
    }
}

recycle_viewdatabase.xml

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:listitem = "@layout/card_viewdatabase"/>

</android.support.design.widget.CoordinatorLayout>

此问题是我在Recycle View doesn't show data from adapter提出的第一个问题的更新问题 也许这是一个关于NullPointerException的普通问题,但是这个问题是关于在Android上实现MVC的,因此,如果这个问题已经提出,我感到抱歉。

1 个答案:

答案 0 :(得分:0)

经过大量工作,我发现此行中的错误

final actViewDB avd = new actViewDB();

您以错误的方式重新创建活动,并且未调用onCreate方法,因此您有N P异常。

您必须做什么:

onCreate中:

@Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recycle_viewdatabase);
        Controller controller = new Controller(this);
        controller.getData();
    }

Controller

 public class Controller {
      actViewDB activity;
     public  void Controller(actViewDB activity){
            this.activity = activity;
        }
        public static void getData(){
            DataModel.getData();
        }

        public static void ifSuccess(List<DataModel> mUsername){

            activity.showDatabase(mUsername);
        }
    }

注意1 :您在Android中使用 MVP 代替了 MVC

注意2::不要使用大量的静态方法,它会使内存在湖中

注意3:使用驼峰式命名约定