gpt4 book ai didi

java - UUID 返回空指针异常

转载 作者:行者123 更新时间:2023-11-29 09:32:06 25 4
gpt4 key购买 nike

我正在编写 big nerd ranch 指南的 Android 编程的第二部分。出于某种原因,当我尝试运行该应用程序时,它在启动时崩溃,日志 cat 显示在线抛出了一个空指针异常:

UUID crimeID = (UUID)getArguments().getSerializable(EXTRA_CRIME_ID);

为什么 EXTRA_CRIME_ID 返回空值?

这是抛出异常的我的犯罪列表 fragment :

package com.bignerdranch.android.criminalintent;

import java.util.UUID;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;

public class CrimeFragment extends Fragment {
public static final String EXTRA_CRIME_ID =
"com.bignerdranch.android.criminalintent.crime_id";

private Crime mCrime;
private EditText mTitleField;
private Button mDateButton;
private CheckBox mSolvedCheckBox;

@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
**UUID crimeID = (UUID)getArguments().getSerializable(EXTRA_CRIME_ID);**
mCrime = CrimeLab.get(getActivity()).getCrime(crimeID);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
View v =inflater.inflate(R.layout.fragment_crime, parent, false);
mTitleField = (EditText)v.findViewById(R.id.crime_title);
mTitleField.setText(mCrime.getTitle());
mTitleField.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence c, int start, int before, int count){
mCrime.setTitle(c.toString());
}

public void beforeTextChanged(CharSequence c, int start, int count, int after){
//left blank
}

public void afterTextChanged(Editable c){
//this one to
}
});

mDateButton = (Button)v.findViewById(R.id.crime_date);
mDateButton.setText(DateFormat.format("hh:mm:ss", mCrime.getDate()));
mDateButton.setEnabled(false);

mSolvedCheckBox = (CheckBox)v.findViewById(R.id.crime_solved);
mSolvedCheckBox.setChecked(mCrime.isSolved());
mSolvedCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//set the crimes solved property
mCrime.setSolved(isChecked);
}
});

return v;
}

}

最佳答案

我还没有定义任何参数,所以我的 getArguments() 返回 null。我应该用

UUID crimeId = (UUID)getActivity().getIntent().getSerializableExtra(EXTRA_CRIME_ID);

相反。

----2014 年 12 月 16 日更新----

正如 njzk2 所指出的,我将 fragment 链接到 Activity ,以防止它被用于另一个 Activity 。 BigNerdRanch 书中出于简单原因将其链接到前面章节中的 Activity ,然后让读者在后面更改它并添加方法:

public static CrimeFragment newInstance(UUID crimeId){

Bundle args = new Bundle();
args.putSerializable(EXTRA_CRIME_ID, crimeId);

CrimeFragment fragment = new CrimeFragment();
fragment.setArguments(args);

//pass UUID from extra
return fragment;
}

并更改 UUID crimeId = (UUID)getActivity().getIntent().getSerializableExtra(EXTRA_CRIME_ID);到

 UUID crimeId = (UUID)getArguments().getSerializable(EXTRA_CRIME_ID);

关于java - UUID 返回空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27319366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com