4

Tabhost 및 Fragments를 사용하여 탭이 설정된 간단한 Android 애플리케이션이 있습니다. 탭 페이지 중 하나에서 조각은 목록보기입니다. 내가하고 싶은 일은 사용자가 목록보기의 항목 중 하나를 클릭하고 클릭 한 항목을 기반으로보다 구체적인 정보로 새보기를 열어 탭을 그대로 유지하면서 보존합니다.Android Fragment, TabHost 내부의 ID에 대한 뷰가 없습니다.

내 접근 방식은 사용자가 목록보기에서 항목을 클릭하고 새 단편을 만든 다음이 모든 단편을 소유하고 탭 논리를 처리하는 기본 FragmentActivity 클래스에서 현재 표시된 단편을 분리하고 이 "새로운"(singleStationListItem) 프래그먼트를 추가하십시오. 내가 겪고있는 문제는 새로운 조각을 R.id.realtabcontent가있는 fragmentTransaction에 추가 할 수 없기 때문에 제대로 추가되지 않았습니다. 내가 얻을 :

10월 3일에서 21일까지 : 50 : 01.862 : E/FragmentManager (1136) : ID 0x1010000 (안드로이드 : ATTR/테마)에 대한 찾을 수 없습니다보기 조각 singleStationListItem에 대한 {40d090a0 # 2 ID = 0x1010000}

여기서 R.id.realtabcontent = 0x01010000;

Id없이 첨부 할 수 있지만 사용자는 되돌릴 수 없습니다 (addtobackstash()). 나는이 오류를 찾았지만 다른 솔루션은 tabhost와 관련이 없으며 필자는 framelayout 태그가 포함 된 레이아웃의 xml 파일에 onCreate()를 호출하여 setContextView (...)와 같이 필요한 작업을하는 것처럼 보입니다. 내 조각을 붙이려고. R.id.realtabcontent에 탭 조각을 문제없이 추가 할 수 있습니다. 아무도 내 오류의 원인이 될 수 있고 그것을 고칠 수있는 방법을 알고 있습니까?

참고 : 본인은 내 탭을 작동시키기 위해이 자습서를 따릅니다. 내 코드는 매우 유사합니다. http://thepseudocoder.wordpress.com/2011/10/04/android-tabs-the-fragment-way/

내 코드 : activiy_main.xml 내 조각 활동 클래스

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/main_body" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <TabHost 
     android:id="@android:id/tabhost" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="vertical" > 

      <FrameLayout 
       android:id="@android:id/tabcontent" 
       android:layout_width="0dp" 
       android:layout_height="0dp" 
       android:layout_weight="0" 
       android:padding="5dp" /> 

      <FrameLayout 
       android:id="@+android:id/realtabcontent" <--id that I want to set to 
       android:layout_width="fill_parent" 
       android:layout_height="0dp" 
       android:layout_weight="1" /> 

      <TabWidget 
       android:id="@android:id/tabs" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginBottom="-4dp" 
       android:layout_weight="0" 
       android:orientation="horizontal" /> 
     </LinearLayout> 
    </TabHost> 
</LinearLayout> 

main_activity.java // 부품

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // Step 1: Inflate layout 
     setContentView(R.layout.activity_main); 
     // Step 2: Setup TabHost 
     initialiseTabHost(savedInstanceState); 
     if (savedInstanceState != null) { 
      mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); //set the tab as per the saved state 
     } 
    } 

public void onTabChanged(String tag) { //handles tab changes 
    TabInfo newTab = (TabInfo) this.mapTabInfo.get(tag); 
    if (mLastTab != newTab) { 
     FragmentTransaction ft =  this.getSupportFragmentManager().beginTransaction(); 
     if (mLastTab != null) { 
      if (mLastTab.fragment != null) { 
       ft.detach(mLastTab.fragment); 
      } 
     } 
     if (newTab != null) { 
      if (newTab.fragment == null) { 
       newTab.fragment = Fragment.instantiate(this, 
         newTab.clss.getName(), newTab.args); 
       ft.add(R.id.realtabcontent, newTab.fragment, newTab.tag); //note it uses the R.id.realtabcontent without any issues here 
      } else { 
       ft.attach(newTab.fragment); 
      } 
     } 

     Log.d("fragment manager in activity: ", "" + ft.isEmpty()); 
     mLastTab = newTab; 
     ft.commit(); 
     this.getSupportFragmentManager().executePendingTransactions(); 
     Log.d("ft ID: ", ft.toString()); 
    } 
} 

//This is the callback function inside the listview fragment. I created an interface and it is overwritten in here as suggested by the Android SDK guide 
public void onStationSelected(String station) { 
    FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction(); 
    ft.addToBackStack(null); 
    ft.addToBackStack(null); 

    //create argument for new fragment that will be created 
    Bundle b = new Bundle(); 
    b.putString("station", station); 
    Fragment displayStationInfo = Fragment.instantiate(this, singleStationListItem.class.getName(), b); 

    Fragment cur = getSupportFragmentManager().findFragmentById(R.id.realtabcontent); 
    ft.detach(cur); 
    ft.add(R.id.realtabcontent, displayStationInfo); <-- this line causes the error 
    ft.commit(); 
    this.getSupportFragmentManager().executePendingTransactions(); 
} 

singleStationListItem.java 여기

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Bundle b = this.getArguments(); 

    getActivity().setContentView(R.layout.single_station_item_view); 

    TextView txtStation = (TextView) getActivity().findViewById(R.id.station_label); 

    String station = b.getString("station"); 
    // displaying selected product name 
    Log.d(TAG, "passed in station information: " + station); 
    txtStation.setText(station); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    View v = (LinearLayout)inflater.inflate(R.layout.single_station_item_view, container, false); 
    return v; 
} 

입니다 어떤 도움이 gre일까요? 감사합니다.

답변

0

나는이 질문을 한참 전에 물어 보았다. 나는이 질문을 접할 수있는 다른 사람들을위한 답변을 게시하고있다.

내가 아는 한 Android ID는 'realtabcontent'가 아닙니다. XML에서 FrameLayout에 대한 ID의 'android'부분을 삭제합니다. 다음과 같이

<FrameLayout 
    android:id="@+id/realtabcontent" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" /> 

developer documentation에 게시 된 전체 XML 코드

는 다음과 같습니다

<android.support.v4.app.FragmentTabHost 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

    <TabWidget 
     android:id="@android:id/tabs" 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0"/> 

    <FrameLayout 
     android:id="@android:id/tabcontent" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_weight="0"/> 

    <FrameLayout 
     android:id="@+id/realtabcontent" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"/> 

</LinearLayout> 

행운을 빕니다 및 코딩 행복!

관련 문제