简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM