2013-08-20 2 views
0

조각을 만들었을 때 간단히 몇 가지 TextViews를 설정하려고합니다. 먼저 sharedPrefrences가 설정되었는지 또는 설정되지 않았는지 확인합니다.TextView가 Fragment Create에 설정되지 않음

내 조각 코드는 다음과 같습니다

@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 (Null 포인터 예외를) 제공 유지는 모든 곳에서 내가 시도의 setText().

왜이 기능이 작동하지 않고 어떻게 해결할 수 있습니까? ImageView에 비트 맵을 저장하고 저장하기 위해 다른 조각에서 매우 유사한 것을 복제합니다. 이 경우 finview에서보기를 만듭니다에서 호출하는

+3

아마 텍스트 뷰가 postcardback 속해있는 텍스트 뷰 객체를 반환이 병이 처음 사용해야합니다. xml이므로 backView.findViewById()를 사용해야합니다. – Blackbelt

답변

1

는 팽창 XML 뷰를 사용 backView.findViewById (...) 당신에게

+0

감사합니다! 위대한 작품, 나는 확실히 그걸 보았다. – Keeano