Android:资源 ID 进入视图?

发布于 2025-01-05 00:57:13 字数 982 浏览 0 评论 0原文

我已通过意图将资源 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风透绣罗衣 2025-01-12 00:57:13

视图来自 int 类型。因此,您可以将布局作为 Extras 放入 Intent 中:

final Intent intent = new Intent(this,Activity2.class);
intent.putExtra("layout",R.layout.mylayout);
startActivity(intent);

然后放入 Activity2 中:

Bundle bundle = getIntent().getExtras();
final int iLayout = bundle.getInt("layout");
setContentView(iLayout);

Views are from the type int. So you can put the layout as Extras in an Intent:

final Intent intent = new Intent(this,Activity2.class);
intent.putExtra("layout",R.layout.mylayout);
startActivity(intent);

And then in your Activity2:

Bundle bundle = getIntent().getExtras();
final int iLayout = bundle.getInt("layout");
setContentView(iLayout);
仲春光 2025-01-12 00:57:13

在第一个活动中,您应该将该活动连接到包含此视图的布局。

 setContentView(R.layout.layout1);

之后,您不仅必须传递给第二个活动视图 id,还要传递该 id 有意义的上下文。

因此,在第一个活动中将“(Context)this”放入 extra 中。

在第二个活动中恢复上下文后:

View view = (View)context.findViewByid(id); 

In the first activity you should connect the activity to the layout that contains this view.

 setContentView(R.layout.layout1);

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:

View view = (View)context.findViewByid(id); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文