4

インテントを通じてリソース ID を別のクラスに渡しました。次に、インテントからエクストラを取得し、int に格納します。

getTag() を使用できるように、その int をビューなどに変換したいのですか? 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 型からのものです。したがって、レイアウトをエクストラとしてインテントに入れることができます。

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);

その後、その 2 番目のアクティビティに、ビュー ID だけでなく、この ID が意味を持つコンテキストも渡す必要があります。

そこで、最初のアクティビティで「(Context)this」をextraに入れます。

2 番目のアクティビティでコンテキストを復元した後:

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