从不同的类编辑TextView时强制关闭

时间:2018-04-19 15:02:37

标签: java android textview settext forceclose

我想在 A类中使用来自 B类的微调器的数据编辑TextView

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;
       }
}

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

            }

当我运行“debug”whit print时,app force close on

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

所以我把文字格式错误或是否有其他错误?

1 个答案:

答案 0 :(得分:0)

在A类中你应该设置一个方法来设置textvieuw上的文字:

A类:

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

B类:

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