Android:资源 ID 进入视图?
我已通过意图将资源 ID 传递给另一个类。然后,我从意图中检索额外内容并将其存储在 int 中。
现在我想将该 int 转换为视图或其他内容,以便我可以使用 getTag()?我尝试将它分配给 ImageView 但不断收到 NullPointer
Passed:
int resourceId = v.getId();
Intent intent = new Intent(FetchMenu.this,FetchContent.class);
intent.putExtra("ResourceId",resourceId);
startActivity(intent);
Received:
int id;
Intent callingIntent = getIntent();
int getView= callingIntent.getIntExtra("ResourceId", 1);
id = getView;
This prints to logcat:
System.out.println("Resource ID: " + id);
Logcat:"Resource ID: 2131099660"
This is Give me NullPointer:
View v = (View)findViewById(id);
String str=(String) v.getTag();
System.out.println("Tag : " + str);
谢谢
I have passed my resource ID through intent to another class. I then retrieve the extra from the intent and store it in an int.
Now I want to get that int to convert into a view or something so that I can use getTag()? I tried assigning it to an ImageView but kept getting NullPointer
Passed:
int resourceId = v.getId();
Intent intent = new Intent(FetchMenu.this,FetchContent.class);
intent.putExtra("ResourceId",resourceId);
startActivity(intent);
Received:
int id;
Intent callingIntent = getIntent();
int getView= callingIntent.getIntExtra("ResourceId", 1);
id = getView;
This prints to logcat:
System.out.println("Resource ID: " + id);
Logcat:"Resource ID: 2131099660"
This is giving me NullPointer:
View v = (View)findViewById(id);
String str=(String) v.getTag();
System.out.println("Tag : " + str);
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
视图来自 int 类型。因此,您可以将布局作为 Extras 放入 Intent 中:
然后放入 Activity2 中:
Views are from the type int. So you can put the layout as Extras in an Intent:
And then in your Activity2:
在第一个活动中,您应该将该活动连接到包含此视图的布局。
之后,您不仅必须传递给第二个活动视图 id,还要传递该 id 有意义的上下文。
因此,在第一个活动中将“(Context)this”放入 extra 中。
在第二个活动中恢复上下文后:
In the first activity you should connect the activity to the layout that contains this view.
After that you have to pass to that second activity not only view id, but the context, in which this id has sense.
So, in the first activity put "(Context)this" into extra.
After restoring the context in the second activity: