2015-09-28 74 views
0

我想動態地改變一個文本片段textview裏面的活動,但我不斷得到'無法獲得根'的錯誤。我試圖在onCreateView和onActivityCreated內部實例化textview,但它不起作用。如何更改活動內的片段文本視圖中的文本?從功能訪問片段的活動與功能

片段的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 
<TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:text="Tab 1" 
     android:textAppearance="?android:attr/textAppearanceLarge"/> 
</RelativeLayout> 

片段編碼:

public class TabFragment1 extends Fragment { 
    private TextView tv; 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.tab_fragment_1, container, false); 
     return view; 
    } 
    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     tv = (TextView) getView().findViewById(R.id.textView); 
    } 
    public void setText(String text){ 
     tv.setText(text); 
    } 

} 

主要的xml:

<RelativeLayout 
    android:id="@+id/main_layout" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:background="?attr/colorPrimary" 
     android:elevation="6dp" 
     android:minHeight="?attr/actionBarSize" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 

    <android.support.design.widget.TabLayout 
     android:id="@+id/tab_layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/toolbar" 
     android:background="?attr/colorPrimary" 
     android:elevation="6dp" 
     android:minHeight="?attr/actionBarSize" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/pager" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:layout_below="@id/tab_layout"/> 
</RelativeLayout> 

主代碼:

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout); 
     tabLayout.addTab(tabLayout.newTab().setText("Tab 1")); 
     tabLayout.addTab(tabLayout.newTab().setText("Tab 2")); 
     tabLayout.addTab(tabLayout.newTab().setText("Tab 3")); 
     tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); 

     final ViewPager viewPager = (ViewPager) findViewById(R.id.pager); 
     final PagerAdapter adapter = new PagerAdapter 
       (getSupportFragmentManager(), tabLayout.getTabCount()); 
     TabFragment1 t1 = new TabFragment1(); 
     TabFragment2 t2 = new TabFragment2(); 
     TabFragment3 t3 = new TabFragment3(); 
     adapter.newFrag(t1); 
     adapter.newFrag(t2); 
     adapter.newFrag(t3); 
     viewPager.setAdapter(adapter); 
     viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); 
     tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { 
      @Override 
      public void onTabSelected(TabLayout.Tab tab) { 
       viewPager.setCurrentItem(tab.getPosition()); 
      } 

      @Override 
      public void onTabUnselected(TabLayout.Tab tab) { 

      } 

      @Override 
      public void onTabReselected(TabLayout.Tab tab) { 

      } 
     }); 
     t1.setText("HELLO WORLD"); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

PagerAdapter:

public class PagerAdapter extends FragmentStatePagerAdapter { 
    int mNumOfTabs; 
    List<Fragment> aFrag; 
    public PagerAdapter(FragmentManager fm, int NumOfTabs) { 
     super(fm); 
     this.mNumOfTabs = NumOfTabs; 
     aFrag = new ArrayList<Fragment>(); 
    } 
    public void newFrag(Fragment f){ 
     aFrag.add(f); 
    } 
    @Override 
    public Fragment getItem(int position) { 

     return aFrag.get(position); 
    } 

    @Override 
    public int getCount() { 
     return mNumOfTabs; 
    } 
} 
+0

後堆棧跟蹤並確保片段連接到試圖訪問其視圖層次之前的活動 – fractalwrench

+0

我會盡快發佈它謝謝 – scapegoat

回答

3

您嘗試設置上查看文本,但並沒有創造這個觀點,是不是創造了這個片段。如果您想從活動中設置文本,您可以將轉換爲片段帶有該文本的參數,以及片段chceck包和設置文本。稍後如果你想改變這個片段中的文本,那麼你可以使用你的函數。

TabFragment1 t1 =TabFragment1.create("HELLO WORLD"); 

你的基地代碼性交執行:

public class TabFragment1 extends Fragment { 
private TextView tv; 

public static TabFragment1 create(String text){ 
    Bundle b = new Bundle(); 
    b.putString("textdata",text); 
    TabFragment1 f = new TabFragment1(); 
    f.setArguments(b); 
    return f; 
} 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.tab_fragment_1, container, false); 
    tv = (TextView) view.findViewById(R.id.textView); 
    if(getArguments()!=null){ 
     tv.setText(getArguments().getString("textdata")); 
    } 

    return view; 
} 

    public void setText(String text){ 
    tv.setText(text); 
} 

} 

在你的活動採用靜態方法來創建這個片段

new Handler().postDelayed(new Runnable(){t1.setText("HELLO WORLD");},300); 
+0

爲什麼它不是被創建?(我認爲使用TabFragment1 t1 =新的TabFragment1();會做)我如何創建它呢? – scapegoat

+0

這只是構造函數。調用onCreateView時您有視圖。閱讀片段生命週期。你不應該保持對你的片段的引用。最佳解決方案是使用EventBus與片段進行通信活動。 –

+0

我明白了。儘管這段代碼的重點是片段被重用於另一個項目,因此函數(get/set)改變了textview文本,這樣我就不需要改變片段的xml了。我還沒有閱讀關於eventbus的內容,但我已經看到有人使用我正在嘗試的以及使用偵聽器或在片段的活動接口上實現。我讀了關於fragment的生命週期,並且在onCreateView上的佈局已經被膨脹之後調用onactivitycreated,這意味着我創建了視圖,不是嗎? – scapegoat