活动中的构造函数未定义

时间:2014-11-29 20:37:18

标签: java android android-fragments

我一直在与这个错误作斗争几个小时,我无法继续。我需要你的帮助!

我正在尝试用SDK21中的ViewPager替换我之前在ActionBar上的TabNavigation,查看StackOverflow中我发现this webpage的注释,其中使用了一个示例描述了PagerTabStrip的使用,所以我尝试在我的活动,但是我得到了一个奇怪的错误。

我试图谷歌问题,所有的建议都不适用于我的问题(构造函数中的错误参数是通常的错误)。我还使用简单的Activity复制了错误,以避免我在之前的活动中遇到的任何错误,但错误仍然存​​在。我在这里附上我复制的代码:

import android.app.Activity;   
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class TestActivity extends Activity {

CustomPagerAdapter mCustomPagerAdapter;
ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.adding_users_to_list);

    mCustomPagerAdapter = new CustomPagerAdapter(getFragmentManager(), this.getApplicationContext());

    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mCustomPagerAdapter);
}

class CustomPagerAdapter extends FragmentPagerAdapter {

    Context mContext;

    public CustomPagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        mContext = context;
    }

    @Override
    public Fragment getItem(int position) {

        // Create fragment object
        Fragment fragment = new DemoFragment();

        // Attach some data to the fragment
        // that we'll use to populate our fragment layouts
        Bundle args = new Bundle();
        args.putInt("page_position", position + 1);

        // Set the arguments on the fragment
        // that will be fetched in the
        // DemoFragment@onCreateView
        fragment.setArguments(args);

        return fragment;
    }

    @Override
    public int getCount() {
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return "Page " + (position + 1);
    }
}

class DemoFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout resource that'll be returned
        View rootView = inflater.inflate(R.layout.fragment_users, container, false);

        // Get the arguments that was supplied when
        // the fragment was instantiated in the
        // CustomPagerAdapter
        Bundle args = getArguments();
        ((TextView) rootView.findViewById(R.id.text_option)).setText("Page " + args.getInt("page_position"));

        return rootView;
    }
}
}

我在构造函数的调用中遇到以下错误:

"构造函数TestActivity.CustomePageAdapter(FragmentManager,Context)未定义"

在这里:

 mCustomPagerAdapter = new CustomPagerAdapter(getFragmentManager(), this.getApplicationContext());

我试图在活动定义的内部和内部引入适配器,但仍然无法正常工作。可能是简单的东西,但是...我无法看到它,我需要其他的眼睛。知道我做错了吗?

2 个答案:

答案 0 :(得分:1)

原因是您的活动是在ADT中使用的:

import android.app.Activity;

以及支持包中使用的片段:

import android.support.v4.app.FragmentManager;

将活动的导入更改为:

import android.support.v4.app.FragmentActivity;

答案 1 :(得分:1)

将活动的导入更改为:

import android.support.v4.app.FragmentActivity;

还将构造函数更改为:

 mCustomPagerAdapter = new  CustomPagerAdapter(getSupportFragmentManager(), this.getApplicationContext());