javaandroidtextviewsettextforceclose

force close when editing TextView from different class


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?


Solution

  • 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());