android – 如何动态设置文本到TextView?

我想在活动开始时显示连接的ssid和ip地址.它包含一个图像视图(用于标题,因为我没有使用操作栏)和4个文本视图(ssid标签,ssid值,ipaddr标签和ipaddr值)

我已经测试了获取ssid和ipaddr的代码,但它们运行良好.问题是我无法将它们显示在TextView中.

在此代码中,没有错误,但是当活动开始,程序崩溃或停止工作时.

这是我的java代码:

import android.app.Activity;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;

public class ConnectionInfoActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    TextView ssid = (TextView) findViewById (R.id.ssid);
    TextView ip = (TextView) findViewById (R.id.ipaddr);

    ssid.setText(getSsid());
    ip.setText(getIpAddr());

    setContentView(R.layout.activity_connection_info);
}   

// Get the connected network SSID
private String getSsid() {
    WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    String ssid = null;

    ssid = wifiInfo.getSSID();

    return ssid;
}

// Get the network IP Address
private String getIpAddr() {
    WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    int ip = wifiInfo.getIpAddress();

    String ipString = null;
    ipString = String.format(
        "%d.%d.%d.%d",
        (ip & 0xff),
        (ip >> 8 & 0xff),
        (ip >> 16 & 0xff),
        (ip >> 24 & 0xff));

    return ipString;
    }
}

和布局xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/coninfo_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:orientation="vertical" >

<ImageView
    android:id="@+id/title_conInfo"
    android:layout_width="fill_parent"
    android:layout_height="44.5dp"
    android:src="@drawable/header_coninfo" />

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:textSize="20sp"
            android:text="SSID" />
        <TextView
            android:id="@+id/ssid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:textSize="20sp"
            android:text="" />
    </TableRow>

    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:textSize="20sp"
            android:text="IP Address" />
        <TextView
            android:id="@+id/ipaddr"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:textSize="20sp"
            android:text="" />
    </TableRow>
</TableLayout>

解决方法:

您在查找视图后设置内容视图.您应该先设置内容视图.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_connection_info);
    TextView ssid = (TextView) findViewById (R.id.ssid);
    TextView ip = (TextView) findViewById (R.id.ipaddr);

    ssid.setText(getSsid());
    ip.setText(getIpAddr());
} 
上一篇:android – textView.setText();崩溃


下一篇:java – label.setText NullPointerException