android - 如何使用 getResource.getIdentifier() 获取布局?

标签 android user-interface android-linearlayout

我有一个问题,大家。

我想使用 getResource.getIdentifier() 在 XML (layout/xxx.xml) 中获取我的布局元素。

但我遇到“资源 ID #0x7f070003 类型 #0x12 无效...”错误消息。

我不知道如何解决这个问题。

我正在使用返回线性布局的方法。

下面是我的代码:

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

我的布局有很多linearlayout,但是名字都差不多(layout1, layout2, layout3...)。

我想使用调用方法获取指定元素。

我的布局代码如下:

<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"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="test2" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.37"
        android:background="#cccccc" >

        <TextView
            android:id="@+id/TextView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="test3" />
</LinearLayout>

<LinearLayout
    android:id="@+id/layout4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.37"
    android:background="#cccccc" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="test4" />

</LinearLayout>

但是java代码会返回错误。

错误信息如下:

      : FATAL EXCEPTION: main
     : java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testdemo/com.example.testdemo.MainActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f070003 type #0x12 is not valid
     :  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194)
     :  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
     :  at android.app.ActivityThread.access$600(ActivityThread.java:139)
     :  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
     :  at android.os.Handler.dispatchMessage(Handler.java:99)
     :  at android.os.Looper.loop(Looper.java:154)
     :  at android.app.ActivityThread.main(ActivityThread.java:4945)
     :  at java.lang.reflect.Method.invokeNative(Native Method)
     :  at java.lang.reflect.Method.invoke(Method.java:511)
     :  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
     :  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
     :  at dalvik.system.NativeStart.main(Native Method)
     : Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f070003 type #0x12 is not valid
     :  at android.content.res.Resources.loadXmlResourceParser(Resources.java:2358)
     :  at android.content.res.Resources.getLayout(Resources.java:952)
     :  at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
     :  at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
     :  at com.example.testdemo.MainActivity.getWeekOfDay(MainActivity.java:25)
     :  at com.example.testdemo.MainActivity.onCreate(MainActivity.java:19)
     :  at android.app.Activity.performCreate(Activity.java:4531)
     :  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
     :  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)
     :  ... 11 more

有人知道这个问题吗?如何解决这个问题?

最佳答案

有了这个:

int layoutID = getResources().getIdentifier("layout"+n, "layout", getPackageName());

您基本上是检索可以膨胀的布局文件的 id。是动态版的

int layoutID = R.layout.layout1;

您打算从已经膨胀的布局中检索 View 。这就是你的做法:

int layoutID = getResources().getIdentifier("layout"+n, "id", getPackageName());
return (LinearLayout)findViewById(layoutID);

这是动态版

return (LinearLayout)findViewById(R.id.layout1);

关于android - 如何使用 getResource.getIdentifier() 获取布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17203454/

相关文章:

java - 使用 FragmentPagerAdapter 从 Fragment 以编程方式更改选项卡

c++ - QtWebView - 如何启用页面滚动和页面中元素的滚动(例如谷歌地图)

c - 何时将 union 嵌套在结构中作为设计选择

java - TextView 在 LinearLayout 中被截断

android - LinearLayout 被推离屏幕

ScrollView 中线性布局的Android困难

java - 如何使用 viewPager 和 Fragments 传递多个数据

java - AsyncTask onPostExecute 在 doInBackground 之前调用

android - 在 android 2.3.3、2.3.5 中来自 gcm 的注册 ID 为 null 但它不为 null 并且在 android 2.3.4、4.0.4 中工作

java - Clojure - "Exception in thread "main“java.lang.ClassCastException : seesaw. core.proxy”