我和Tablayot有关系。

public class MapTab extends AppCompatActivity {

private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;


public MapTab() {
}

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

    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.addFragment(new PvPMap(), "PvP Карты");
    adapter.addFragment(new PvEMap(), "PvE Карты");
    viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

XML格式
<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_scrollFlags = "scroll|enterAlways"
        />
    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="center"
        />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

在第一个选项卡中,ListFragment
public class PvPMap extends ListFragment {

Fragment permafrost;
Fragment river;
Fragment reactor;
Fragment oil;
Fragment mountain;
Fragment island;
Fragment cold;
Fragment pis;
Fragment fire;
Fragment air;

FragmentManager manager;
FragmentTransaction transaction;


String [] mapname = new String[] {"Вечная мерзлота","Тихая река","Реактор","Нефтепровод","Предгорье",
        "Затеряный остров","Холодная сталь","Мирный рубеж","Горящий песок","Воздушная тревога"};
int [] mapimg = new int[] {R.drawable.permafrost, R.drawable.river, R.drawable.reaktor, R.drawable.oil, R.drawable.mauntain,
        R.drawable.island, R.drawable.cold, R.drawable.pis, R.drawable.fire, R.drawable.air};
String [] mapinfo = new String[] {getResources().getString(R.string.permafrostInfo), getResources().getString(R.string.riverInfo),
        getResources().getString(R.string.reaktorInfo), getResources().getString(R.string.oilInfo),
        getResources().getString(R.string.mauntainInfo), getResources().getString(R.string.islandInfo),
        getResources().getString(R.string.coldInfo), getResources().getString(R.string.pisInfo),
        getResources().getString(R.string.fireInfo), getResources().getString(R.string.airInfo),};



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View pvp = inflater.inflate(R.layout.layout_pvp, container, false);

    permafrost = new Permafrost();
    river = new River();
    reactor = new Reactor();
    oil = new Oil();
    mountain = new Mauntain();
    island = new Island();
    cold = new Cold();
    pis = new Pis();
    fire = new Fire();
    air = new Air();

    List<HashMap<String, String>> aList =  new ArrayList<HashMap<String, String>>();

    for (int i = 0; i < mapname.length; i++){
        HashMap<String, String> m = new HashMap<String,String>();
        m.put("name", mapname[i]);
        m.put("info", mapinfo[i]);
        m.put("img", Integer.toString(mapimg[i]));
        aList.add(m);
    }

    String[] from = {"name", "info", "img"};

    int [] to = {R.id.textViewMap, R.id.textViewInfo, R.id.imageViewMap};

    SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.list_item, from, to);

    setListAdapter(adapter);

    return pvp;
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    manager = getChildFragmentManager();
    transaction = manager.beginTransaction();

    switch (position){
        case 0:
            transaction.replace(R.id.container, permafrost);
            transaction.addToBackStack(null);
            break;
        case 1:
            transaction.replace(R.id.container, river);
            transaction.addToBackStack(null);
            break;
        case 2:
            transaction.replace(R.id.container, reactor);
            transaction.addToBackStack(null);
            break;
        case 3:
            transaction.replace(R.id.container, oil);
            transaction.addToBackStack(null);
            break;
        case 4:
            transaction.replace(R.id.container, mountain);
            transaction.addToBackStack(null);
            break;
        case 5:
            transaction.replace(R.id.container, island);
            transaction.addToBackStack(null);
            break;
        case 6:
            transaction.replace(R.id.container, cold);
            transaction.addToBackStack(null);
            break;
        case 7:
            transaction.replace(R.id.container, pis);
            transaction.addToBackStack(null);
            break;
        case 8:
            transaction.replace(R.id.container, fire);
            transaction.addToBackStack(null);
            break;
        case 9:
            transaction.replace(R.id.container, air);
            transaction.addToBackStack(null);
            break;

    }
    transaction.commit();
}

仅在次要文本中。
当我使用tablayout进入活动时,会发生错误:
06-15 16:52:09.520 29302-29302/com.example.roman.armoredwarfare E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.example.roman.armoredwarfare, PID: 29302
                                                                              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.roman.armoredwarfare/com.example.roman.armoredwarfare.MapTab}: java.lang.IllegalStateException: Fragment PvPMap{11d5eb92} not attached to Activity
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2534)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2608)
                                                                                  at android.app.ActivityThread.access$800(ActivityThread.java:178)
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:111)
                                                                                  at android.os.Looper.loop(Looper.java:194)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5637)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
                                                                               Caused by: java.lang.IllegalStateException: Fragment PvPMap{11d5eb92} not attached to Activity
                                                                                  at android.support.v4.app.Fragment.getResources(Fragment.java:636)
                                                                                  at com.example.roman.armoredwarfare.PvPMap.<init>(PvPMap.java:55)
                                                                                  at com.example.roman.armoredwarfare.MapTab.setupViewPager(MapTab.java:47)
                                                                                  at com.example.roman.armoredwarfare.MapTab.onCreate(MapTab.java:39)
                                                                                  at android.app.Activity.performCreate(Activity.java:6092)
                                                                                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2481)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2608) 
                                                                                  at android.app.ActivityThread.access$800(ActivityThread.java:178) 
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:111) 
                                                                                  at android.os.Looper.loop(Looper.java:194) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5637) 
                                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                                  at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754) 

具体来说1:
String [] mapinfo = new String[] {getResources().getString(R.string.permafrostInfo), getResources().getString(R.string.riverInfo),
        getResources().getString(R.string.reaktorInfo), getResources().getString(R.string.oilInfo),
        getResources().getString(R.string.mauntainInfo), getResources().getString(R.string.islandInfo),
        getResources().getString(R.string.coldInfo), getResources().getString(R.string.pisInfo),
        getResources().getString(R.string.fireInfo), getResources().getString(R.string.airInfo),};

带列表片段的片段
2:setupViewPager(viewPager);
3:adapter.addFragment(new PvPMap(), "PvP Карты");
与Tablayot一起活动
对不起,我的英语不太好。

最佳答案

您试图在实例化片段之前访问getResources()中所需的上下文。

String [] mapinfo = new String[] {getResources().getString(R.string.permafrostInfo), getResources().getString(R.string.riverInfo),
        getResources().getString(R.string.reaktorInfo), getResources().getString(R.string.oilInfo),
        getResources().getString(R.string.mauntainInfo), getResources().getString(R.string.islandInfo),
        getResources().getString(R.string.coldInfo), getResources().getString(R.string.pisInfo),
        getResources().getString(R.string.fireInfo), getResources().getString(R.string.airInfo),};

你应该在你的视图膨胀之后或者像这样在-
    //Declare it here
     String [] mapinfo;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View pvp = inflater.inflate(R.layout.layout_pvp, container, false);
             //do it here as here you'll have the required context
            mapinfo = new String[] {getResources().getString(R.string.permafrostInfo), getResources().getString(R.string.riverInfo),
            getResources().getString(R.string.reaktorInfo), getResources().getString(R.string.oilInfo),
            getResources().getString(R.string.mauntainInfo), getResources().getString(R.string.islandInfo),
            getResources().getString(R.string.coldInfo), getResources().getString(R.string.pisInfo),
            getResources().getString(R.string.fireInfo), getResources().getString(R.string.airInfo),};


        }

关于android - fragment 未附加到 Activity ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37846127/

10-10 22:42