如何给textview赋值?
<TextView
android:id="@+id/hName"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="25sp"
android:text = "@string/hName"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
这是我正在创建的视图之一的结构.现在我想将来自先前活动的值分配给hname.我能够从先前活动中获取它,但无法将其设置为textview.当我使用setText()时,它显示空指针异常.如何为该文本字段分配值.提前致谢.
这是相应的Java代码.
Intent intent = getIntent();
String host_name = intent.getStringExtra(ConnectionsActivity.HOST_NAME);
TextView tvName = (TextView)findViewById(R.id.hName);
tvName.setText(host_name);
setContentView(R.layout.sharedfiles);
解决方法:
您的结构有问题.
它给你一个空指针异常,因为它找不到文本视图…
把setContentView(R.layout.sharedfiles);高于意图…
尝试这个..
setContentView(R.layout.sharedfiles);
Intent intent = getIntent();
String host_name = intent.getStringExtra(ConnectionsActivity.HOST_NAME);
TextView tvName = (TextView)findViewById(R.id.hName);
tvName.setText(host_name);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。