Android - java.lang.NullPointerException错误

时间:2014-02-05 10:05:00

标签: 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 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());

我认为错误存在是因为itemnull并且我正试图获得它的价值。但我无法找到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>

任何建议表示赞赏。

1 个答案:

答案 0 :(得分:0)

试试这个..

使用以下代码

UUID itemId = (UUID) getArguments().getSerializable(ITEM_ID);

item = AllItems.get(getActivity()).getItem(itemId);

onCreateView