Android 错误:无法从片段转换为文章片段



我是Android编程的新手,目前正在从以下链接学习片段教程:http://www.youtube.com/watch?v=D88gtGD5bJA/http://www.newthinktank.com/2013/08/android-development-17/

但是,我在此行遇到了一个错误:

ArticleFragment articleFrag =(ArticleFragment)
getSupportFragmentManager().findFragmentById(R.id.article_fragment);

错误消息:无法从片段转换为ArticleFragment

主活动.java:

    public void onArticleSelected(int position) {
    // TODO Auto-generated method stub
    // Capture the article fragment from the activity layout
    ArticleFragment articleFrag = (ArticleFragment)
            getSupportFragmentManager().findFragmentById(R.id.article_fragment);
    // If the article fragment is here we're in the two pane layout
    if (articleFrag != null) {
        // Get the ArticleFragment to update itself
        articleFrag.updateArticleView(position);
    } else {
        // If the fragment is not available, use the one pane layout and 
        // swap between the article and headline fragments
        // Create fragment and give it an argument for the selected article
        ArticleFragment newFragment = new ArticleFragment();
        // The Bundle contains information passed between activities
        Bundle args = new Bundle();
        // Save the current article value
        args.putInt(ArticleFragment.ARG_POSITION, position);
        // Add the article value to the new Fragment
        newFragment.setArguments(args);
        // The FragmentTransaction adds, removes, replaces and
        // defines animations for Fragments
        // The FragmentManager provides methods for interacting
        // beginTransaction() is used to begin any edits of Fragments
        FragmentTransaction transaction = 
            getSupportFragmentManager().beginTransaction();
        // Replace whatever is in the fragment_container view with this fragment,
        // and add the transaction to the back stack so the user can navigate back
        transaction.replace(R.id.fragment_container, newFragment);
        // addToBackStack() causes the transaction to be remembered. 
        // It will reverse this operation when it is later popped off 
        // the stack.
        transaction.addToBackStack(null);
        // Schedules for the addition of the Fragment to occur
        transaction.commit();
    }
}

news_article.xml:

 <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:baselineAligned="false"
  android:orientation="horizontal"
   android:layout_width="match_parent"
   android:layout_height="match_parent">
   <fragment android:name="com.example.fragment2.HeadlinesFragment"
    android:id="@+id/headlines_fragment"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="match_parent" />
    <fragment android:name="com.example.fragment2.ArticleFragment"
    android:id="@+id/article_fragment"
    android:layout_weight="2"
    android:layout_width="0dp"
    android:layout_height="match_parent" />
      </LinearLayout>

请帮助我,我渴望学习。

在代码导入中。

Import android.support.v4.app.Fragment

也许你的代码工作正常。

我认为主要是因为您正在混合库,如上所述,请检查您是否在 ArticleFragment 类中导入了正确的库

Import android.support.v4.app.Fragment;

最新更新