Android-java.lang.NullPointerException错误
我的应用程序又出现了类似的问题-由于java.lang.NullPointerException错误,它不想工作 以下是出现问题的java文件:Android-java.lang.NullPointerException错误,android,nullpointerexception,Android,Nullpointerexception,我的应用程序又出现了类似的问题-由于java.lang.NullPointerException错误,它不想工作 以下是出现问题的java文件: public class ShoppingFragment extends Fragment { public static final String ITEM_ID = "com.example.shopping.item_id"; private Item item; private EditText itemNameE
public class ShoppingFragment extends Fragment {
public static final String ITEM_ID = "com.example.shopping.item_id";
private Item item;
private EditText itemNameEditText;
private EditText itemQuantityEditText;
private EditText itemCommentEditText;
private CheckBox doneCheckBox;
public static ShoppingFragment newItemFragment (UUID itemId) {
Bundle passedData = new Bundle();
passedData.putSerializable(ITEM_ID, itemId);
ShoppingFragment itemFragment = new ShoppingFragment();
itemFragment.setArguments(passedData);
return itemFragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
UUID itemId = (UUID) getArguments().getSerializable(ITEM_ID);
item = AllItems.get(getActivity()).getItem(itemId);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View theView = inflater.inflate(R.layout.fragment_list, container, false);
itemNameEditText = (EditText) theView.findViewById(R.id.itemNameEditText);
itemQuantityEditText = (EditText) theView.findViewById(R.id.itemQuantityEditText);
itemCommentEditText = (EditText) theView.findViewById(R.id.itemCommentEditText);
TextWatcher editTextWatcher = new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (itemNameEditText.hasFocus() == true) {
item.setName(s.toString());
} else if (itemQuantityEditText.hasFocus() == true) {
item.setQuantity(s.toString());
} else if (itemCommentEditText.hasFocus() == true) {
item.setComment(s.toString());
}
}
};
itemNameEditText.addTextChangedListener(editTextWatcher);
itemQuantityEditText.addTextChangedListener(editTextWatcher);
itemCommentEditText.addTextChangedListener(editTextWatcher);
doneCheckBox = (CheckBox) theView.findViewById(R.id.doneCheckBox);
doneCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
item.setDone(!item.getDone());
}
});
itemNameEditText.setText(item.getName());
itemQuantityEditText.setText(item.getQuantity());
itemCommentEditText.setText(item.getComment());
doneCheckBox.setChecked(item.getDone());
return theView;
}
}
这是产生错误的一行:
itemNameEditText.setText(item.getName());
我认为错误的存在是因为item
为null
,我正在尝试获取它的值。但是我找不到Eclipse认为它实际上是null
的原因
这是我的fragment_list.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/TableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="@dimen/component_bottom_margin" >
<EditText
android:id="@+id/itemNameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/shopping_list_hint" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/itemQuantityEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/shopping_quantity_hint" >
</EditText>
<EditText
android:id="@+id/itemCommentEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/shopping_comment_hint" >
</EditText>
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<CheckBox
android:id="@+id/doneCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/shopping_done_checkbox" />
</TableRow>
</TableLayout>
感谢您的建议。试试这个
使用以下代码
UUID itemId = (UUID) getArguments().getSerializable(ITEM_ID);
item = AllItems.get(getActivity()).getItem(itemId);
在您的
onCreateView
中,项目是空的还是视图?项目是空的,否则它在调试应用程序之前已经崩溃了一些行。调试没有多大帮助,只是第690行出现了一些抛出异常,不管它意味着什么,然后应用程序就崩溃了。