2011-10-18 80 views
5

Tôi đang làm việc trên 3.0 lần đầu tiên. Tôi muốn thêm mảnh vỡ động, nhưng lỗi hiển thị của nó: -Nhận lỗi khi thêm phân đoạn động -java.lang.IllegalStateException:

10-18 18:29:11.215: ERROR/AndroidRuntime(3550): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 

mã XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:id="@+id/frags"> 

<ListView 
     android:id="@+id/number_list" 
     android:layout_width="250dip" 
     android:layout_height="match_parent" /> 

<FrameLayout 
     android:id="@+id/the_frag" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

Hoạt động

public class FragmentExampleActivity extends Activity implements 
    OnItemClickListener { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    ListView l = (ListView) findViewById(R.id.number_list); 
    ArrayAdapter<String> numbers = new ArrayAdapter<String>(
      getApplicationContext(), android.R.layout.simple_list_item_1, 
      new String[] { "one", "two", "three", "four", "five", "six" }); 
    l.setAdapter(numbers); 
    l.setOnItemClickListener(this); 
} 

public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

    Fragment f = new FragmentExample(); 

    FragmentTransaction ft = getFragmentManager().beginTransaction();  
    ft.replace(R.id.the_frag, f); 
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 
    ft.addToBackStack(null); 
    ft.commit(); 

} 
} 


public class FragmentExample extends Fragment { 
private int nAndroids=1; 

public FragmentExample() { 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saved) { 
    int n; 

    View l = inflater.inflate(R.layout.textlay,container); 
    TextView tv = (TextView) l.findViewById(R.id.textView1); 
    tv.setText("value "+nAndroids); 

    return l; 
} 

}

Plz đặt một số ánh sáng trên, nơi tôi đang đi sai :(

Trả lời

19

Thay vì sử dụng

View l = inflater.inflate(R.layout.textlay,container); 

bạn nên sử dụng

View l = inflater.inflate(R.layout.textlay, container, false); 

hoặc cách khác

View l = inflater.inflate(R.layout.textlay, null); 

Tôi đề nghị bạn sử dụng, phiên bản inflate có ba tham số. Android chỉ sử dụng vùng chứa cho mục đích thông số của bố cục. Thông qua false làm tham số thứ ba, ngăn không cho việc thêm textlay, vào container và khắc phục sự cố của bạn

+0

Thankx it worked :) – voidRy

+0

Nhưng y chúng tôi đã vượt qua không? – voidRy

+2

Vùng chứa chỉ nên được sử dụng cho Bố cục bố cục. Không phải trong quá trình lạm phát. – Spidy

3

hoặc bạn có thể sử dụng đối số thứ ba và chuyển nó thành false.

inflater.inflate(R.layout.sample_fragment, container,false); 

có nghĩa là không đính kèm chế độ xem hiện tại vào gốc.

4

nếu trong onCreateView(), bạn muốn sử dụng lại xem, sau đó bạn có thể đối phó với onDestroyView()

@Override 
public void onDestroyView() { 
    super.onDestroyView(); 
    if (view != null) { 
     ViewGroup parentViewGroup = (ViewGroup) view.getParent(); 
     if (parentViewGroup != null) { 
      parentViewGroup.removeAllViews(); 
     } 
    } 
} 
0

Sau đây giải quyết nhiều vấn đề:

public static View contentView; 

public static class MenuCardFrontFragment extends Fragment { 
    public MenuCardFrontFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     try 
     { 
      LinearLayout frontLayout = (LinearLayout)inflater.inflate(R.layout.content_card, container, false); 
      // modify layout if necessary 
      return contentView = frontLayout; 
     } catch (InflateException e) { 
      return contentView; 
     } 
    } 

    @Override 
    public void onDestroyView() { 
     super.onDestroyView(); 
     if (contentView != null) { 
      ViewGroup parentViewGroup = (ViewGroup) contentView.getParent(); 
      if (parentViewGroup != null) { 
       parentViewGroup.removeAllViews(); 
      } 
     } 
    } 
} 
  1. Nếu mảnh show với hoạt ảnh và người dùng buộc phải lặp lại hoạt ảnh trước khi nó kết thúc. ví dụ: nhấn nút menu hai lần trước khi phân đoạn menu kết thúc hoạt ảnh và xuất hiện. (điều này đã được giải quyết bởi onDestryView từ user2764384)

  2. Nếu phương pháp thổi phồng được gọi là chỉ định vùng chứa để đặt thùng chứa. có vẻ như trường hợp đó là một vấn đề bởi vì vào lần thứ 2 xảy ra, vùng chứa cũ đã có chế độ xem. do đó, nếu ngoại lệ đó xảy ra, nó sẽ bị bắt và trả về chế độ xem nội dung cũ.

Các vấn đề liên quan