我正在尝试使用主 - 详细信息流制作ShoppingList。 但是当我尝试保存帖子时,我在尝试从DetailFragment中的EditText中读取时会得到NullPointerException。 这是我的DetailFragment类
public class ItemDetailFragment extends Fragment{
int id = 0;
private EditText name, amount, comment;
private CheckBox done;
public static final String ARG_ITEM_ID = "item_id";
private ItemContent.Item mItem;
public ItemDetailFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments().containsKey(ARG_ITEM_ID)) {
mItem = ItemContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_item_detail, container, false);
if (mItem != null) {
name = (EditText) rootView.findViewById(R.id.editName);
name.setText(mItem.getName());
amount = (EditText) rootView.findViewById(R.id.editAmount);
amount.setText(mItem.getAmount());
comment = (EditText) rootView.findViewById(R.id.editComment);
comment.setText(mItem.getComment());
done = (CheckBox) rootView.findViewById(R.id.checkDone);
done.setChecked(mItem.getDone());
}
return rootView;
}
public void save(){
id++;
String idSave = id + "";
String nameSave = name.getText().toString();
String amountSave = amount.getText().toString();
String commentSave = comment.getText().toString();
Boolean doneSave;
if(done.isChecked())
doneSave =true;
else
doneSave = false;
mItem.setId(idSave);
mItem.setName(nameSave);
mItem.setAmount(amountSave);
mItem.setComment(commentSave);
mItem.setDone(doneSave);
ItemContent ic = new ItemContent();
ic.addItem(mItem);
}
}
这是我的DetailActivity类
public class ItemDetailActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_detail);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (savedInstanceState == null) {
Bundle arguments = new Bundle();
arguments.putString(ItemDetailFragment.ARG_ITEM_ID, getIntent().getStringExtra(ItemDetailFragment.ARG_ITEM_ID));
ItemDetailFragment fragment = new ItemDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction().add(R.id.item_detail_container, fragment).commit();
}
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_detail, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.save:
ItemDetailFragment idf = new ItemDetailFragment();
idf.save();
break;
case R.id.cancel:
this.finish();
break;
case R.id.home:
NavUtils.navigateUpTo(this,new Intent(this, ItemListActivity.class));
break;
}
return super.onOptionsItemSelected(item);
}
}
这是出问题的地方。当我在列表中选择一个对象时,我可以让EditItem显示所选列表元素中的字符串,但是我无法将EditText中的文本转换为字符串......
03-13 13:05:45.760: E/AndroidRuntime(451): FATAL EXCEPTION: main
03-13 13:05:45.760: E/AndroidRuntime(451): java.lang.NullPointerException
03-13 13:05:45.760: E/AndroidRuntime(451): at com.example.list.ItemDetailFragment.save(ItemDetailFragment.java:53)
03-13 13:05:45.760: E/AndroidRuntime(451): at com.example.list.ItemDetailActivity.onOptionsItemSelected(ItemDetailActivity.java:37)
有人可以看到我做错了什么,还是应该以其他方式做到这一点?