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

에러부분좀 알려주세요 ㅠㅠ

0 추천

캘린더 만들기를 했고요 캘린더 한페이지만 했을떄는 오류가없고 실행도 잘됐습니다

근데 버튼하나만 추가해서 엑티비티 두개로 만들어줬는데 오류는 안뜨는데 앱실행시 언폴츈 뜨면서 앱이 꺼집니다 ㅠㅠ 

 

 

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.samplecalendar, PID: 7908
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.samplecalendar/com.example.samplecalendar.Main2Activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.GridView.findViewById(int)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.GridView.findViewById(int)' on a null object reference
        at com.example.samplecalendar.Main2Activity.onCreate(Main2Activity.java:82)
        at android.app.Activity.performCreate(Activity.java:5990)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
        at android.app.ActivityThread.access$800(ActivityThread.java:151) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:135) 
        at android.app.ActivityThread.main(ActivityThread.java:5254) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

 

 

오류 링크를 클릭하면 밑에 tv_Date 부분이 선택이됩니다

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);



    setContentView(R.layout.activity_main);



    tvDate = (TextView)findViewById(R.id.tv_date);

    gridView = (GridView)findViewById(R.id.gridview);


 

wague (240 포인트) 님이 6월 7일 질문

2개의 답변

+1 추천
코드가 이게 끝인가요 ?
kane9529 (1,070 포인트) 님이 6월 7일 답변
public class Main2Activity extends AppCompatActivity {
    private TextView tvDate;
    private GridAdapter gridAdapter;
    private ArrayList<String> dayList;
    private GridView gridView;
    private Calendar mCal;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvDate = (TextView)findViewById(R.id.tv_date);
        gridView = (GridView)findViewById(R.id.gridview);
        long now = System.currentTimeMillis();
        final Date date = new Date(now);
        final SimpleDateFormat curYearFormat = new SimpleDateFormat("yyyy", Locale.KOREA);
        final SimpleDateFormat curMonthFormat = new SimpleDateFormat("MM", Locale.KOREA);
        final SimpleDateFormat curDayFormat = new SimpleDateFormat("dd", Locale.KOREA);

        tvDate.setText(curYearFormat.format(date) + "/" + curMonthFormat.format(date));
        dayList = new ArrayList<String>();
        dayList.add("일");
        dayList.add("월");
        dayList.add("화");
        dayList.add("수");
        dayList.add("목");
        dayList.add("금");
        dayList.add("토");
        
        mCal = Calendar.getInstance();
        mCal.set(Integer.parseInt(curYearFormat.format(date)), Integer.parseInt(curMonthFormat.format(date)) - 1, 1);
        int dayNum = mCal.get(Calendar.DAY_OF_WEEK);
        for (int i = 1; i < dayNum; i++) {
            dayList.add("");
        }
        setCalendarDate(mCal.get(Calendar.MONTH) + 1);
        gridAdapter = new GridAdapter(getApplicationContext(), dayList);
        gridView.setAdapter(gridAdapter);
    }

    private void setCalendarDate(int month) {
        mCal.set(Calendar.MONTH, month - 1);
        for (int i = 0; i < mCal.getActualMaximum(Calendar.DAY_OF_MONTH); i++) {
            dayList.add("" + (i + 1));
        }
        
    }

    private class GridAdapter extends BaseAdapter {
        private final List<String> list;
        private final LayoutInflater inflater;

public GridAdapter(Context context, List<String> list) {
            this.list = list;
            this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        
        @Override
        public int getCount() {
            return list.size();
        }
        
        
        @Override
        public String getItem(int position) {
            return list.get(position);
        }

        
        @Override
        public long getItemId(int position) {
            return position;
        }
        
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.item_calendar_gridview, parent, false);
                holder = new ViewHolder();
                holder.tvItemGridView = (TextView)convertView.findViewById(R.id.tv_item_gridview);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder)convertView.getTag();
            }
            
            holder.tvItemGridView.setText("" + getItem(position));
            mCal = Calendar.getInstance();
            Integer today = mCal.get(Calendar.DAY_OF_MONTH);
            String sToday = String.valueOf(today);
            if (sToday.equals(getItem(position))) { //오늘 day 텍스트 컬러 변경
                holder.tvItemGridView.setTextColor(getResources().getColor(R.color.color_000000));
            }
            return convertView;
        }
    }
    
    private class ViewHolder {
        TextView tvItemGridView;
    }
}
오류가나는 2번째 xml이고 첫번쨰 엑티비티는 간단한 버튼이여서 문제는 없을것이라 생각이듭니다

    <TextView
        android:id="@+id/tv_date"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center_vertical"
        android:paddingLeft="10dp"
        android:textSize="20sp" />
    
    <GridView
        android:layout_marginTop="10dp"
        android:id="@+id/gridview"
        android:layout_weight="1"
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:numColumns="7">

    </GridView>



</LinearLayout>
해결했습니다~~~~~~~~~
+1 추천
findViewById 속에 인자로 들어가는 아이디값이 R.layout.activity_main에 속해있는지 확인해보세요.
rhaps20 (5,310 포인트) 님이 6월 7일 답변
해결했습니다!! 감사합니다 위치오류였군요 ㅠㅠ
...