我试图将数据从PageAdapter发送到一个片段两天,但没有成功。 我使用捆绑,并发送这样的:null对象的java.lang.String android.os.Bundle.getString(java.lang.String)'参考
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ficha__completa);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
nome="Chimpanze";
try {
Bundle bundle = new Bundle();
bundle.putString("Nome_animal",nome);
Imagem_Animal imagem = new Imagem_Animal();
imagem.setArguments(bundle);
} catch (Exception e) {
Log.d(TAG_LOG,"Erro de bundle: "+e.getLocalizedMessage());
}
}
我的片段是这样的:
public class Imagem_Animal extends Fragment {
ImageView imagem_p;
static final String TAG ="Fragmento";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View Layout_imagem = inflater.inflate(R.layout.fragment_imagem__animal, container, false);
imagem_p = (ImageView) Layout_imagem.findViewById(R.id.imagem);
try {
String nomeAnimal = getArguments().getString("Nome_animal");
} catch (Exception e){
Log.d(TAG, "getStirng :"+e.getMessage());
}
return Layout_imagem;
}
}
登录:的getString:试图调用虚拟方法java.lang.String中的Android .os.BaseBundle.getString(java.lang.String)'在空对象引用
任何人都可以帮助我吗?
非常感谢你
我试图从一个PageAdapter发送数据到一个片段?为什么不使用MainActivity的上下文访问数据? –
发布并接受您的答案。这对未来的开发者会更有帮助。 – Noorul