繁体   English   中英

当我尝试在TextView上设置Stringbuffer时,我的应用程序崩溃

[英]My app crashes when i try to set Stringbuffer on my TextView

这是我的代码:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    StringBuffer stringBuffer = null;
    stringBuffer.append("zain");

    TextView textView = (TextView)findViewById(R.id.textview);
    textView.setText(stringBuffer);
}
}

你需要

StringBuffer stringBuffer = new StringBuffer();

代替:

StringBuffer stringBuffer = null;

但是,我根本看不到为什么要使用StringBuffer 您可以简单地致电:

textView.setText("zain");

并从您的代码中删除stringBuffer变量。

您需要更改以下内容: StringBuffer stringBuffer = null; 使用此StringBuffer stringBuffer = new StringBuffer(); 为了将数据附加到StringBuffer

希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM