1

真的需要一些建議,不知道這裏出了什麼問題。片段中間(I):從edittext獲取輸入,在片段的textview中設置文本

上下文: 2片段與每個一個TextView和主活動有2按鈕和一個的EditText

目的: 類型喂到主活性和的EditText框 當點擊用於片段1的按鈕,textview將更改爲hello。

問題:

的臉,當輸入Hello到EditText上,然後點擊按鈕1.

logcat的一個運行時錯誤:

E/AndroidRuntime(1291): FATAL EXCEPTION: main 
E/AndroidRuntime(1291): android.view.InflateException: Binary XML file line #29: Error inflating class fragment 
E/AndroidRuntime(1291): android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
E/AndroidRuntime(1291):android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 
E/AndroidRuntime(1291):android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
E/AndroidRuntime(1291): android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
E/AndroidRuntime(1291): com.example.FragmentOne.onCreateView(FragmentOne.java:19) 
E/AndroidRuntime(1291):android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829) 

fragment_one.xml

<?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" 
    android:background="#00ffff"> 

<TextView 
     android:id="@+id/textView1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:text="This is fragment No.1" 
     android:textStyle="bold" /> 
</LinearLayout> 

activity_main。 xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <EditText 
      android:id="@+id/easy" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:ems="10" > 

      <requestFocus /> 
     </EditText> 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:onClick="selectFrag" 
      android:text="Fragment No 1" /> 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:onClick="selectFrag" 
      android:text="Fragment No 2" /> 

     <fragment 
      android:name="com.example.FragmentTwo" 
      android:id="@+id/fragment_place" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"/> 

    </LinearLayout> 

MainActivity.java

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 

    public void selectFrag(View view) { 
     Fragment fr;  
     if(view == findViewById(R.id.button2)) { 
      fr = new FragmentTwo(); 

     }else { 
      fr = new FragmentOne(); 
     } 
     FragmentManager fm = getFragmentManager(); 
     FragmentTransaction fragmentTransaction = fm.beginTransaction(); 
     fragmentTransaction.replace(R.id.fragment_place, fr); 
     fragmentTransaction.commit(); 

    } 

} 

FragmentOne.java

public class FragmentOne extends Fragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) 
    { 
     View view = inflater.inflate(R.layout.fragment_one, container, false); 
     TextView monthlypayment= (TextView) view.findViewById(R.id.textView1); 
     EditText easy = (EditText) inflater.inflate(R.layout.activity_main, container, false).findViewById(R.id.easy); 
     monthlypayment.setText(easy.getText().toString()); 

     return view; 
    } 
} 
+0

你有XML片段,但你也把它添加到容器編程。您實際上沒有容器 – Raghunandan

回答

3

有兩種方法可以將片段添加到活動佈局:

  1. 聲明活動的佈局文件中的片段。

  2. 以編程方式將片段添加到現有的ViewGroup。如果你想片段添加到您需要在XML中使用的ViewGroup容器

這兩種方法都在文檔

http://developer.android.com/guide/components/fragments.html

提及。一般使用FrameLayout。所以有以下在xml

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

活動代碼是罰款。在按鈕上單擊您替換容器中的相應片段。

在您的片段onCreateView中,您可以使片段佈局膨脹並使用視圖對象初始化該佈局的視圖。但你膨脹activity_main.xml這是不需要的。

引用文檔

具體而言,片段可以與 getActivity()訪問活動實例,並容易地執行任務,如發現在 活動佈局

因此,一個視圖EditText你可以使用

EditText easy = (EditText)getActivity().findViewById(R.id.easy); 

或初始化EditTextActivityButton點擊得到EditText的值,然後你可以從Activity合格EditText價值Fragment

Send data from activity to fragment in android

+0

親愛的主席先生,感謝您的評論。它不僅解決了錯誤。但它也非常清楚地解釋了片段是什麼以及如何操縱片段之間的事物。非常感謝你。 – lonelearner

+0

不客氣 – Raghunandan

0

告訴我的onclick方法和容器u有一個片段中......它的兩件事情之一,

無論你是以編程方式添加一個已經存在的視圖,或者你正在嘗試更新沒有處理程序的用戶界面....處理程序可以在碎片中解決,所以我假設我TS第一個....