FindViewById其中ID是动态的字符串 [英] FindViewById where ID is dynamic string

查看:784
本文介绍了FindViewById其中ID是动态的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个40平方的表,每个都有一个ID。当我从另一个活动通过捆绑我需要提取注意,颜色和ID从该包。然后应用程序将改变/添加文字和改变由提取ID指定的广场背景。每一方拥有的INT格式ID。从其他活动通过每个ID是字符串格式。我不知道如何使它findViewById(R.id.passed_id),以及如何获得两种不同的格式一起工作。我试图改变每平方米的ID,但日食说,ID必须有一个字母以及数字。我迷路了....这里的code:

 包com.tt;

 进口android.os.Bundle;
 进口android.app.Activity;
 进口android.content.Intent;
进口android.content.res.Resources;
进口android.util.TypedValue;
进口android.view.Gravity;
进口android.view.Menu;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.TextView;

公共类MainActivity扩展活动
 {
字符串gotNotes;
字符串n;
字符串gotDOW;
字符串gotID;
公共字符串ID;
字符串gotHour;
TextView的说明;
@覆盖
公共无效的onCreate(包savedInstanceState)
{
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    初始化();
    按键设置=(按钮)findViewById(R.id.settings);

    捆绑gotPackage = getIntent()getExtras()。
    如果(gotPackage!= NULL){
    gotNotes = gotPackage.getString(AddedNote);
   //如果(gotNotes.equals()){N =空;}其他N = gotNotes;
    // gotDOW = gotPackage.getString(日);
    // gotHour = gotPackage.getInt(一小时);
    gotID = gotPackage.getString(ID);

    n = gotID;
    notes.setText(gotNotes +(\ N)+ gotID);

    }
    其他{}



  settings.setOnClickListener(新OnClickListener()
    {



          公共无效的onClick(视图v)
         {
              意图I =新的意图(v.getContext(),Settings.class);
              startActivityForResult(ⅰ,0);
         }

     });

   }

  私人无效初始化()
       {
    // TODO自动生成方法存根


    注释=(TextView中)findViewById(R.id。);
    notes.setGravity(Gravity.CENTER);
    notes.setTextSize(TypedValue.COMPLEX_UNIT_SP,12);
}



    @覆盖
            公共布尔onCreateOptionsMenu(菜单菜单)
             {
               。getMenuInflater()膨胀(R.menu.activity_main,菜单);
               返回true;

             }

      }
 

更新

 确定..这里的日志:

12-27 16:18:55.661:D / AndroidRuntime(12497):关闭虚拟机
12-27 16:18:55.661:W / dalvikvm(12497):主题ID = 1:螺纹退出与未捕获的异常(组= 0x40abf228)
12-27 16:18:55.671:E / AndroidRuntime(12497):致命异常:主要
12-27 16:18:55.671:E / AndroidRuntime(12497):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.tt/com.tt.MainActivity}:显示java.lang.NullPointerException
12-27 16:18:55.671:E / AndroidRuntime(12497):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194)
12-27 16:18:55.671:E / AndroidRuntime(12497):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
12-27 16:18:55.671:E / AndroidRuntime(12497):在android.app.ActivityThread.access $ 600(ActivityThread.java:139)
12-27 16:18:55.671:E / AndroidRuntime(12497):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1261)
12-27 16:18:55.671:E / AndroidRuntime(12497):在android.os.Handler.dispatchMessage(Handler.java:99)
12-27 16:18:55.671:E / AndroidRuntime(12497):在android.os.Looper.loop(Looper.java:154)
12-27 16:18:55.671:E / AndroidRuntime(12497):在android.app.ActivityThread.main(ActivityThread.java:4944)
12-27 16:18:55.671:E / AndroidRuntime(12497):在java.lang.reflect.Method.invokeNative(本机方法)
12-27 16:18:55.671:E / AndroidRuntime(12497):在java.lang.reflect.Method.invoke(Method.java:511)
12-27 16:18:55.671:E / AndroidRuntime(12497):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)
12-27 16:18:55.671:E / AndroidRuntime(12497):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-27 16:18:55.671:E / AndroidRuntime(12497):在dalvik.system.NativeStart.main(本机方法)
12-27 16:18:55.671:E / AndroidRuntime(12497):由:显示java.lang.NullPointerException
12-27 16:18:55.671:E / AndroidRuntime(12497):在com.tt.MainActivity.initialize(MainActivity.java:69)
12-27 16:18:55.671:E / AndroidRuntime(12497):在com.tt.MainActivity.onCreate(MainActivity.java:30)
12-27 16:18:55.671:E / AndroidRuntime(12497):在android.app.Activity.performCreate(Activity.java:4531)
12-27 16:18:55.671:E / AndroidRuntime(12497):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
12-27 16:18:55.671:E / AndroidRuntime(12497):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)
12-27 16:18:55.671:E / AndroidRuntime(12497):11 ...更多
 

解决方案

您可以通过使用从一个字符串的标识符:

 笔记=(TextView中)findViewById(getResources()则getIdentifier(VIEW_NAME,ID,getPackageName()));
 

其中, VIEW_NAME 是什么标识字符串您正在生成。在这之后,你可以设置它的文字像你现在是。这也适用,如果你需要得到字符串和可绘为好,只是改变 ID 为适当的类型。

I have a table of 40 squares and each has an ID. When I'm passing Bundle from another activity I need to extract Note, Color and ID from that bundle. And then the app will change/add text and change background of the square specified by extracted ID. Each square has ID of int format. Each ID passed from other activity is in string format. I can't figure out how to make it to findViewById(R.id.passed_id), and how to get two different formats working together. I've tried to change ID of each square but eclipse says that ID have to have a letter along with a number. I'm lost....Here's the code:

package com.tt;

 import android.os.Bundle;
 import android.app.Activity;
 import android.content.Intent;
import android.content.res.Resources;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity
 {
String gotNotes;
String n;
String gotDOW;
String gotID;
public String Id;
String gotHour;
TextView notes;
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initialize();
    Button settings = (Button)findViewById(R.id.settings);

    Bundle gotPackage = getIntent().getExtras();
    if (gotPackage != null){
    gotNotes = gotPackage.getString("AddedNote");
   // if (gotNotes.equals(" ")){n = "Empty";}else n = gotNotes;
    //gotDOW = gotPackage.getString("Day");
    //gotHour = gotPackage.getInt("Hour");
    gotID = gotPackage.getString("ID");

    Id = gotID;
    notes.setText(gotNotes + (" \n") + gotID);

    }
    else{}



  settings.setOnClickListener(new OnClickListener()
    {



          public void onClick(View v)
         {
              Intent i = new Intent(v.getContext(),Settings.class);
              startActivityForResult(i,0);
         }

     });

   }

  private void initialize() 
       {
    // TODO Auto-generated method stub


    notes = (TextView)findViewById(R.id.);
    notes.setGravity(Gravity.CENTER);
    notes.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
}



    @Override
            public boolean onCreateOptionsMenu(Menu menu) 
             {
               getMenuInflater().inflate(R.menu.activity_main, menu);
               return true;

             }

      }

UPDATE

Ok.. here's the log:

12-27 16:18:55.661: D/AndroidRuntime(12497): Shutting down VM
12-27 16:18:55.661: W/dalvikvm(12497): threadid=1: thread exiting with uncaught exception (group=0x40abf228)
12-27 16:18:55.671: E/AndroidRuntime(12497): FATAL EXCEPTION: main
12-27 16:18:55.671: E/AndroidRuntime(12497): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tt/com.tt.MainActivity}: java.lang.NullPointerException
12-27 16:18:55.671: E/AndroidRuntime(12497):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at android.app.ActivityThread.access$600(ActivityThread.java:139)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at android.os.Looper.loop(Looper.java:154)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at android.app.ActivityThread.main(ActivityThread.java:4944)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at java.lang.reflect.Method.invokeNative(Native Method)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at java.lang.reflect.Method.invoke(Method.java:511)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at dalvik.system.NativeStart.main(Native Method)
12-27 16:18:55.671: E/AndroidRuntime(12497): Caused by: java.lang.NullPointerException
12-27 16:18:55.671: E/AndroidRuntime(12497):    at com.tt.MainActivity.initialize(MainActivity.java:69)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at com.tt.MainActivity.onCreate(MainActivity.java:30)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at android.app.Activity.performCreate(Activity.java:4531)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
12-27 16:18:55.671: E/AndroidRuntime(12497):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)
12-27 16:18:55.671: E/AndroidRuntime(12497):    ... 11 more

解决方案

You can get an identifier from a string by using:

notes = (TextView)findViewById(getResources().getIdentifier(VIEW_NAME, "id", getPackageName()));

Where VIEW_NAME is whatever identifier string you're generating. After that, you can set the text of it like you currently are. This also works if you need to get strings and drawables as well, just change id to the appropriate type.

这篇关于FindViewById其中ID是动态的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆