Setting value to textview in android

android, textview

Solution

There is problem with your structure..

its giving you a null-pointer exception because it can't find text-view...

put `setContentView(R.layout.sharedfiles);` above intent...

try this..

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

Problem

How to assign a value to a 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> ``` This is the structure of one of the views I am creating. Now I want to assign value which comes from the previous activity to hname .I am able to get it from the previous activity but I am not able to set it to the textview. When I use setText(), it shows null pointer exception. How can I assign value to that text field. Thanks in advance. This is the corresponding java code. ``` 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); ```

Original source