eclipse FindViewById 其中 ID 是动态字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14056817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
FindViewById where ID is dynamic string
提问by jprofitt
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:
我有一个 40 个方格的表,每个方格都有一个 ID。当我从另一个活动传递 Bundle 时,我需要从那个 bundle 中提取 Note、Color 和 ID。然后应用程序将更改/添加文本并更改提取的 ID 指定的方块的背景。每个方块都有 int 格式的 ID。从其他活动传递的每个 ID 都是字符串格式。我不知道如何使它成为 findViewById(R.id.passed_id),以及如何让两种不同的格式一起工作。我试图更改每个方块的 ID,但 eclipse 说 ID 必须有一个字母和一个数字。我迷路了......这是代码:
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.access0(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
回答by jprofitt
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.
VIEW_NAME
您生成的任何标识符字符串在哪里。之后,您可以像当前一样设置它的文本。如果您还需要获取字符串和可绘制对象,这也适用,只需更改id
为适当的类型即可。
回答by DeeV
Assuming you're passing in the actual string "R.id.passed_id", I would recommend passing in the integer value instead. Thus, simply pass in R.id.passed_id.
假设您传递的是实际字符串“R.id.passed_id”,我建议您改为传递整数值。因此,只需传入 R.id.passed_id。
All values in the R file of an application are simply integers that are mapped to resource locations. The findById()
methods are only looking for integers, so if you start the Activity
and pass in the integer value of the resource you want, it will find it.
应用程序的 R 文件中的所有值都只是映射到资源位置的整数。这些findById()
方法只寻找整数,所以如果你开始Activity
并传入你想要的资源的整数值,它会找到它。
Note: This will only work if the resources are within the same application. R files remain constant for an application, but they can change from one app to another even if they have the same name. If, however, the two activities fall under the same application package, it will work.
注意:这仅在资源位于同一个应用程序中时才有效。R 文件对于应用程序保持不变,但它们可以从一个应用程序更改为另一个应用程序,即使它们具有相同的名称。但是,如果这两个活动属于同一个应用程序包,它将起作用。
回答by gezimi005
Man I'm not understating what exactly you want to do. Idis the id off an android widget then on function initialize()you should cast string to int this way: Integer.ValueOf(Id)Hope this is what you want.
伙计,我并没有低估你到底想做什么。 Id是 android 小部件的 id,然后在函数initialize() 上,您应该以这种方式将字符串转换为 int:Integer.ValueOf(Id)希望这是您想要的。