0

I'm trying to click a recyclerview item and get the clicked item's data.

In this time, i want to use a method to query a database and show the data in recyclerview.

I'm facing with a problem while method run. Method can't find RecyclerView at R.id becuse of context, i guess.

Here is my code that i try, method is in MainActivity but not in onCreate method sure.

While debugging, it stops at the first line of below code.

public class MainActivity extends AppCompatActivity {

  ...


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


mRcyView = (RecyclerView) findViewById(R.id.rv);
  ...

}
 public void sqlQuery  (String param) {

    **mRcyView = (RecyclerView)this.findViewById(R.id.rv);**
    mdbHelper = new DB_Helper(this);
    mRows = new ArrayList<>();
    mRows = mdbHelper.getListRow2(param);
    mRvAdapter = new RV_Adapter(this,mRows);
    mRcyView.setAdapter(mRvAdapter);
}
}

When disable the findViewById row, query works and get a value mRows. However Recyclerview set adapter does not work.

Here is the method first call and RecyclerView adapter code

public class RV_Adapter extends RecyclerView.Adapter<RV_Adapter.ViewHolder> {




public RV_Adapter(Context rContext, List<EachRow> rows) {

    this.rows = rows;
    this.rContext = rContext;
}




public class ViewHolder extends RecyclerView.ViewHolder {

   ...

    public ViewHolder(View v, final Context context) {

        super(v);

        this.context = context;
        ...


        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int pos = getAdapterPosition();
                EachRow eachRow = rows.get(pos);
                String kSenArab = eachRow.getkSenArab();

                MainActivity act = new MainActivity();
                act.sqlQuery(kSenArab);


            }
        });


    }

}

}

Here is the error logcat

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.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:56)
    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.mustafa.otre.MainActivity.sqlQuery(MainActivity.java:112)
    at com.mustafa.otre.RV_Adapter$ViewHolder$1.onClick(RV_Adapter.java:116)
    at android.view.View.performClick(View.java:5609)
    at android.view.View$PerformClick.run(View.java:22259)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

This is not only null exception question. it contains context reach problem

Thanks for replies.

AhuraMazda
  • 460
  • 4
  • 22

2 Answers2

1

Here :

MainActivity act = new MainActivity();
 act.sqlQuery(kSenArab);

Not right way to access methods from Activity which required Context.

Do it as using Context parameter passed in RV_Adapter :

( (MainActivity) rContext).sqlQuery(kSenArab);

And best way is create a listener using interface for updating Activity from Adapter.

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
0
 MainActivity act = new MainActivity();
 act.sqlQuery(kSenArab);

You should not try to initialize MainActivity in this manner. The reason you are facing the error is due to above attempt.

Try not reference MainActivity from your ViewHolder. If you want to handle the onClick in MainActivity then implement an interface as indicated in this SO.

Sagar
  • 23,903
  • 4
  • 62
  • 62