从不同 class 编辑 TextView 时强制关闭
force close when editing TextView from different class
我想使用 Class B[= 中微调器的数据编辑 Class A 中的 TextView 14=]
Class一个
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;
}
}
ClassB
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" 打印时,应用程序强制关闭
tv.setText(parent.getSelectedItem().toString());
所以我输入的文本格式不对还是有其他错误?
在class中你应该设置一个方法来设置textview上的文本:
Class答:
public void setText(String text) {
tvv.setText(text);
}
Class乙:
ClassA.setText(parent.getSelectedItem().toString());
我想使用 Class B[= 中微调器的数据编辑 Class A 中的 TextView 14=]
Class一个
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;
}
}
ClassB
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" 打印时,应用程序强制关闭
tv.setText(parent.getSelectedItem().toString());
所以我输入的文本格式不对还是有其他错误?
在class中你应该设置一个方法来设置textview上的文本:
Class答:
public void setText(String text) {
tvv.setText(text);
}
Class乙:
ClassA.setText(parent.getSelectedItem().toString());