0

我在 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."
};
}
4

2 回答 2

2

标记为已接受的答案对我不起作用。我最终在这里找到了详细的答案(http://marksunghunpark.blogspot.com/2015/04/googles-fragment-example-error.html

来自 Sunghun 的博客:

当您单击标题中的项目时,它将加载新闻内容。切换 Fragment 以在MainActivity手机中显示它,但它在桌面设备的第二个窗格中显示新闻内容。错误来自ArticleFragment课堂。当MainActivity加载两个窗格 news_articles.xml/res/layout-large,它找不到具有 ID'article'的视图,因为它试图通过调用来查找视图 getActivity().findViewById()。这不起作用。您必须 阅读'article' TextView.rootViewarticle_view.xml

于 2016-01-12T19:57:05.517 回答
1

声明TextView为班级成员

初始化它onCreateView

TextView article; 
@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);
    }
    View view =inflater.inflate(R.layout.article_view, container, false); 
     article = (TextView) view.findViewById(R.id.article);
    // Inflate the layout for this fragment
    return view;
}

findViewById查找当前 infalted 布局中提到的视图。如果没有找到,您将获得 NPE。所以infalte布局使用视图对象来初始化textview。

于 2013-11-07T18:57:33.693 回答