Bundle.getString() 返回空值?
Bundle.getString() returning null?
我正在尝试通过捆绑包中的字符串传递 json。该字符串被很好地加载到包中。但看起来它收到了错误的包。
在一个class的onCreate中:
if(intent!=null){
jsonString = intent.getStringExtra(this.getBaseContext().getResources().getString(R.string.recipe_detail_json));
//prints the string just fine here
System.out.println(jsonString);
Bundle bundle = new Bundle();
bundle.putString("RECIPE_DETAIL_JSON",jsonString);
srdFragment= new SelectRecipeDetailFragment();
srdFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction()
.replace(R.id.recipe_list_step_container, srdFragment).commit();
}
setContentView(R.layout.select_a_recipe_step);
在我的片段中:
private String jsonString;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
jsonString = bundle.getString("RECIPE_DETAIL_JSON");
//this string prints null
System.out.println(jsonString);
}
您必须像这样在片段内部创建实例
public class SelectRecipeDetailFragment extends Fragment{
public static SelectRecipeDetailFragment newInstance(String jsonString) {
SelectRecipeDetailFragment frag = new SelectRecipeDetailFragment();
Bundle args = new Bundle();
args.putString("RECIPE_DETAIL_JSON", jsonString);
frag.setArguments(args);
return frag;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
jsonString = bundle.getString("RECIPE_DETAIL_JSON");
//this string prints null
System.out.println(jsonString);
}
}
然后在你的 activity
中像这样使用
getSupportFragmentManager().beginTransaction()
.replace(R.id.recipe_list_step_container, SelectRecipeDetailFragment.newInstance(jsonString).commit();
在片段中使用这个
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
jsonString = getArguments().getString("RECIPE_DETAIL_JSON");
return inflater.inflate(R.layout.fragment, container, false);
}
我正在尝试通过捆绑包中的字符串传递 json。该字符串被很好地加载到包中。但看起来它收到了错误的包。
在一个class的onCreate中:
if(intent!=null){
jsonString = intent.getStringExtra(this.getBaseContext().getResources().getString(R.string.recipe_detail_json));
//prints the string just fine here
System.out.println(jsonString);
Bundle bundle = new Bundle();
bundle.putString("RECIPE_DETAIL_JSON",jsonString);
srdFragment= new SelectRecipeDetailFragment();
srdFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction()
.replace(R.id.recipe_list_step_container, srdFragment).commit();
}
setContentView(R.layout.select_a_recipe_step);
在我的片段中:
private String jsonString;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
jsonString = bundle.getString("RECIPE_DETAIL_JSON");
//this string prints null
System.out.println(jsonString);
}
您必须像这样在片段内部创建实例
public class SelectRecipeDetailFragment extends Fragment{
public static SelectRecipeDetailFragment newInstance(String jsonString) {
SelectRecipeDetailFragment frag = new SelectRecipeDetailFragment();
Bundle args = new Bundle();
args.putString("RECIPE_DETAIL_JSON", jsonString);
frag.setArguments(args);
return frag;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
jsonString = bundle.getString("RECIPE_DETAIL_JSON");
//this string prints null
System.out.println(jsonString);
}
}
然后在你的 activity
中像这样使用 getSupportFragmentManager().beginTransaction()
.replace(R.id.recipe_list_step_container, SelectRecipeDetailFragment.newInstance(jsonString).commit();
在片段中使用这个
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
jsonString = getArguments().getString("RECIPE_DETAIL_JSON");
return inflater.inflate(R.layout.fragment, container, false);
}