Jonno
Jonno

Reputation: 1552

Android: Resource Id into View?

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

Upvotes: 4

Views: 4245

Answers (2)

Gangnus
Gangnus

Reputation: 24464

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

Upvotes: 2

ezcoding
ezcoding

Reputation: 2994

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

Upvotes: 2

Related Questions