본문 바로가기
Front-end/Android (안드로이드 앱 개발)

Android studio (안드로이드 스튜디오) 전화번호부 만들기 2 (객체화), 3 (Glide)

by javapp 자바앱 2020. 3. 3.
728x90

코드 짜다가 에러 발견,, 흔한 널 포인터 에러다

 

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference
        at com.example.telbook.MainActivity.onCreate(MainActivity.java:37)

 

선언만하고 객체 생성을 하지 않았었다.

phoneBookList = new ArrayList<>(); 객체 생성 해주면 끝

 

 


결과

전화번호부 addView

 

public class MainActivity extends AppCompatActivity {
    ArrayList<PhoneBook> phoneBookList;                     //객체 담을 리스트
    LayoutInflater layoutInflater;
    LinearLayout container;
    View view;
    Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;                                                  //메인엑티비티 컨텍스트

        phoneBookList = new ArrayList<>();
        PhoneBook phoneBook1 = new PhoneBook(R.drawable.ha1, "영하영", "010-0000-0000");
        PhoneBook phoneBook2 = new PhoneBook(R.drawable.ha2, "일하영", "010-1111-1111");
        PhoneBook phoneBook3 = new PhoneBook(R.drawable.ha3, "이하영", "010-2222-2222");
        PhoneBook phoneBook4 = new PhoneBook(R.drawable.ha4, "삼하영", "010-3333-3333");
        PhoneBook phoneBook5 = new PhoneBook(R.drawable.ha5, "송하영", "010-4444-4444");

        phoneBookList.add(phoneBook1);
        phoneBookList.add(phoneBook2);
        phoneBookList.add(phoneBook3);
        phoneBookList.add(phoneBook4);
        phoneBookList.add(phoneBook5);

        container = findViewById(R.id.container );  
        layoutInflater = LayoutInflater.from(this);  //그릴곳

        for(int i = 0; i < (phoneBookList.size()); i++) {
            view = layoutInflater.inflate(R.layout.layout_complex, null, false);      //넣을 레이아웃 뷰에 넣음
            //사진
            ImageView imageView = view.findViewById(R.id.item_image);
            imageView.setImageResource(phoneBookList.get(i).getImage());
            //이름
            TextView nameText = view.findViewById(R.id.item_name);
            nameText.setText(phoneBookList.get(i).getName());
            //번호
            TextView phoneText = view.findViewById(R.id.item_phonenum);
            phoneText.setText(phoneBookList.get(i).getNum());
            container.addView(view);                                                  //만들어진 뷰를 컨테이너(메인xml)에 넣는다
        }
    }
}

 

 

 <PhoneBook.java>

public class PhoneBook {
    private int image;
    private String name;
    private String num;

    PhoneBook(int image, String name, String num)
    {
        this.image = image;
        this.name = name;
        this.num = num;
    }

    public int getImage(){return image;}
    public String getName(){return name;}
    public String getNum(){return num;}
}

xml 동일

 

 

 

 


Glide 사용, 사진 부분 circleCrop 적용

 

for(int i = 0; i < (phoneBookList.size()); i++) {
            view = layoutInflater.inflate(R.layout.layout_complex, null, false);
            //사진
//          imageView.setImageResource(phoneBookList.get(i).getImage());
            ImageView imageView = view.findViewById(R.id.item_image);
            RequestOptions cropOptions = new RequestOptions();
            Glide.with(this)
                    .load(phoneBookList.get(i).getImage())//int
                    .apply(cropOptions.optionalCircleCrop())
                    .into(imageView);
            //이름
            TextView nameText = view.findViewById(R.id.item_name);
            nameText.setText(phoneBookList.get(i).getName());

            //번호
            TextView phoneText = view.findViewById(R.id.item_phonenum);
            phoneText.setText(phoneBookList.get(i).getNum());
            container.addView(view);
        }

댓글