0

我只是在创建片段时尝试设置一些 TextView,只需首先检查以确保我的 sharedPrefrences 已设置或未设置。

我的片段代码如下所示:

    @SuppressLint("ValidFragment")
    public class CardBackFragment extends Fragment {
        public CardBackFragment() {
        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            backView = inflater.inflate(R.layout.postcardback, container, false);
            TextView to = (TextView) findViewById(R.id.backToLabel);
            TextView address = (TextView) findViewById(R.id.backAddressLabel);
            TextView city = (TextView) findViewById(R.id.cityLabel);
            TextView state = (TextView) findViewById(R.id.stateLabel);
            TextView zip = (TextView) findViewById(R.id.zipLabel);
            TextView country = (TextView) findViewById(R.id.countryLabel);
            postcard = getApplicationContext().getSharedPreferences(postcardconst, 0);
            if(savedInstanceState != null){
            if(!getFreshValueForBackView()){
                postcard = getApplicationContext().getSharedPreferences(postcardconst, 0);
                toString = (postcard.getString(toConst, null));
                addressString = (postcard.getString(address1Const, null));
                cityString = (postcard.getString(cityConst, null));
                stateString = (postcard.getString(stateConst, null));
                zipString = (postcard.getString(zipcodeConst, null));
                countryString = (postcard.getString(countryConst, null));
                //setText
                to.setText(toString);
                address.setText(addressString);
                city.setText(cityString);
                state.setText(stateString);
                zip.setText(zipString);
                country.setText(countryString);
            }else{
                toString = ("add recipiants address");
                addressString = ("");
                cityString = ("");
                stateString = ("");
                zipString = ("");
                countryString = ("");
                //setText
                to.setText(toString);
                address.setText(addressString);
                city.setText(cityString);
                state.setText(stateString);
                zip.setText(zipString);
                country.setText(countryString);
            }
        }else if(!getFreshValueForBackView()){
        postcard = getApplicationContext().getSharedPreferences(postcardconst, 0);
            toString = (postcard.getString(toConst, null));
            addressString = (postcard.getString(address1Const, null));
            cityString = (postcard.getString(cityConst, null));
            stateString = (postcard.getString(stateConst, null));
            zipString = (postcard.getString(zipcodeConst, null));
            countryString = (postcard.getString(countryConst, null));
            //setText
            to.setText(toString);
            address.setText(addressString);
            city.setText(cityString);
            state.setText(stateString);
            zip.setText(zipString);
            country.setText(countryString);
        }else{
            toString = ("add recipiants address");
            addressString = ("");
            cityString = ("");
            stateString = ("");
            zipString = ("");
            countryString = ("");
        }
           return backView;
        }
    }

这一直给我一个 NPE(空指针异常),无论我在哪里尝试和 setText()。

为什么这不起作用,我该如何解决?我在另一个片段上复制了一些非常相似的东西,以获取 Bitmap 并将其保存到 ImageView。

4

1 回答 1

1

在这种情况下,您从创建视图中调用它 finview 应该与膨胀的 xml 视图一起使用使用 backView.findViewById(....) 这将开始返回 textview 对象

于 2013-08-20T09:33:16.863 回答