Android Can';无法从活动中获取片段中的bundle参数

Android Can';无法从活动中获取片段中的bundle参数,android,android-fragments,android-bundle,Android,Android Fragments,Android Bundle,我是一名非常新的android开发者(目前是PHP程序员)。我正在尝试通过遵循谷歌开发者和其他网站的各种教程来学习android 我的问题是,在我的活动中设置的片段中,我无法获取捆绑参数 以下是我的家庭活动的方法: public void SelectItem(int possition) { Fragment fragment = new FragmentImageList(); Bundle bundle = new Bundle(); if (possition &

我是一名非常新的android开发者(目前是PHP程序员)。我正在尝试通过遵循谷歌开发者和其他网站的各种教程来学习android

我的问题是,在我的活动中设置的片段中,我无法获取捆绑参数

以下是我的家庭活动的方法:

public void SelectItem(int possition) {
    Fragment fragment = new FragmentImageList();
    Bundle bundle = new Bundle();
    if (possition <= 3) {

        bundle.putString("test_string", "Hello test");
        fragment.setArguments(bundle);
        FragmentManager frgManager = getFragmentManager();
        frgManager.beginTransaction().replace(R.id.image_list_frame, fragment)
                .commit();
    }

}
字符串strtext=getArguments().getString(“测试字符串”);不起作用。我也试过了 Bundle=this.getArguments(); Log.d(TAG,bundle.getString(“test_string”);但它也不起作用。然而,若我对这些捆绑行进行注释,那个么应用程序运行良好,并没有错误。这个“test_字符串”只是一个测试参数,我正在努力使它工作。所以,我想知道我犯了什么错误,导致了这个错误

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sujit.wallpapersnepal/com.sujit.wallpapersnepal.HomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference

编辑:我正在尝试在我的Nexus 5 android版本5.1.1中运行此应用程序

考虑使用工厂方法,如中所示(靠近底部)。可以找到这样做的原因

FragmentImageList
fragment中添加以下静态方法

public static FragmentImageList newInstance(String string) {
    FragmentImageList f = new FragmentImageList();

    // Supply index input as an argument.
    Bundle args = new Bundle();
    args.putString("test_string", string);
    f.setArguments(args);

    return f;
}
并像这样修改您的
onCreate()
方法

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString("test_string");
    }
}
在您的
SelectItem
方法中

Fragment fragment = FragmentImageList.newInstance("Hello test");

你能试着换一下这条线吗

Bundle=this.getArguments()

String value=getArguments().getString(“键”); Log.i(“安卓”,值)

而且我也不确定,你是否应该得到这个包,然后扩大视图, 换一种方式,先将视图充气,然后取出包裹

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d(MainActivity.TAG,"Fragment onCreateView()");
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        String value = getArguments().getString("key");
        Log.i("ANDROID",value);

        return rootView;

    }

你也在布局中声明了片段吗?@Blackbelt,是的,我已经在我的活动\ U主页布局中声明了片段,感谢你的回复。我尝试了你的解决方案,但它抛出了相同的错误消息。我想我不能在片段的任何地方使用bundle。可能是什么问题?
Fragment fragment = FragmentImageList.newInstance("Hello test");
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d(MainActivity.TAG,"Fragment onCreateView()");
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        String value = getArguments().getString("key");
        Log.i("ANDROID",value);

        return rootView;

    }