Search code examples
androidandroid-layoutviewfindviewbyid

findViewByID returns null after setting same id to the view


I am inflating layout in onCreate() which is already declared in my activity:

layout = (RelativeLayout) findViewById(R.id.layout);

Assume id is declared as an int in my activity.

Later, after some event I am adding an imageview to this layout

id = (int) Calendar.getInstance().getTimeInMillis();
ImageView imageView = new ImageView(context);
imageView.setImageResource(R.drawable.ic_launcher);    
imageView.setId(id);
layout.addView(imageView);

Later somewhere, I want to get imageView from the id we have set earlier:

ImageView imageView = (ImageView) findViewById(id);
if (imageView == null)
    Log.e("Test", "imageview is null");

All code runs successfully and Imageview always returns null as printed in log.

Note: I can't able to keep the object of imageview itself because I have many number of different views in real project. Here I have described my problem using single imageview. I have stored all the ids of the views but can't able to get all those views using findViewById(). The reason why I have used Calendar.getInstance().getTimeInMillis() to generate id is because I want a unique id everytime. I have stored all the layout data including ids for later use. If user again opens this activity anytime, he will get from where he left off. So while adding any new imageview the id must not be repeated which is generated earlier.

Keypoint: If I set the device date-time 2-3 or more days earlier then it is worknig properly. I think the issue is with generating the id using calendar.


Solution

  • Finally, I got the solution using try and error. As I have mentioned that it was working properly before 2 days, I have decided to debug using the id generated and casted from long to int by decreasing the date every time. Assume counter is declared as an int in activity.

                ImageView imageView = new ImageView(context);
                imageView.setImageResource(R.drawable.ic_launcher);
                Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) - counter);
                counter++;
                String date = getDate(calendar.getTimeInMillis(), "dd/MM/yyyy hh:mm:ss.SSS");
                Log.e("Test", "Date :" + date);
                int id = (int) calendar.getTimeInMillis();
                Log.e("Test", "ID:" + id);
                imageView.setId(id);
                layout.addView(imageView);
                imageView = (ImageView) findViewById(id);
                if (imageView == null)
                    Log.e("Test", "imageview is null");
                else
                    Log.e("Test", "not null");
    

    Logs:

    03-17 23:13:44.952: E/Test(1469): Date :17/03/2016 11:13:44.952
    
    03-17 23:13:44.952: E/Test(1469): ID:-2018055688
    
    03-17 23:13:44.952: E/Test(1469): imageview is null
    
    03-17 23:13:48.744: E/Test(1469): Date :16/03/2016 11:13:48.745
    
    03-17 23:13:48.744: E/Test(1469): ID:-2104451895
    
    03-17 23:13:48.748: E/Test(1469): imageview is null
    
    03-17 23:13:53.376: E/Test(1469): Date :15/03/2016 11:13:53.380
    
    03-17 23:13:53.376: E/Test(1469): ID:2104120036
    
    03-17 23:13:53.376: E/Test(1469): not null
    

    Here, I found that I get negative int from last two days and before that I am getting positive int. As mentioned in the doc that you should set id to positive int. Then I have converted same id to absolute value using:

    id = Math.abs(id);

    and all works fine.

    Finally, I am wondering why setId() is not giving exception if set to non-positive int. They should give exception as they have mentioned that it should be postive int so any developer come to know what is the problem.