첫번째 프로젝트에서는 프로젝트 생성하고 layout을 변경했으니

두번째 프로젝트에서는 Toylist를 보여주는 것을 한다.


Excercise 에서 Todo 를 확인한다.

만들던 프로젝트에서 쭉 해도 되고

Excercise를 수정해도 된다.

만들던 프로젝트에는 Todo가 표시되지 않으니 알아서 선택.


MainActivity 에 ToDo1

    // TODO (1) Declare a TextView variable called mToysListTextView
    private TextView mToysListTextView;


activity_main.xml 에 ToDo2 tv_toy_names 추가

before

 after

 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:padding="16dp"
    android:textSize="20sp" />

 <TextView
    android:id="@+id/tv_toy_names"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="16dp"
    android:textSize="20sp" />


MainActivity 에 ToDo3

onCreate method 에서 Textview와 mToysListTextView 연결


// TODO (3) Use findViewById to get a reference to the TextView from the layout

mToysListTextView = (TextView) findViewById(R.id.tv_toy_names);


// TODO (4) Use the static ToyBox.getToyNames method and store the names in a String array

String[] toyNames = ToyBox.getToyNames();


그런데 Excercise에서 작업을 하면 문제가 없으나

기존 프로젝트에서 작업하는 경우에는 ToyBox class가 없으므로 Error가 난다.

ToyBox라는 class를 만들거나 복사해 온다.

package com.example.nobang.favoritetoys;

public final class ToyBox {

    public static String[] getToyNames() {
        return new String[] {
                "Red Toy Wagon",
                "Chemistry Set",
               
                "Squirt Guns",
                "Miniature Replica Animals Stuffed with Beads that you swore to your parents would be worth lots of money one day",
                "Creepy Gremlin Doll",
                "Neodymium-Magnet Toy"
        };
    }


에러가 없으면 다음 Todo

// TODO (5) Loop through each toy and append the name to the TextView (add \n for spacing)

for (String toyName : toyNames) {
    mToysListTextView.append(toyName + "\n\n\n");
}


ToyBox class에 있는 장난감이름들을 불러와서

TextView에 일렬로 쭉(for문으로) 붙이는 거다.



todo 3을 진행하지 않으면 mToysListTextView가 초기화되어있지 않기 떄문에 아래와 같이 에러가 난다.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nobang.favoritetoys/com.example.nobang.favoritetoys.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.append(java.lang.CharSequence)' on a null object reference
 

MainActivity

- solution 프로젝트에는 TODO가 COMPLETED로 되어있음.

- todo를 진행했으면 COMPLETED로 바꿔주는 것이 좋음.

결과

"\n\n\n"으로 줄바꿈이 됨.


728x90
BLOG main image
"그게 뭐 어쨌다는 거냐?" 늘 누가 나에게 나에대한 말을할 때면 이말을 기억해라. by nobang

카테고리

nobang이야기 (1933)
Life With Gopro (7)
Life With Mini (79)
Diary (971)
너 그거 아니(do you know) (162)
난 그래 (159)
Study (290)
속지말자 (10)
Project (34)
Poem (15)
Song (0)
Photo (113)
낙서장 (45)
일정 (0)
C.A.P.i (2)
PodCast (0)
nobang (27)
고한친구들 (4)
recieve (0)
History (0)
android_app (2)

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

Total :
Today : Yesterday :