ここでは、zip ファイルでGoogle の例のコードを使用しています。
そのまま実行すると、すべてうまくいきます。
サポート ライブラリを削除し、 FragmentActivity を Activity に変更し、 support.Fragment を Fragment に (また gertsupportFragmentManager() を FragmentManager() に) 変更し、マニフェストを api 17 を指すように変更すると、次のようになります。
<uses-sdk android:minSdkVersion="11" android:targetSdkVersion="17" />
NPE
リスト項目をクリックすると、 が表示されます。何が変わったのかわからず、R.id.記事が見つかりません。
私はレイアウト ファイルを変更しておらず、コードをあまりいじっていないので、FragmentActivity と support.Fragment の代わりに、API や Activity/Fragments のライフサイクルと関係があると推測しています。
エラーは:
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);
}
}
イプサムクラス:
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."
};
}