2016-11-14 66 views
1

MainActivity在啓動時將一個片段佈局添加到Relativeview,然後我發送一個數據片段將其添加到ExpandablelistView,但我的應用程序顯示我無法識別ExpandablelistView的錯誤。無法訪問片段中的ExpandablelistView

MainActivity:

public class MainActivity extends AppCompatActivity implements FragmentAddCatergory.onClickButtonListener { 
private FragmentManager manager; 
private FragmentTransaction transactionShowList; 
private FragmentTransaction transactionAddCatergory; 
private FragmentAddCatergory addCatergory; 
private FragmentShowCategory showCategory; 
private boolean addcategory; 
private TextView txtAddCategory; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    manager = getFragmentManager(); 
    transactionShowList = manager.beginTransaction(); 
    showCategory = new FragmentShowCategory(); 
    addCatergory=new FragmentAddCatergory(); 
    transactionShowList.add(R.id.Fragment_container, showCategory); 
    transactionShowList.commit(); 
    addcategory=false; 
    txtAddCategory = (TextView) findViewById(R.id.txtaddcategory); 
    txtAddCategory.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      ChangeFragment(); 
     } 
    }); 
} 

public void ChangeFragment(){ 
    transactionAddCatergory=manager.beginTransaction(); 
    if (addcategory){ 
     transactionAddCatergory.replace(R.id.Fragment_container,addCatergory); 
     txtAddCategory.setText("Do you want to see your List?Show me!"); 
     addcategory=false; 
    }else{ 
     transactionAddCatergory.replace(R.id.Fragment_container,showCategory); 
     txtAddCategory.setText("Do you want to add a Category?Create One"); 
     addcategory=true; 
    } 
    transactionAddCatergory.commit(); 
} 

@Override 
public void ClickButton(String group, String child) { 
    FragmentShowCategory a=new FragmentShowCategory(); 
    a.showExpand(this,group,child); 
}} 

在最後上面的代碼中我做的第一個片段對象和發送數據,並在下面的代碼是第一個片段

public class FragmentShowCategory extends Fragment { 
private View view; 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    super.onCreateView(inflater, container, savedInstanceState); 
    view = inflater.inflate(R.layout.activity_expandable_list_view, container, false); 
    return view; 
} 

public void showExpand(Context context, String g, String c) { 
    Toast.makeText(context, g + " is " + c, Toast.LENGTH_LONG).show(); 
    HashMap<String, List<String>> carsDetails = DataProvider.getInfo(g, c); 
    List<String> carsBrands = new ArrayList<String>(carsDetails.keySet()); 
    ItemClass adapter = new ItemClass(context, carsDetails, carsBrands); 
    ExpandableListView list = (ExpandableListView) view.findViewById(R.id.expandList); 
    list.setAdapter(adapter); 
}} 

的代碼,但是當我跑我的應用程序,我得到的錯誤,我不知道爲什麼排隊:

ExpandableListView list =(ExpandableListView)view.findViewById(R.id.expandList);

我很樂意幫助我。

回答

0

僅僅因爲您創建了片段的實例,您的片段的視圖層次結構不會自動膨脹,就像您在ClickButton中做的那樣。必須先調用onCreateView()方法才能使視圖膨脹,這是fragment's lifecycle的一部分。您應該讓Android實例化您的片段,並通過FragmentManager獲取它的實例。

This tutorial解釋關於片段的基本知識。