0

조각에 SharedPreferences를 구현하려고합니다. 로그인 한 사용자의 세부 정보를 표시하려고합니다. 그러나 계정 단편에 표시하는 데 문제가 있습니다. MainActivity에 SharedPreferences를 표시 할 때 아무런 문제가 없습니다. 나는 코드를 어떻게 처리 할 것인가에 대한 해결책을 찾고있다. 그래서 조각에서 작동 할 것이다. 이 AccountFragment에서조각 내의 SharedPreferences

if(!SharedPrefManager.getInstance(this).isLoggedIn()){ 
      finish(); 
      startActivity(new Intent(this, LoginActivity.class)); 
     } 
     textviewUsername = (TextView)findViewById(R.id.usernameLabel); 
     textviewUsername.setText(SharedPrefManager.getInstance(this).getUsername()); 

MainActivity.java 나는 계정 세부 사항을 표시하려합니다.

public class AccountFragment extends Fragment { 
private TextView textViewUsername, textViewEmail, textViewFirstName, textViewLastName; 

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

    if(!SharedPrefManager.getInstance(getContext()).isLoggedIn()) { 
     startActivity(new Intent(getContext(), LoginActivity.class)); 
    } 

     textViewUsername = (TextView) getView().findViewById(R.id.editTextUsername); 
     textViewEmail = (TextView) getView().findViewById(R.id.editTextEmail); 
     textViewFirstName = (TextView) getView().findViewById(R.id.editTextFirstName); 
     textViewLastName = (TextView) getView().findViewById(R.id.editTextLastName); 


     textViewUsername.setText(SharedPrefManager.getInstance(getActivity()).getUsername()); 
     textViewEmail.setText(SharedPrefManager.getInstance(getActivity()).getEmail()); 
     textViewFirstName.setText(SharedPrefManager.getInstance(getActivity()).getFirstName()); 
     textViewLastName.setText(SharedPrefManager.getInstance(getActivity()).getLastName()); 

} 

답변

0

AccountFragment.java 당신은 당신의 onCreateView에 연결할 수없는 코드가 - 당신은 첫 번째 줄에서보기 객체를 반환한다. 당신은 다음과 같은 것을 가지고 있어야합니다 :

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

    if(!SharedPrefManager.getInstance(getContext()).isLoggedIn()) { 
     startActivity(new Intent(getContext(), LoginActivity.class)); 
    } 

    textViewUsername = (TextView) view.findViewById(R.id.editTextUsername); 
    textViewEmail = (TextView) view.findViewById(R.id.editTextEmail); 
    textViewFirstName = (TextView) view.findViewById(R.id.editTextFirstName); 
    textViewLastName = (TextView) view.findViewById(R.id.editTextLastName); 

    textViewUsername.setText(SharedPrefManager.getInstance(getActivity()).getUsername()); 
    textViewEmail.setText(SharedPrefManager.getInstance(getActivity()).getEmail()); 
    textViewFirstName.setText(SharedPrefManager.getInstance(getActivity()).getFirstName()); 
    textViewLastName.setText(SharedPrefManager.getInstance(getActivity()).getLastName()); 

    return view; 
} 
0

잘 작동 할 수도 있습니다. 내가 가진 조각에 저장 :

getActivity().getSharedPreferences(PREFS_NAME,MODE_PRIVATE) 
           .edit() 
           .putString(PREF_EMAIL, email) 
           .putString(PREF_PASSWORD, password) 
           .apply(); 

는 그럼으로 mainactivity에 그들을 얻을 :

SharedPreferences pref = getSharedPreferences(PREFS_NAME, MODE_PRIVATE); 
    String email = pref.getString(PREF_EMAIL, null); 
    String password = pref.getString(PREF_PASSWORD, null); 

가 반대로 작동하지 말아야하는 이유가 표시되지 않습니다.

0

당신이 정말로 달리 할 필요가없는 나는 기본 공유 설정을 사용하는 것이 좋습니다 것 :

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getContext()); 
String storedValue = sharedPrefs.getString("key", "defaultValue"); 
: 값을 검색하기 위해 다음

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getContext()); 
SharedPreferences.Editor editor = sharedPrefs.edit(); 

editor.putString("key", "value"); 
editor.apply(); 

관련 문제