TextView不会在片段创建上设置

时间:2013-08-20 09:05:35

标签: android android-fragments settext

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

My Fragment代码如下所示:

    @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;
        }
    }

在我尝试和setText()的任何地方,这一直给我一个NPE(空指针异常)。

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

1 个答案:

答案 0 :(得分:1)

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