我在 zip 文件中使用来自 Google 示例的代码。
当我按原样运行它时,一切都很顺利。
如果我删除支持库并将 FragmentActivity 更改为 Activity 并将 support.Fragment 更改为 Fragment (也将 gertsupportFragmentManager() 更改为 FragmentManager() )并且清单指向 api 17,如下所示:
<uses-sdk android:minSdkVersion="11" android:targetSdkVersion="17" />
NPE
当我单击列表项时,我得到了一个。我不明白发生了什么变化,也找不到 R.id.article。
由于我没有更改布局文件,并且我没有对代码造成太大影响,我猜它与 API 和/或 Activity/Fragments 的生命周期有关,而不是 FragmentActivity 和 support.Fragment 。
错误是:
11-07 18:30:20.397: E/AndroidRuntime(1266): FATAL EXCEPTION: main
11-07 18:30:20.397: E/AndroidRuntime(1266): java.lang.NullPointerException
11-07 18:30:20.397: E/AndroidRuntime(1266): at com.example.android.fragments.ArticleFragment.updateArticleView(ArticleFragment.java:63)
11-07 18:30:20.397: E/AndroidRuntime(1266): at com.example.android.fragments.MainActivity.onArticleSelected(MainActivity.java:70)
11-07 18:30:20.397: E/AndroidRuntime(1266): at com.example.android.fragments.HeadlinesFragment.onListItemClick(HeadlinesFragment.java:75)
ArticleFragment 代码在这里:
public class ArticleFragment extends android.app.Fragment {
final static String ARG_POSITION = "position";
int mCurrentPosition = -1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// If activity recreated (such as from screen rotate), restore
// the previous article selection set by onSaveInstanceState().
// This is primarily necessary when in the two-pane layout.
if (savedInstanceState != null) {
mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
}
// Inflate the layout for this fragment
return inflater.inflate(R.layout.article_view, container, false);
}
@Override
public void onStart() {
super.onStart();
// During startup, check if there are arguments passed to the fragment.
// onStart is a good place to do this because the layout has already been
// applied to the fragment at this point so we can safely call the method
// below that sets the article text.
Bundle args = getArguments();
if (args != null) {
// Set article based on argument passed in
updateArticleView(args.getInt(ARG_POSITION));
} else if (mCurrentPosition != -1) {
// Set article based on saved instance state defined during onCreateView
updateArticleView(mCurrentPosition);
}
}
public void updateArticleView(int position) {
TextView article = (TextView) getActivity().findViewById(R.id.article);
article.setText(Ipsum.Articles[position]);
mCurrentPosition = position;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Save the current article selection in case we need to recreate the fragment
outState.putInt(ARG_POSITION, mCurrentPosition);
}
}
Ipsum 类:
package com.example.android.fragments;
public class Ipsum {
static String[] Headlines = {
"Article One",
"Article Two"
};
static String[] Articles = {
"Article One\n\nExcepteur pour-over occaecat squid biodiesel umami gastropub, nulla laborum salvia dreamcatcher fanny pack. Ullamco culpa retro ea, trust fund excepteur eiusmod direct trade banksy nisi lo-fi cray messenger bag. Nesciunt esse carles selvage put a bird on it gluten-free, wes anderson ut trust fund twee occupy viral. Laboris small batch scenester pork belly, leggings ut farm-to-table aliquip yr nostrud iphone viral next level. Craft beer dreamcatcher pinterest truffaut ethnic, authentic brunch. Esse single-origin coffee banksy do next level tempor. Velit synth dreamcatcher, magna shoreditch in american apparel messenger bag narwhal PBR ennui farm-to-table.",
"Article Two\n\nVinyl williamsburg non velit, master cleanse four loko banh mi. Enim kogi keytar trust fund pop-up portland gentrify. Non ea typewriter dolore deserunt Austin. Ad magna ethical kogi mixtape next level. Aliqua pork belly thundercats, ut pop-up tattooed dreamcatcher kogi accusamus photo booth irony portland. Semiotics brunch ut locavore irure, enim etsy laborum stumptown carles gentrify post-ironic cray. Butcher 3 wolf moon blog synth, vegan carles odd future."
};
}