0

I try to edit my TextViews from another class.

It is a RecyclerView Adapter. When I click on "delete" an server gets deleted. Also some buttons and text should get invisible on the UI.

protected MainActivity context;

public ContactsAdapter(Context context){
    this.context = (MainActivity) context;
}

public void onBindViewHolder(ContactsAdapter.ViewHolder viewHolder,final int position) {

...

final Handler mHandler = new Handler();

new Thread(new Runnable() {
@Override
public void run () {

mHandler.post(new Runnable() {
@Override
public void run () {
TextView commandrun = (TextView) context.findViewById(R.id.command_run);         
commandrun.setVisibility(View.INVISIBLE);
}
});
}
}).start();

Actually I get java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View com.minvercraft.minvercraftfree.MainActivity.findViewById(int)' on a null object reference.

Karl
  • 11
  • 3
  • first of all, is `command_run` created in your `context`? if it is not there, calling findViewById here will produce NullPointer. because each context (Activity, Dialog,...) will instantiate their own elements when they are created. if you calling a view which is not created here will cause exception – Amir Hedieh Jan 04 '19 at 19:38
  • Oh no... it was the button in front of the TextView... Thanks @Amas. – Karl Jan 04 '19 at 19:51
  • was problem fixed? – Amir Hedieh Jan 04 '19 at 19:53
  • @Amas May it is an problem with the setuped context? I have the same problem with the correct TextView. – Karl Jan 04 '19 at 20:03

2 Answers2

0

You don't need a handler. you can pass the interface to the adapter and listen to interface in your activity(other class that you mentioned) and then do a change to your textView.

public interface ChangeVisibility {
void change(boolean visibility);
}

then pass this interface to your adapter

public ContactsAdapter(Context context,ChangeVisibility 
changeVisibility){
this.context = (MainActivity) context;
this.changeVisibility = changeVisibility;
}

then when you click the delete in your adapter you must call

changeVisibility.change(true);

then in the other class that you pass the interface, you can listen to the change method and set visibility as you want

Mehrdad Pedramfar
  • 10,941
  • 4
  • 38
  • 59
Masoud Darzi
  • 640
  • 9
  • 16
  • I didn't worked with that before. Could you expand your answer? – Karl Jan 04 '19 at 19:20
  • sure man. when you are creating your adapter in the other class you will create like this `adapter = new ContactsAdapter(context, new ChangeVisibility() { @Override public void change(boolean visibility) { // set visibility here } }); – Masoud Darzi Jan 04 '19 at 19:26
  • At `this.changeVisibility = changeVisibility;` I am getting cannot resolve symbol. – Karl Jan 04 '19 at 19:26
  • you must define the variable first `private ChangeVisibility changeVisibility;` in your adapter – Masoud Darzi Jan 04 '19 at 19:28
  • problem solved or still have a problem man? – Masoud Darzi Jan 04 '19 at 19:35
  • Alright. And where I finally change the TextView visibility? I need to setup something in the MainActivity, right? – Karl Jan 04 '19 at 19:40
  • yes man. when you are initializing the adapter constructor you need to write this adapter = new ContactsAdapter(context, new ChangeVisibility() { @Override public void change(boolean visibility) { // change the TextView visibility here } }); – Masoud Darzi Jan 04 '19 at 19:43
  • Seems like it is a problem with the context. Getting the nullpointer exception aswell. But thank you for the great work. – Karl Jan 04 '19 at 20:07
  • no problem, if you send whole activity and adapter code, I can help you – Masoud Darzi Jan 04 '19 at 20:17
0

it is clearly saying that your View is null. so you are calling findViewById in wrong place, you must call that method on the context that creates that view.

it seems that view is not located in your context(e.g Activity).

Amir Hedieh
  • 1,120
  • 1
  • 14
  • 28