Java 无法解析构造函数 ArrayAdapter

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28848735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:56:13  来源:igfitidea点击:

Cannot resolve constructor ArrayAdapter

javaandroidarraylistandroid-tabs

提问by Jacques Krause

Hi Guys I am getting a Cannot resolve constructor ArrayAdapterin my OnCreateViewmethod with the listAdapterI am trying to list text data in my tabs and here is my main activity. Any help will be appreciated I am still a noobie in Java.

嗨,伙计们,我在我的方法中遇到了无法解析构造函数 ArrayAdapter 的问题,我正在尝试在我的选项卡中列出文本数据,这是我的主要活动。任何帮助将不胜感激我仍然是.OnCreateViewlistAdapterJava

public class MainActivity extends ActionBarActivity {

    private Toolbar toolbar;
    private ViewPager mPager;
    private SlidingTabLayout mTabs;

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

        toolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);

        mPager= (ViewPager) findViewById(R.id.viewPager);
        mTabs=(SlidingTabLayout) findViewById(R.id.tabs);
        mPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
        mTabs.setDistributeEvenly(true);
        mTabs.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
        mTabs.setSelectedIndicatorColors(getResources().getColor(R.color.colorAccent));
        mTabs.setViewPager(mPager);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    class MyPagerAdapter extends FragmentPagerAdapter {

        String[] tabs;

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
            tabs=getResources().getStringArray(R.array.tabs);
        }

        @Override
        public Fragment getItem(int position) {

            MyFragment myFragment=MyFragment.getInstance(position);
            return myFragment;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tabs[position];
        }

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

    public static class MyFragment extends Fragment {

        private TextView textView;
        private ListView mainListView;
        private ArrayAdapter<String> listAdapter ;

        public static MyFragment getInstance(int position){

            MyFragment myFragment=new MyFragment();
            Bundle  args=new Bundle();
            args.putInt("position", position);
            myFragment.setArguments(args);
            return myFragment;
        }

        @Override

        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

            View layout=inflater.inflate(R.layout.fragment_my, container, false);

            mainListView=(ListView) layout.findViewById(R.id.position);

            String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars",
                    "Jupiter", "Saturn", "Uranus", "Neptune"};
            ArrayList<String> planetList = new ArrayList<String>();
            planetList.addAll( Arrays.asList(planets) );

            listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList);
            mainListView.setAdapter( listAdapter );
            return layout;
        }
    }
}

采纳答案by M D

Change

改变

 listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList);

to

 listAdapter = new ArrayAdapter<String>(getActivity(), R.layout.simplerow, planetList);

You can get Contextin Fragmentby using getActivity().

你可以ContextFragment使用getActivity()

回答by NomanJaved

Replace thiswith activityName.thisChange

替换thisactivityName.this更改

listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList);

with

listAdapter = new ArrayAdapter<String>(activityName.this, R.layout.simplerow, planetList);

This solved for me using in asyncTask.

这为我在 asyncTask 中使用解决了。

回答by Souvik Dutta

Try

尝试

import android.widget.ArrayAdapter;

This can as well solve the problem.

这也可以解决问题。