android将数据保存为键值集

时间:2016-05-16 07:31:03

标签: javascript java android sharedpreferences textview

晚上好,我正在创建一个应用程序,我需要帮助解决此错误。

log cat of the error:java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                               at cqdevelopers.incrediblediet.Fragment3.getAddressKeyValueFile(Fragment3.java:90)
                                                                               at cqdevelopers.incrediblediet.Fragment3.onCreateView(Fragment3.java:25)



<!-- begin snippet: js hide: false -->

<!-- language: lang-js -->

    05-16 02:25:21.711 6498-6498/cqdevelopers.incrediblediet E/AndroidRuntime: FATAL EXCEPTION: main
                                                                               Process: cqdevelopers.incrediblediet, PID: 6498
                                                                               java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                                   at cqdevelopers.incrediblediet.Fragment3.getAddressKeyValueFile(Fragment3.java:90)
                                                                                   at cqdevelopers.incrediblediet.Fragment3.onCreateView(Fragment3.java:25)
                                                                                   at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
                                                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
                                                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
                                                                                   at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
                                                                                   at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
                                                                                   at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
                                                                                   at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
                                                                                   at android.support.v4.view.ViewPager.populate(ViewPager.java:1177)
                                                                                   at android.support.v4.view.ViewPager.populate(ViewPager.java:1025)
                                                                                   at android.support.v4.view.ViewPager$3.run(ViewPager.java:254)
                                                                                   at android.support.v4.view.ViewPager.completeScroll(ViewPager.java:1920)
                                                                                   at android.support.v4.view.ViewPager.onInterceptTouchEvent(ViewPager.java:2050)
                                                                                   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2108)
                                                                                   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
                                                                                   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
                                                                                   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
                                                                                   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
                                                                                   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
                                                                                   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
                                                                                   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
                                                                                   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
                                                                                   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
                                                                                   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
                                                                                   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)
                                                                                   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
                                                                                   at com.android.internal.policy.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2403)
                                                                                   at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1737)
                                                                                   at android.app.Activity.dispatchTouchEvent(Activity.java:2765)
                                                                                   at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
                                                                                   at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
                                                                                   at com.android.internal.policy.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2364)
                                                                                   at android.view.View.dispatchPointerEvent(View.java:9514)
                                                                                   at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4230)
                                                                                   at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4096)
                                                                                   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
                                                                                   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
                                                                                   at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
                                                                                   at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3787)
                                                                                   at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
                                                                                   at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3844)
                                                                                   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
                                                                                   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
                                                                                   at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
                                                                                   at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
                                                                                   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
                                                                                   at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5922)
                                                                                   at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5896)
                                                                                at android.view.

Fragment3.java:

public class Fragment3 extends Fragment {
    private EditText editText;
    private TextView textView;
    private Button button;

    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment3_layout, container, false);

        getAddressKeyValueFile();
        editText = (EditText) v.findViewById(R.id.tvadress);
        textView = (TextView) v.findViewById(R.id.textView);
        button = (Button) v.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String address = null;

                address = editText.getText().toString();
                textView.setText(address);

                setAddressKeyValueFile(address);

            }
        });


        return v;
    }


    private void getAddressKeyValue() {
        SharedPreferences sharedPreferences = this.getActivity().getSharedPreferences("r", Context.MODE_PRIVATE);

        String key = getString(R.string.address);
        String existingAddress = sharedPreferences.getString(key, null);

        if (existingAddress != null) {

            TextView textView = (TextView) getActivity().findViewById(R.id.textView);
            textView.setText(existingAddress);

        }


    }

    private void setAddressKeyValue(String address)
    {
        SharedPreferences sharedPreferences = this.getActivity().getSharedPreferences("d" ,Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();

        String key = getString(R.string.address);
        editor.putString(key,address);

        editor.commit();


    }

    private void getAddressKeyValueFile()
    {
        Context context = getContext().getApplicationContext();

        String fileName = getString(R.string.filename);
        SharedPreferences sharedPreferences = context.getSharedPreferences(fileName,Context.MODE_PRIVATE);

        String key = getString(R.string.address);
        String existingAddress = sharedPreferences.getString(key,null);

        if(existingAddress != null)
        {
            TextView textView = (TextView) getActivity().findViewById(R.id.textView);
            textView.setText(existingAddress);
        }

    }


    private void setAddressKeyValueFile(String address)
    {
        Context context = getContext().getApplicationContext();
        String fileName = getString(R.string.filename);
        SharedPreferences sharedPreferences = context.getSharedPreferences(fileName,Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();

        String key = getString(R.string.address);
        editor.putString(key, address);

        editor.commit();
    }
}

和fragment3.xml

<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="cqdevelopers.incrediblediet.MainActivity$PlaceholderFragment">


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tvadress"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="register"
        android:id="@+id/textView2"
        android:layout_marginTop="23dp"
        android:layout_below="@+id/button"
        android:layout_centerHorizontal="true" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_below="@+id/tvadress"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:id="@+id/textView"
        android:layout_marginTop="29dp"
        android:layout_below="@+id/textView2"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

请查看异常并让我知道我在哪里做错了,感谢您的帮助。

3 个答案:

答案 0 :(得分:0)

更改getAddressKeyValueFile的位置

//getAddressKeyValueFile();
editText = (EditText) v.findViewById(R.id.tvadress);
textView = (TextView) v.findViewById(R.id.textView);
getAddressKeyValueFile();

并删除行:

TextView textView = (TextView) getActivity().findViewById(R.id.textView);
在getAddressKeyValueFile()

答案 1 :(得分:0)

这行代码textView = (TextView) v.findViewById(R.id.textView);正试图从活动的布局中获取ID为TexView的{​​{1}},而不是来自片段的布局。

你应该改变:

textView

为:

TextView textView = (TextView) getActivity().findViewById(R.id.textView);
textView.setText(existingAddress);

同时使用if(textView!=null){ textView.setText(existingAddress); } getAddressKeyValueFile()方法。

此外,在getAddressKeyValue()方法中,只有在onCreateView(...)初始化后才能致电getAddressKeyValueFile();

答案 2 :(得分:0)

在getAddressValueFile中,改变字符串的恢复方式。

private void getAddressKeyValueFile()
    {
        Context context = getContext().getApplicationContext();

        String fileName = getResources.getString(R.string.filename);
        SharedPreferences sharedPreferences = context.getSharedPreferences(fileName,Context.MODE_PRIVATE);

        String key = getResources.getString(R.string.address);
        String existingAddress = sharedPreferences.getString(key,null);

        if(existingAddress != null)
        {
            TextView textView = (TextView) getActivity().findViewById(R.id.textView);
            textView.setText(existingAddress);
        }

    }

而不是

getString(R.string.filename)

更改为

getResources.getString(R.string.filename)

另外,在setAddressKeyValue方法中进行类似的更改。