4

我已通过意图将我的资源 ID 传递给另一个类。然后我从意图中检索额外的内容并将其存储在一个 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);

谢谢

4

2 回答 2

2

视图来自 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);
于 2012-02-12T21:08:51.190 回答
2

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

 setContentView(R.layout.layout1);

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

因此,在第一个活动中,将“(上下文)this”放入额外的。

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

View view = (View)context.findViewByid(id); 
于 2012-02-12T21:09:32.797 回答