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

Fragment 이용 시 getApplicationContext() NULL문제 입니다.

0 추천

SMS의 값들을 SQLite에 저장해서 다른 Activty로 전달해서 리스트를 만드려고 하고 있습니다. getApplicationContext()이 NULL이 왜는 이유를 모르겠습니다. intent를 넘겨서 getApplicationContext()를 이용하여 받으면 된다고 생각했는데 내가 잘 못 생각한 것 인지 궁금합니다.

public static class PlaceholderFragment extends Fragment {
        private static final String ARG_SECTION_NUMBER = "section_number";

        public PlaceholderFragment() {
        }
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            final View rootView = inflater.inflate(R.layout.fragment_main, container, false);
//            TextView textView = (TextView) rootView.findViewById(R.id.section_label);
//            textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
            Button testButton = (Button) rootView.findViewById(R.id.test_button);
            testButton.setOnClickListener(new Button.OnClickListener() {
                public void onClick(View v) {
                    Intent intent = new Intent(rootView.getContext().getApplicationContext(), TestActivity.class);
                    startActivity(intent);
                }
            });
            testButton.setBackgroundResource(R.drawable.test_card2);
            return rootView;
        }


    }

 

public class TestActivity extends MainActivity implements iCardConstant{
    private ArrayList<CardData> list;
    private ListAdapter listAdapter;
    private Context mContext;
    private View rootView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mContext = getBaseContext();
        list = getList();
        ListView lv = (ListView)rootView.findViewById(R.id.list);
        listAdapter = new ListAdapter(mContext, list, R.layout.list_item_layout);
        TextView countTv = (TextView)rootView.findViewById(R.id.list_count);
        countTv.setText("사용 내역 건수 : " + list.size());
        lv.setAdapter(listAdapter);
        // 아이템 선택시 세부내역 다이얼로그를 보여준다.
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(final AdapterView<?> parent, final View arg1, final int pos,
                                    final long arg3) {
                // TODO Auto-generated method stub
                ListAdapter adapter = (ListAdapter) parent.getAdapter();
                CardData c = (CardData) adapter.getItem(pos);
                detailDialog(c);
            }
        });
        setContentView(R.layout.all_list_layout);

        rootView=View.inflate(getApplicationContext(), R.layout.all_list_layout, null);
        //rootView.inflate(R.layout.all_list_layout, container, false);

    }

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.smartcardreader.dynamictest, PID: 11793
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.smartcardreader.dynamictest/com.smartcardreader.dynamictest.TestActivity}: java.lang.NullPointerException
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2436)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495)
                      at android.app.ActivityThread.access$900(ActivityThread.java:170)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:146)
                      at android.app.ActivityThread.main(ActivityThread.java:5635)
                      at java.lang.reflect.Method.invokeNative(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:515)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
                      at dalvik.system.NativeStart.main(Native Method)
                   Caused by: java.lang.NullPointerException
                      at com.smartcardreader.dynamictest.TestActivity.onCreate(
TestActivity.java:29)
                      at android.app.Activity.performCreate(Activity.java:5580)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2400)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2495) 
                      at android.app.ActivityThread.access$900(ActivityThread.java:170) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1304) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:146) 
                      at android.app.ActivityThread.main(ActivityThread.java:5635) 
                      at java.lang.reflect.Method.invokeNative(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:515) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107) 
                      at dalvik.system.NativeStart.main(Native Method) 

치도리이 (200 포인트) 님이 2016년 11월 25일 질문
치도리이님이 2016년 11월 25일 수정

1개의 답변

0 추천
inflation 을 위해서 getApplicationContext()  대신 activity context 를 쓰세요.
익명사용자 님이 2016년 11월 25일 답변
어느 부분에서 getApplicationContext()말하시는 건지...;; 제가 워낙 초보라...
TestActivity.this 말하시는 건가요??
...