我在return MyFragment.get(position);
类中包含MyViewPageAdapter
的行上收到错误。
将鼠标悬停在错误上时,错误告诉我:
Incompatible types.
Required: android.support.v4.app.Fragment
Found:android.app.Fragment
我该如何解决这个问题?
这是MainActivity.java
:
import android.app.Fragment;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
TabLayout MyTabs;
ViewPager MyPage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
MyTabs = (TabLayout)findViewById(R.id.MyTabs);
MyPage = (ViewPager) findViewById(R.id.MyPage);
MyTabs.setupWithViewPager(MyPage);
setUpViewPager(MyPage);
}
public void setUpViewPager(ViewPager viewpage) {
MyViewPageAdapter Adapter = new MyViewPageAdapter(getSupportFragmentManager());
Adapter.AddFragmentPage(new Page_1(), "Page 1");
Adapter.AddFragmentPage(new Page_2(), "Page 2");
Adapter.AddFragmentPage(new Page_3(), "Page 3");
viewpage.setAdapter(Adapter);
}
public class MyViewPageAdapter extends FragmentPagerAdapter {
private List<Fragment> MyFragment = new ArrayList<>();
private List<String> MyPageTitle = new ArrayList<>();
public MyViewPageAdapter(android.support.v4.app.FragmentManager fm) {
super(fm);
}
public void AddFragmentPage(Fragment Frag, String Title) {
MyFragment.add(Frag);
MyPageTitle.add(Title);
}
@Override
public android.support.v4.app.Fragment getItem(int position) {
return MyFragment.get(position);
}
@Override
public CharSequence getPageTitle(int position) {
return MyPageTitle.get(position);
}
@Override
public int getCount() {
return 3;
}
}
@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);
}
}
答案 0 :(得分:0)
FragmentPagerAdapter
中的第一行不正确。
private List<Fragment> MyFragment = new ArrayList<>();
Fragment
中的List<Fragment>
类型为android.app.Fragment
,其类型应为android.support.v4.app.Fragment
。
只需在您的文件中删除此import语句,即应位于顶部:
import android.app.Fragment;
答案 1 :(得分:0)
您可以返回android.app.Fragment
:
@Override
public android.app.Fragment getItem(int position) {
return MyFragment.get(position);
}
或者MyFragment必须扩展android.support.v4.app.Fragment
:
class MyFragment extends android.support.v4.app.Fragment {}
答案 2 :(得分:0)
替换第一行:
import android.app.Fragment;
这一个:
import android.support.v4.app.Fragment;
在Android Sdk中,有两个不同的Fragment
类。 API 11中添加了One,API 22中添加了one。后者为11级以下的API级别提供了Fragment
功能。
答案 3 :(得分:0)
删除第一行导入android.app.fragment。添加android.support.v4.app.Fragment