我遇到過這樣的問題。似乎我做的一切正確,但收到的參數值是null
。希望得到一些幫助。片段和活動之間傳遞的參數爲空
它需要在GridView中傳遞項目的索引。 這裏的片段類:
public class BoardsFragment extends Fragment{
private GridView gridView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_main, container, false);
gridView = (GridView) v.findViewById(R.id.gridview);
return v;
}
@Override
public void onResume(){
super.onResume();
gridView.setAdapter(new MyAdapter(getActivity()));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent myIntent = new Intent(view.getContext(), InfoActivity.class);
myIntent.putExtra("ItemID", i); //Optional parameters
startActivity(myIntent);
}
});
}
我需要通過這個ItemID
。這裏就是我收到:
public class InfoActivity extends Activity {
String itemID;
TextView brand;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info);
brand = (TextView) findViewById(R.id.textViewBrand);
itemID = this.getIntent().getStringExtra("ItemID");
brand.setText(itemID);
}
}
不知道爲什麼,但這個itemID
爲null,在這個類。有任何想法嗎?
片段託管在哪裏?您的活動是否存在並被召回 – madlymad