所以我已经超过了本书的一半, Android编程:大书呆子牧场指南,当我不得不改变时,我差不多完成了 CriminalIntent 应用程序从桌面到笔记本电脑。我只是复制了工作区并安装了eclipse和SDK。现在logcat在尝试添加新犯罪时给了我这个:
06-13 09:10:47.720: E/AndroidRuntime(535): java.lang.NullPointerException
06-13 09:10:47.720: E/AndroidRuntime(535): at com.bignerdranch.android.criminalintent.CrimeFragment.onCreateView(CrimeFragment.java:102)
06-13 09:10:47.720: E/AndroidRuntime(535): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
06-13 09:10:47.720: E/AndroidRuntime(535): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
06-13 09:10:47.720: E/AndroidRuntime(535): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
06-13 09:10:47.720: E/AndroidRuntime(535): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
06-13 09:10:47.720: E/AndroidRuntime(535): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
06-13 09:10:47.720: E/AndroidRuntime(535): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)
06-13 09:10:47.720: E/AndroidRuntime(535): at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
06-13 09:10:47.720: E/AndroidRuntime(535): at android.support.v4.view.ViewPager.populate(ViewPager.java:1072)
06-13 09:10:47.720: E/AndroidRuntime(535): at android.support.v4.view.ViewPager.populate(ViewPager.java:918)
06-13 09:10:47.720: E/AndroidRuntime(535): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1440)
CrimeFragment
处的第102行是:
getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
新支持库可能有些不同?会很奇怪,我一个月前做过我之前的设置。
另一件事是我首先安装了Android Studio和SDK,然后只是将eclipse指向该SDK,也许这就是问题所在? 代码在旧机器上完美运行,所以我不在这里发布,但如果你需要它,我可以。
编辑:
@TargetApi(11)
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
/* int resource, ViewGroup root, boolean attachToRoot */
View v = inflater.inflate(R.layout.fragment_crime, parent, false);
// enabling back button on menu bar to be shown
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
/*
* this action bar (guess back button in particular) is only compatible with holo.light
* theme, maybe some others idk. There is a way around it but it is complicated, need to
* use supportActionBar
*/
if (NavUtils.getParentActivityName(getActivity()) != null) {
getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
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());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
mDateButton = (Button) v.findViewById(R.id.crime_date);
mDateButton.setOnClickListener(new View.OnClickListener() {
/*
* standard procedure of sending info between fragments of same activity - use
* NewInstance
*/
@Override
public void onClick(View v) {
FragmentManager fm = getActivity().getSupportFragmentManager();
DatePickerFragment dialog = DatePickerFragment.newInstance(mCrime.getDate());
/*
* setting itself as a target to receive REQUEST_DATE after child is dead; first set
* up REQUEST_DATE string; used together with private void sendResult(int
* resultCode) and DialogInterface.onClickListener in positive button in
* datePickerFragment, overriding onActivityResult is necessary in here
*/
dialog.setTargetFragment(CrimeFragment.this, REQUEST_DATE);
dialog.show(fm, DIALOG_DATE);
}
});
mTimeButton = (Button) v.findViewById(R.id.crime_time);
mTimeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fm = getActivity().getSupportFragmentManager();
TimePickerFragment dialog = TimePickerFragment.newInstance(mCrime.getDate());
dialog.setTargetFragment(CrimeFragment.this, REQUEST_TIME);
dialog.show(fm, DIALOG_TIME);
}
});
mDeleteButton = (Button) v.findViewById(R.id.crime_delete);
mDeleteButton.setText(R.string.delete_crime);
mDeleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
UUID crimeId = (UUID) getArguments().getSerializable(EXTRA_CRIME_ID);
CrimeLab crimeLab = CrimeLab.get(getActivity());
Crime crime = CrimeLab.get(getActivity()).getCrime(crimeId);
crimeLab.deleteCrime(crime);
crimeLab.saveCrimes();
getActivity().finish();
}
});
mPhotoButton = (ImageButton) v.findViewById(R.id.crime_imageButton);
mPhotoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getActivity(), CrimeCameraActivity.class);
startActivity(i);
}
});
// disabling camera button for devices without camera
PackageManager pm = getActivity().getPackageManager();
if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)
&& !pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT)) {
mPhotoButton.setEnabled(false);
}
mSolvedCheckBox = (CheckBox) v.findViewById(R.id.crime_solved);
mSolvedCheckBox.setChecked(mCrime.isSolved());
mSolvedCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(
/* checkButton needs compoundButton */CompoundButton buttonView,
boolean isChecked) {
mCrime.setSolved(isChecked);
}
});
updateDate();
updateTime();
return v;
}
答案 0 :(得分:0)
它声明您的ActionBar
是null
。如果您正在使用支持库
尝试
getActivity().getSupportActionBar().setDisplayHomeAsUpEnabled(true);
而不是
getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);