0

I am displaying some data in a recycler listview based on retrofit post back data.It is working,Now I am trying to show a Bottom sheet dialog on Listview ItemClick.For this I have implemented item click event in my onBindViewHolder in DataAdapter class. Below is the code in my DataAdapter.java

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.MyViewHolder>{

private Context context;
private List<Datum> dataList;

public DataAdapter(Context context, List<Datum> dataList) {
    this.context = context;
    this.dataList = dataList;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_movie2,parent,false);
    return new MyViewHolder(view);
}


@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
    holder.tvtitle.setText(dataList.get(position).getCar_name());
    holder.tvFare.setText(dataList.get(position).getTotal_fare());
    holder.tvBreakdown.setText("Base Fare "+dataList.get(position).getBase_fare()+dataList.get(position).getPer_km());
    holder.tvCount.setText(dataList.get(position).getCount());

    Picasso.with(context)
            .load(dataList.get(position).getUrl())
            .placeholder(R.drawable.user)
            .error(R.drawable.user)
            .into(holder.list_image);

    holder.itemView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        //Toast.makeText(context, dataList.get(position).getTotal_fare(), Toast.LENGTH_SHORT).show();

        /***bottom sheet ********************************************************/


        final BottomSheetDialog dialog3 = new BottomSheetDialog((Activity)context);
        dialog3.setContentView(R.layout.hire_now);

        final TextView tvMainFare = (TextView) dialog3.findViewById(R.id.tvMainFare);
        final TextView tvRegularFare = (TextView) dialog3.findViewById(R.id.tvRegularFare);
        final Button btn_hire_now = (Button) dialog3.findViewById(R.id.btn_hire_now);

        final TextView tvDriver = (TextView) dialog3.findViewById(R.id.tvDriver);
        final TextView tvKMaway = (TextView) dialog3.findViewById(R.id.tvKMaway);

        final TextView tvDelete = (TextView) dialog3.findViewById(R.id.tvDelete);
        final TextView tvReport = (TextView) dialog3.findViewById(R.id.tvReport);
        final TextView tvPosition = (TextView) dialog3.findViewById(R.id.tvPosition);
        dialog3.show();

       }
    });

}

On Item click it is giving below error

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.lorry9.rides, PID: 13121
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
    at android.view.ViewRootImpl.setView(ViewRootImpl.java:540)
    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
    at android.app.Dialog.show(Dialog.java:286)
    at com.lorry9.rides.DataAdapter$1.onClick(DataAdapter.java:79)
    at android.view.View.performClick(View.java:4438)
    at android.view.View$PerformClick.run(View.java:18422)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5017)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    at dalvik.system.NativeStart.main(Native Method)
    I/Process: Sending signal. PID: 13121 SIG: 9
    Process 13121 terminated.

But As I toast message on item click it is working but not showing the bottom sheet dialog.How can i resolve this pls help

in my Activity I am calling the dataadapter on retrofit callback

Call<List<Datum>> call= requestInterface.getTopRatedMovies(data);
    call.enqueue(new Callback<List<Datum>>() {
        @Override
        public void onResponse(Call<List<Datum>> call, Response<List<Datum>> response) {
            dataArrayList = response.body();
            Log.e(TAG, "onResponse: "+dataArrayList );
            dataAdapter=new DataAdapter(getApplicationContext(),dataArrayList);
            recyclerView.setAdapter(dataAdapter);
        }

        @Override
        public void onFailure(Call<List<Datum>> call, Throwable t) {
            Log.e("Error",t.getMessage());
        }

    });
roy
  • 131
  • 1
  • 10
  • 1
    That stack trace would seem to indicate that you instantiated your `DataAdapter` with something other than the current `Activity` for the `Context`; e.g., `getApplicationContext()`, `getBaseContext()`, etc. (That doesn't quite line up with the given code, though, as I would expect it to crash at the `Activity` cast.) You need to use the current `Activity` instead for the first argument in your `new DataAdapter(...)` calls, if you're not already. – Mike M. May 30 '20 at 10:24
  • I have included the activity code that i am using to call the dataadapter but not understanding where is the mistake – roy May 30 '20 at 10:44
  • 1
    Replace `getApplicationContext()` with `YourActivity.this`. – Mike M. May 30 '20 at 10:44
  • 1
    Oh thanks its working now – roy May 30 '20 at 10:48

0 Answers0