Android:资源ID进入视图?

时间:2012-02-12 20:51:41

标签: android android-intent int android-resources

我已将我的资源ID通过意图传递给另一个类。然后我从intent中检索额外的内容并将其存储在int中。

现在我想将int转换为视图或其他东西,以便我可以使用getTag()?我尝试将它分配给ImageView,但不断获取NullPointer

通过:

             int resourceId = v.getId(); 

             Intent intent = new Intent(FetchMenu.this,FetchContent.class);
             intent.putExtra("ResourceId",resourceId);  
             startActivity(intent);       

收到:

             int id;

             Intent callingIntent = getIntent();
             int getView= callingIntent.getIntExtra("ResourceId", 1); 
             id = getView;

打印到logcat:

System.out.println("Resource ID: " + id);

Logcat:"Resource ID: 2131099660"

这给了我NullPointer:

             View v = (View)findViewById(id);                

             String str=(String) v.getTag();

             System.out.println("Tag : " + str);

由于

2 个答案:

答案 0 :(得分:1)

视图来自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);

答案 1 :(得分:0)

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

 setContentView(R.layout.layout1);

之后你必须传递给第二个活动,不仅要查看id,还要传递这个id有意义的上下文。

所以,在第一个活动中将“(Context)this”放入额外的。

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

View view = (View)context.findViewByid(id); 
相关问题