我怎样才能开始片段活动?

时间:2011-11-02 11:01:16

标签: java android

对于我的应用程序,我使用ViewPager lib,因此用户可以刷页面。我想使用fragmentactivity或activity,所以我可以通过FindViewById访问布局元素。但是当我尝试启动我的应用程序时,我收到一个错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.ViewPagerActivity}: 

Caused by: java.lang.ClassCastException: com.example.testActivity

我不能投入一个片段......那么解决方案是什么?

主:

/**
 * The <code>ViewPagerFragmentActivity</code> class is the fragment activity hosting the ViewPager
 */
public class ViewPagerActivity extends FragmentActivity{
    /** maintains the pager adapter*/
    private PagerAdapter mPagerAdapter;
    /* (non-Javadoc)
     * @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.main);
        //initialsie the pager
        this.initialisePaging();
    }

    /**
     * Initialise the fragments to be paged
     */
    private void initialisePaging() {

        List<Fragment> fragments = new Vector<Fragment>();
        fragments.add(Fragment.instantiate(this, testActivity.class.getName()));
        fragments.add(Fragment.instantiate(this, test2Activity.class.getName()));
        this.mPagerAdapter  = new PagerAdapter(super.getSupportFragmentManager(), fragments);


        ViewPager pager = (ViewPager)super.findViewById(R.id.viewpager);
        TitlePageIndicator indicator = (TitlePageIndicator) findViewById(R.id.indicator);
        pager.setAdapter(this.mPagerAdapter);
        indicator.setViewPager(pager);
    }
}

测试活动:

public class test extends FragmentActivity { //or activity?

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout);
  { 

}

1 个答案:

答案 0 :(得分:0)

RuntimeException会告诉你出了什么问题。如果您使用的是Eclipse,它将不会显示完整的堆栈跟踪,但您可以在logcat中找到它。