gpt4 book ai didi

java - Activity 中的构造函数未定义

转载 作者:行者123 更新时间:2023-12-02 05:12:43 24 4
gpt4 key购买 nike

我已经与这个错误斗争了几个小时,但无法继续。我需要你的帮助!

我正在尝试用 SDK21 中的 ViewPager 替换 ActionBar 上以前的 TabNavigation,查看 StackOverflow 中的评论,我发现 this webpage ,其中通过示例描述了 PagerTabStrip 的使用,因此我尝试在我的 Activity 中实现它,但是我收到了一个奇怪的错误。

我尝试用谷歌搜索该问题,但所有建议并不真正适用于我的问题(构造函数中的错误参数是常见错误)。我还使用一个简单的 Activity 重现了该错误,以避免在之前的 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());

我尝试在 Activity 定义的外部和内部引入适配器,但仍然不起作用。可能很简单,但是......我看不到它,我需要其他眼睛。知道我做错了什么吗?

最佳答案

将 Activity 的导入更改为:

import android.support.v4.app.FragmentActivity;

还将构造函数更改为:

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

关于java - Activity 中的构造函数未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27206857/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com