-1

I want to edit TextView in Class A with data from spinner from Class B

Class A

TextView tvv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ClassA);
tvv = findViewById(R.id.tv_sel1);

public TextView getTextView() {    
        TextView tv = tvv;
            return tv;
       }
}

Class B

select1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override

    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getApplicationContext(),
"Select 1 - "    +parent.getSelectedItem().toString(),
Toast.LENGTH_LONG).show();

                ClassA txt = new ClassA();
                TextView tv = txt.getTextView();
                tv.setText(parent.getSelectedItem().toString());

            }

when I run "debug" whit prints, app force close on

tv.setText(parent.getSelectedItem().toString());

So I put the text in wrong format or is there some other mistake?

  • you will need to pass this data with a putextra from class b to class a, then get that value and simply put it inside your setText – Gastón Saillén Apr 19 '18 at 15:04
  • yet another ... where `ClassA.onCreate` is called? nowhere ... which obviously will cause NPE when you would like to use `getTextView` ... please learn some android basics: do not create `Activity/Service/Application` derived class by yourself with operator `new` – Selvin Apr 19 '18 at 15:05

1 Answers1

0

In class A u should set a method to set the text on the textvieuw:

Class A:

public void setText(String text) {    
    tvv.setText(text);
   }

Class B:

ClassA.setText(parent.getSelectedItem().toString());
Daan Seuntjens
  • 880
  • 1
  • 18
  • 37