大家好,我有一个问题.
我想用来getResource.getIdentifier() 获取XML格式的布局元素(layout/xxx.xml).
但我遇到"资源ID#0x7f070003类型#0x12无效..."错误消息.
我不知道如何解决这个问题.
我使用的方法是返回linearlayout.
这是我的代码如下:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWeekOfDay(2).setBackgroundColor(Color.RED);
}
private LinearLayout getWeekOfDay(int n)
{
int layoutID = getResources().getIdentifier("layout"+n, "id", getPackageName());
return (LinearLayout) LayoutInflater.from(this).inflate(layoutID, null);
}
....
...
..
}
Run Code Online (Sandbox Code Playgroud)
我的布局有很多linearlayout,但名称相似(layout1,layout2,layout3 ......).
我想使用call方法获取指定元素.
我的布局代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.37"
android:background="#cccccc" >
<TextView
android:id="@+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test1" />
</LinearLayout>
<LinearLayout
android:id="@+id/layout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.37"
android:background="#cccccc" >
<TextView
android:id="@+id/TextView02" …Run Code Online (Sandbox Code Playgroud) 我想在活动开始时显示连接的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() { …Run Code Online (Sandbox Code Playgroud)