2012-11-21 4 views
3

두 개의 마스터 디테일 프래그먼트가있는 주 활동이 있습니다. "Multiple fragments, multiple activities" 메서드를 구현하려고합니다.클래스 조각 오류가 부 풀릴 때 오류가 발생했습니다.

레이아웃 폴더 activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MoneyActivity" 
    android:id="@+id/fragment_container" > 
<fragment class="com.mysite.money.AFragment" 
       android:id="@+id/AFragment" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"/> 
</FrameLayout> 

레이아웃 큰 폴더 activity_main.xml 태블릿에

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


    <fragment class="com.mysite.money.AFragment" 
       android:id="@+id/AFragment" 
       android:layout_width="@dimen/action_bar_title_text_size" 
       android:layout_height="match_parent"/> 

    <fragment class="com.mysite.money.BFragment" 
       android:id="@+id/BFragment" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"/> 


</LinearLayout> 

나는 아래 (같은 오류가 발생했습니다를 실행 -layout-large) :

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mysite.money/com.mysite.money.MoneyActivity}: android.view.InflateException: Binary XML file line #15: Error inflating class fragment 

조각의 클래스 이름을 올바르게 검사했습니다.

나는 내가 오류 것 같아 BFragment

BFragment : 공용 클래스 BFragment은 (단편 A를 관련)

내 mainActivity에 OnItemSelected SherlockFragment {

String selectedItem=""; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     int size = getArguments().size(); 
     if(size>0) 
     { 
      selectedItem = getArguments().getString("position").toString(); 
     } 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 
     TextView textView=new TextView(inflater.getContext()); 
     textView.setText("Selected Item->"+selectedItem); 
     return textView; 
    } 

}를 확장

**@Override 
    public void onItemSelected(String id) { 
      BFragment displayFrag = (BFragment) getSupportFragmentManager().findFragmentById(new BFragment().getId()); 
     if (displayFrag == null) { 
      // DisplayFragment (Fragment B) is not in the layout (handset layout), 
      // so start DisplayActivity (Activity B) 
      // and pass it the info about the selected item 
      Intent intent = new Intent(this, BActivity.class); 
      intent.putExtra("position", id); 
      Log.i("innodea", "position->"+id); 
      startActivity(intent); 

     } else { 
      // DisplayFragment (Fragment B) is in the layout (tablet layout), 
      // so tell the fragment to update 
      //displayFrag.updateContent(id); 
     } 
    }** 

AFragment :

public class AFragment extends SherlockListFragment { 

    private View inflate; 
    private Callbacks mCallbacks = sDummyCallbacks; 
    private int mActivatedPosition= ListView.INVALID_POSITION; 
    private static final String STATE_ACTIVATED_POSITION = "activated_position"; 

    public interface Callbacks { 
     public void onItemSelected(String id); 
    } 

    /** 
    * A dummy implementation of the {@link Callbacks} interface that does 
    * nothing. Used only when this fragment is not attached to an activity. 
    */ 
    private static Callbacks sDummyCallbacks = new Callbacks() { 
     public void onItemSelected(String id) { 
     } 
    }; 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
     // Activities containing this fragment must implement its callbacks. 
     if (!(activity instanceof Callbacks)) { 
      throw new IllegalStateException(
        "Activity must implement fragment's callbacks."); 
     } 

     mCallbacks = (Callbacks) activity; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setListAdapter(new ArrayAdapter<DummyContent.DummyItem>(getActivity(),android.R.layout.simple_list_item_activated_1,android.R.id.text1, DummyContent.ITEMS)); 
    } 

    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 

     // Restore the previously serialized activated item position. 
     if (savedInstanceState != null 
       && savedInstanceState.containsKey(STATE_ACTIVATED_POSITION)) { 
      setActivatedPosition(savedInstanceState 
        .getInt(STATE_ACTIVATED_POSITION)); 
     } 
    } 



    @Override 
    public void onDetach() { 
     super.onDetach(); 

     // Reset the active callbacks interface to the dummy implementation. 
     mCallbacks = sDummyCallbacks; 
    } 

    @Override 
    public void onListItemClick(ListView listView, View view, int position,long id) { 
     super.onListItemClick(listView, view, position, id); 

     // Notify the active callbacks interface (the activity, if the 
     // fragment is attached to one) that an item has been selected. 
     mCallbacks.onItemSelected(DummyContent.ITEMS.get(position).id); 
    } 

    @Override 
    public void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
     if (mActivatedPosition != ListView.INVALID_POSITION) { 
      // Serialize and persist the activated item position. 
      outState.putInt(STATE_ACTIVATED_POSITION, mActivatedPosition); 
     } 
    } 

    /** 
    * Turns on activate-on-click mode. When this mode is on, list items will be 
    * given the 'activated' state when touched. 
    */ 
    public void setActivateOnItemClick(boolean activateOnItemClick) { 
     // When setting CHOICE_MODE_SINGLE, ListView will automatically 
     // give items the 'activated' state when touched. 
     getListView().setChoiceMode(
       activateOnItemClick ? ListView.CHOICE_MODE_SINGLE 
         : ListView.CHOICE_MODE_NONE); 
    } 

    private void setActivatedPosition(int position) { 
     if (position == ListView.INVALID_POSITION) { 
      getListView().setItemChecked(mActivatedPosition, false); 
     } else { 
      getListView().setItemChecked(position, true); 
     } 

     mActivatedPosition = position; 
    } 

} 
+2

조각 코드와 logcat 오류를 게시하십시오. –

+0

동일한 문제가 있습니다. 작동합니까? 여기 내 질문 : http://stackoverflow.com/questions/14840128/android-fragments-2-fragment-1-frame-layout –

답변

1

onActivityCreated()이 호출되기 전에 조각 내에 getActivity()으로 조작하면 예외 android.view.InflateException: Binary XML file line: #... Error inflating class fragment이 발생할 수 있습니다. 이 경우 잘못된 활동 참조를 받고 그에 의존 할 수 없습니다.

는 예를 들어 다음 패턴은 잘못된 것입니다 :

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

    Button button = getActivity().findViewById(R.id...); 
    button.setOnClickListener(...); - another problem: button is null 

    return view; 
} 
0

당신의 조각 코드가 가장 가능성이 깨진 단순히 인플레이션의 실패로 끝나는 창조에 충돌한다. 각 조각 onCreateView()에 중단 점을 작성하고 조각을 만드는 동안 호출 된 관련 메서드를 사용하거나 조각을 손으로 인스턴스화 (new AFragment())하고 레이아웃에 연결하여 실패한 경우 정확한 위치를 확인합니다.

0

당신은 android.support.v4.app.Fragment 대신 android.app.Fragment에서 Fragment 클래스를 가져와야합니다.

import android.support.v4.app.Fragment; 

그리고이 단편을 사용하려는 활동의 XML 파일에

, 당신은 사용할 필요가 :

 <fragment 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/map_fragment" 
     name="yourpackagname.yourfragmentclass"/> 
     //package name here, is the name of folder which is in java directory. 
1

가 필요 정적 조각에 대한 고유 ID를 추가. Logs에서 오류를주의 깊게보고 찾았습니다.오류 세부 정보를 볼 수 아래 링크를 클릭 :

Error Inspection in logs

필요 없음을 FragmentActivity 많은 게시물에 제안. AppCompatActivity가 좋습니다. 그래서 아래 코드는 잘 작동합니다.

<?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"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="10dp" 
     android:text="Customize your Android: " /> 

    <fragment xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:name="com.example.android.android_me.ui.MasterListFragment" 
     android:id="@+id/StaticFragment"/> 

</LinearLayout> 
관련 문제