"找不到id"在TabHost设置中选择容器

时间:2014-06-25 11:37:15

标签: android fragment android-tabhost alertdialog

我正在创建一个对话框,其中包含x个用户可以选择的标签页。这种情况的独特之处在于我已经有一个TabHost用于我的活动。不知何故TabHost工作但我的新工作不是。我得到的错误是:

java.lang.IllegalArgumentException: No view found for id 0x7f0b005c (nl.raakict.android.spc:id/realtabcontent2) for fragment CreateCarrierTabFragment{211feed8 #2 id=0x7f0b005c Overig} 

但我确信视图ID存在,因为这是我的XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <android.support.v4.app.FragmentTabHost
        android:id="@+id/tabhost2"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
            <TabWidget
                android:id="@+id/tabs2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent" />
            <FrameLayout
                android:id="@+id/realtabcontent2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </android.support.v4.app.FragmentTabHost>
</LinearLayout>

如果我在id:

上执行findviewbyid,这就是我在调试模式下看到的内容

tabhost test

我的完整课程代码:

package nl.raakict.android.spc.Fragment;

import android.app.AlertDialog;
import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

import nl.raakict.android.spc.Controller.API;
import nl.raakict.android.spc.Model.CarrierCategory;
import nl.raakict.android.spc.Model.CarrierType;
import nl.raakict.android.spc.R;

public class PopupCreateCarrier extends DialogFragment {

    private FragmentTabHost mTabHost;
    private API mApi;
    private ArrayList<CarrierType> carrierTypes;
    private HashMap<Long, CarrierCategory> carrierCategories;
    private HashMap<Long, View> carrierCategoriesViewIndicators;
    private HashMap<Long, ArrayList<CarrierType>> indicatorCarrierTypes;
    private LayoutInflater layoutInflater;
    private CarrierCategory otherCategory;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        layoutInflater = LayoutInflater.from(getActivity());
//        final View view = layoutInflater.inflate(R.layout.fragment_tab_host_new_carrier, null);
//        indicatorCarrierTypes = new HashMap<Long, ArrayList<CarrierType>>();
//        mApi = API.getInstance(getActivity());
//        mApi.GetCarrierCategories(this);
//        mTabHost = (FragmentTabHost) view.findViewById(android.R.id.tabhost);
//        mTabHost.setup(getActivity(), getActivity().getSupportFragmentManager(), R.id.realtabcontent);
//        otherCategory = new CarrierCategory();
//        otherCategory.setName("Overig");
//        otherCategory.setID(Long.MAX_VALUE);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        layoutInflater = LayoutInflater.from(getActivity());
        final View view = layoutInflater.inflate(R.layout.fragment_tab_host_new_carrier, null);
        otherCategory = new CarrierCategory();
        otherCategory.setName("Overig");
        otherCategory.setID(Long.MAX_VALUE);
        indicatorCarrierTypes = new HashMap<Long, ArrayList<CarrierType>>();
        mApi = API.getInstance(getActivity());
        mTabHost = (FragmentTabHost) view.findViewById(R.id.tabhost2);
        View test = view.findViewById(R.id.realtabcontent2);
        mTabHost.setup(getActivity(), getActivity().getSupportFragmentManager(), R.id.realtabcontent2);

        mapCategoriesToHashMaps();
        mapCarrierTypesToHashMaps();
        setTabViews();

        Dialog dialog = new AlertDialog.Builder(getActivity())
                .setTitle(R.string.titel_pallets_bakken)
                .setView(view)
                .create();
        return dialog;
    }

    private void mapCategoriesToHashMaps(){
        ArrayList<CarrierCategory> carrierCategoriesAL = mApi.getAllCarrierCategories();
        carrierCategories = new HashMap<Long, CarrierCategory>();
        carrierCategoriesViewIndicators = new HashMap<Long, View>();
        indicatorCarrierTypes = new HashMap<Long, ArrayList<CarrierType>>();
        if(carrierCategoriesAL != null) {
            for (CarrierCategory cc : carrierCategoriesAL) {
                carrierCategories.put(cc.getID(), cc);
            }
            for (CarrierCategory category : carrierCategoriesAL) {
                if (!carrierCategoriesViewIndicators.containsKey(category)) {
                    carrierCategoriesViewIndicators.put(category.getID(), layoutInflater.inflate(R.layout.tab_view_carrier, null));
                    indicatorCarrierTypes.put(category.getID(), new ArrayList<CarrierType>());
                }
            }
        }
    }

    private void mapCarrierTypesToHashMaps(){
        carrierTypes =  mApi.getAllCarrierTypes();
        for(CarrierType carrierType : carrierTypes){
            if(carrierType.getCarrierCategoryID() != null){
                long carrierTypeId = carrierType.getCarrierCategoryID();
                if(carrierCategoriesViewIndicators.containsKey(carrierType.getCarrierCategoryID()))
                    indicatorCarrierTypes.get(carrierTypeId).add(carrierType);
                else{
                    carrierCategoriesViewIndicators.put(carrierTypeId, layoutInflater.inflate(R.layout.tab_view_carrier, null));
                    indicatorCarrierTypes.put(carrierTypeId, new ArrayList<CarrierType>());
                    indicatorCarrierTypes.get(carrierTypeId).add(carrierType);
                }
            } else {
                //TODO remove after test
                if(carrierCategoriesViewIndicators.containsKey(otherCategory.getID()))
                    indicatorCarrierTypes.get(otherCategory.getID()).add(carrierType);
                else{
                    carrierCategoriesViewIndicators.put(otherCategory.getID(), layoutInflater.inflate(R.layout.tab_view_carrier, null));
                    indicatorCarrierTypes.put(otherCategory.getID(), new ArrayList<CarrierType>());
                    indicatorCarrierTypes.get(otherCategory.getID()).add(carrierType);
                    carrierCategories.put(otherCategory.getID(), otherCategory);
                }
            }
        }
    }

    private void setTabViews() {
        for(long catId : carrierCategoriesViewIndicators.keySet()){
            Bundle b = new Bundle();
            View indicator = carrierCategoriesViewIndicators.get(catId);
            indicator.setBackgroundResource(R.drawable.carrier_tab);
            TextView werkplekIndicatorTitle = (TextView) indicator
                    .findViewById(R.id.text_category_name);
            werkplekIndicatorTitle.setText(carrierCategories.get(catId).getName());
            werkplekIndicatorTitle.setTextColor(Color.WHITE);
            b.putString("key", carrierCategories.get(catId).getName());
            b.putParcelableArrayList("carriertypes", indicatorCarrierTypes.get(catId));
            mTabHost.addTab(
                    mTabHost.newTabSpec(carrierCategories.get(catId).getName()).setIndicator(carrierCategoriesViewIndicators.get(catId)),
                    CreateCarrierTabFragment.class, b);
        }
    }

}

请注意,如果我将其指向我其他TabHost的容器,它确实有效,但当然它会更改错误的容器。

1 个答案:

答案 0 :(得分:0)

我的观点并没有直接夸大活动,所以我使用了错误的FragmentManager,我必须使用的是getChildFragmentManager()。