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

SharedPreferences와 .NullPointerException

0 추천
public class MainActivity extends Activity
        implements NavigationDrawerFragment.NavigationDrawerCallbacks{

    private NavigationDrawerFragment mNavigationDrawerFragment;

    private CharSequence mTitle;

    EditText edit_Phone1, edit_Phone2,edit_Content;
    CheckBox Auto_LogIn;

    SharedPreferences setting;
    SharedPreferences.Editor editor;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getActionBar().setDisplayShowHomeEnabled(false);
        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));


        edit_Phone1 = (EditText) findViewById(R.id.EditText_phone1);
        edit_Phone2 = (EditText) findViewById(R.id.EditText_phone2);
        edit_Content = (EditText) findViewById(R.id.EditText_Content);
        Auto_LogIn = (CheckBox) findViewById(R.id.Auto_LogIn);

        setting = getSharedPreferences("setting", 0);
        editor = setting.edit();

        if (setting.getBoolean("Auto_Login_enabled", false)) {
            edit_Phone1.setText(setting.getString("Phone1", ""));
            edit_Phone2.setText(setting.getString("Phone2", ""));
            edit_Content.setText(setting.getString("Content", ""));
            Auto_LogIn.setChecked(true);
        }
        Auto_LogIn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if(isChecked){
                    String Phone1 = edit_Phone1.getText().toString();
                    String Phone2 = edit_Phone1.getText().toString();
                    String Content = edit_Content.getText().toString();

                    editor.putString("Phone1", Phone1);
                    editor.putString("Phone2", Phone2);
                    editor.putString("Content", Content);
                    editor.putBoolean("Auto_Login_enabled", true);
                    editor.commit();
                }else{
                    /**
                     * remove로 지우는것은 부분삭제
                     * clear로 지우는것은 전체 삭제 입니다
                     */
//             editor.remove("ID");
//             editor.remove("PW");
//             editor.remove("Auto_Login_enabled");
                    editor.clear();
                    editor.commit();
                }
            }
        });
    }
지금 까지 작성한 메인 엑티비티 일부분 입니다.
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.CheckBox.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)' on a null object reference
            at com.example.administrator.myapplication.MainActivity.onCreate(MainActivity.java:69)
이러한 에러가 뜨는 상태 입니다.
69번쨰 자리는
Auto_LogIn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 입니다...
리뷰즈 (480 포인트) 님이 2015년 10월 17일 질문

1개의 답변

0 추천

activity_main.xml 파일 내용도 같이 첨부되어야 조언을 들을 수 있지 않을가요?

익명사용자 님이 2015년 10월 17일 답변
<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout"
    android:layout_width="match_parent" android:layout_height="match_parent"
    tools:context=".MainActivity">

    <!-- As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions. -->
    <FrameLayout android:id="@+id/container" android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- android:layout_gravity="start" tells DrawerLayout to treat
         this as a sliding drawer on the left side for left-to-right
         languages and on the right side for right-to-left languages.
         If you're not building against API 17 or higher, use
         android:layout_gravity="left" instead. -->
    <!-- The drawer is given a fixed width in dp and extends the full height of
         the container. -->
    <fragment android:id="@+id/navigation_drawer"
        android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent"
        android:layout_gravity="start"
        android:name="com.example.administrator.myapplication.NavigationDrawerFragment"
        tools:layout="@layout/fragment_navigation_drawer" />

</android.support.v4.widget.DrawerLayout>

activity_main.xml 입니다.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.myapplication.JSON_Fragment">

    <!-- TODO: Update blank fragment layout -->

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <EditText
    android:layout_width="320dp"
    android:layout_height="50dp"
    android:inputType="phone"
    android:ems="10"
    android:id="@+id/EditText_phone1"
    android:layout_marginTop="27dp"
    android:hint="번호를 입력 해주세요"
    android:background="@drawable/edit_btn"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
            android:foregroundGravity="center"
            android:gravity="center" />
        <EditText
            android:layout_width="320dp"
            android:layout_height="50dp"
            android:inputType="phone"
            android:ems="10"
            android:id="@+id/EditText_phone2"
            android:hint="번호를 입력 해주세요"
            android:background="@drawable/edit_btn"
            android:layout_below="@+id/EditText_phone1"
            android:layout_alignStart="@+id/EditText_phone1"
            android:layout_marginTop="7dp"
            android:gravity="center" />

        <EditText
            android:layout_width="320dp"
            android:layout_height="280dp"
            android:id="@+id/EditText_Content"
            android:background="@drawable/json_content"
            android:layout_below="@+id/EditText_phone2"
            android:layout_alignStart="@+id/EditText_phone2"
            android:layout_marginTop="7dp"
            android:hint="위급상황시 알려줄 내용을 입력 해주세요" />

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="번호와 내용을 저장하시겠습니까?"
            android:id="@+id/Auto_LogIn"
            android:layout_below="@+id/EditText_Content"
            android:layout_centerHorizontal="true" />

        <Button
            android:layout_width="120dp"
            android:layout_height="55dp"
            android:text="저장"
            android:id="@+id/json_btn"
            android:layout_below="@+id/EditText_Content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="29dp"
            android:background="@drawable/send_btn"
            android:textColor="#fff" />

    </RelativeLayout>

</FrameLayout>

이쪽은 JSON_Fragment이며 여기를 참조 하시면 될듯 합니다
...