java.lang.String android.os.Bundle.getString(java.lang.String)和#x27;关于空对象引用

java.lang.String android.os.Bundle.getString(java.lang.String)和#x27;关于空对象引用,java,android,fragment,Java,Android,Fragment,我试图将数据从PageAdapter发送到一个片段两天,但没有成功。 我正在使用Bundle,并按如下方式发送: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ficha__completa); Toolbar toolbar = (Tool

我试图将数据从PageAdapter发送到一个片段两天,但没有成功。 我正在使用Bundle,并按如下方式发送:

     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;
    }
}
我的片段是这样的:

     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;
    }
}
Log:getString:尝试在空对象引用上调用虚拟方法“java.lang.String android.os.BaseBundle.getString(java.lang.String)”

有人能帮我吗


非常感谢您

在您的片段中创建函数,如下所示

 public void someFunctionName(String data){

 }
@Override
public Fragment getItem(int position) {
    Imagem_Animal ani= new Imagem_Animal();
    ani.someFunctionName("yourdata");
    return ani;
}
在寻呼机适配器中,执行如下操作

 public void someFunctionName(String data){

 }
@Override
public Fragment getItem(int position) {
    Imagem_Animal ani= new Imagem_Animal();
    ani.someFunctionName("yourdata");
    return ani;
}

如果片段在您的
维护

就这些了,实际上
页面编辑器
在。然后您可以访问
Imagem_Animal
Fragment
中的字段,如

((MainActivity) getActivity()).nome

而不是以捆绑方式传递。

我试图将数据从PageAdapter发送到片段?为什么不使用MainActivity的上下文访问数据呢?发布并接受您的答案。这将对未来的开发人员更有帮助。非常感谢,但我使用了另一种解决方案:)我尝试了这种方法,并不断返回相同的错误:/n然后检查为什么
nome
字段是空的,如果您像上面那样访问它。可能是在初始化变量之前加载的
片段
。在初始化变量后设置您的
PagerAdapter
。错误是关于上下文不正确的Toast,而不是关于获取数据!非常感谢。