我想從fragmentActivity回到一個片段,但它不工作,我得到NullPointerException,當我打開取消我需要更新以前的片段。我如何回到片段並更新它?不知道爲什麼會發生這種情況。回到從fragmentActivity在Android中的一個片段
public class AddEscolas extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addescola);
mSessao = new SecurePreferences(AddEscolas.this, "sessao");
pesquisa = (EditText) findViewById(R.id.campo_pesquisa);
nomeEscola = (TextView) findViewById(R.id.nomeEscola);
logradouro = (TextView) findViewById(R.id.endereco);
cidade = (TextView) findViewById(R.id.cidade);
procura = pesquisa.getText();
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(pesquisa.getWindowToken(), 0);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
findViewById(R.id.btnProcurar).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Pesquisa().execute();
mFerramentas.hideKeyboard(AddEscolas.this, pesquisa);
}
});
findViewById(R.id.btnCancelar).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
}
});
}
}
FragmentClass:
public class EscolasFragment extends Fragment {
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_escolas, container, false);
ImageView add = (ImageView) rootView.findViewById(R.id.add);
mSessao = new SecurePreferences(getActivity(), "sessao");
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent addIntent = new Intent(getActivity(), AddEscolas.class);
startActivity(addIntent);
}
});
return rootView;
}
}
有你在做什麼沒有意義的......你應該通過有關片段 –
Android的文檔閱讀,你可以請提供空指針異常 – Vikas
的堆棧跟蹤你想從活動開始片段內。這很令人困惑,因爲片段是活動的一部分,它如何啓動它。片段可以使用回調與活動進行對話。 – Vikas