Android错误:无法从Fragment转换为ArticleFragment

时间:2013-12-31 11:59:11

标签: android android-fragments fragment

我是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

MainActivity.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>

请帮助我,我渴望学习。

2 个答案:

答案 0 :(得分:0)

在您的代码导入中。

Import android.support.v4.app.Fragment

也许你的代码工作正常。

答案 1 :(得分:0)

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

Import android.support.v4.app.Fragment;