我正在Android項目中,我需要給頁面中的標籤,所以我使用相同的片段, 我已成功創建片段,但在我的代碼的主頁加入所有片段,其給出的錯誤爲Android的片段意外的錯誤
'錯誤的第一個參數類型。找到:'com.android.mads.madses.LoginInfoFragment',required:'android.support.v4.app.Fragment'less ... addFrag (android.support.v4.app.Fragment, String) in ViewPagerAdapter不能應用於 到 (com.android.mads.madses.LoginInfoFragment, 字符串)」
我沒有得到究竟是什麼錯誤吧。 任何人都可以幫我解決這個問題嗎?
下面是完整的Java代碼
package com.android.mads.madses;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
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.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import java.util.ArrayList;
import java.util.List;
import com.android.mads.madses.LoginInfoFragment;
import com.android.mads.madses.VechicleInfoFragment;
import com.android.mads.madses.PersonalInfoFragment;
import com.android.mads.madses.ContactInfoFragment;
public class RegisterActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new LoginInfoFragment(), "LoginInfo");
adapter.addFrag(new PersonalInfoFragment(), "PersonalInfo");
adapter.addFrag(new ContactInfoFragment(), "ContactInfo");
adapter.addFrag(new VechicleInfoFragment(), "VechicleInfo");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
謝謝,它爲我工作... –
你能解釋兩者之間的區別嗎? –
查看編輯答案 – escull638