마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

GridLayout문제입니다... NullPointerException..이에요..

0 추천
public class AdminMainActivity extends Activity {

   private String row = "";
   private String column = "";
   private GridLayout gl;
   int totalRoom = 0, floor= 0;

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.login_activity_layout);

      Intent intent  = getIntent();
      row = intent.getStringExtra("row");
      column = intent.getStringExtra("column");

      Log.d("gngngn", row);
      Log.d("gngngn", column);

      gl = new GridLayout(this);
      gl.setLayoutParams(new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

      gl = (GridLayout)findViewById(R.id.gdl_admin_room);
      gl.setRowCount(Integer.parseInt(row));
      gl.setColumnCount(Integer.parseInt(column));

      totalRoom = Integer.parseInt(row) * Integer.parseInt(column);

      Button btn[] = new Button[totalRoom];
      floor = 100;

      for(int i = 0; i < totalRoom; i++){
         btn[i] =  new Button(this);
         if(i / Integer.parseInt(column) == 0)
            floor += 100;
         btn[i].setText(floor + i + "호");
         btn[i].setId(i);
      }

   }
 
다른 Activity에서 정수값을 받아와서 Integer로 변환하여 집어넣는데 자꾸 NullPointerException이 납니다... 
 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.GridLayout.setRowCount(int)' on a null object reference
            at org.springframework.android.basicauth.AdminMainActivity.onCreate(AdminMainActivity.java:54)
 
Log를 찍어보면 값은 정상적으로 잘 들어옵니다..
제가 Layout을 Java Code로는 처음짜봐서 ㅠㅠ.... 경험이 풍부하신 선배님들의 조언을 구합니다...
익명사용자 님이 2015년 12월 14일 질문

1개의 답변

0 추천
gl.setLayoutParams(new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

정확하게는 안봐서 잘 모르겠지만 아마 이 부분이 문제가 생길수 있습니다.

일단 GridLayout의 상위 view를 사용해서 layoutParams를 설정해야 합니다.

예를 들어 R.layout.login_activity_layout.xml 에서 GridLayout이 <LinearLayout> 안에 들어가있다면

LinearLayout.LayoutParams를 사용해서 설정해야 합니다.

MATCH_PARENT도 마찬가지입니다. LinearLayout.MATCH_PARENT 이런식으로 사용해야 합니다.

도움이 되었으면 좋겠네요~

 

캬옹이 (37,920 포인트) 님이 2015년 12월 15일 답변
한번 해보겠습니다 답변 감사합니다 ㅠㅠㅠ
그리고 Linear > ScrollView > GridLayout상태입니다..
...