2013-08-01 9 views
5

Jestem nowy w rozwoju Androida i próbuję napisać małą, prostą aplikację. Ta aplikacja nie powinna zrobić nic więcej niż czytanie tekstu z 2 EditTexts i kiedy użytkownik naciśnie przycisk "wyślij" powinien napisać tekst do 2 TextView. Każdy z tych widoków tekstowych znajduje się w jednym fragmencie.Wywołanie findViewById w Fragment w celu znalezienia przycisku zwraca wartość null

Ale nie mogę dodać onClickEvent do przycisku, ponieważ findViewById (R.id.sendTextButton) zawsze zwraca wartość null. Próbowałem uruchomić tę aplikację, ale do tej pory nie znalazłem żadnego rozwiązania.

Oto odnośne CodeBlocks:

Funkcja onCreateView fragmentu, który powinien obsługiwać pierwsze wejście (HeadlineFragment.java):

Button sendButton = null; 
View inflatedView = null; 

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

    this.inflatedView = inflater.inflate(R.layout.headline_view, container, false); 

    sendButton = (Button) inflatedView.findViewById(R.id.sendTextButton); 

    if(sendButton == null) 
    { 
     Log.d("debugCheck", "HeadFrag: sendButton is null"); 
     return inflatedView; 
    } 
    sendButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String headline = ((EditText) v.findViewById(R.id.enterHeadlineText)).getText().toString(); 
      ((TextView) v.findViewById(R.layout.headline_view)).setText(headline); 
     } 
    }); 
    return inflatedView; 
} 

pliku XML z układem (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" 
     android:orientation="vertical"> 

     <EditText 
      android:id="@+id/enterHeadlineText" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:hint="@string/enterHeadlineTextHint" 
      android:maxLines="1" 
      android:inputType="textCapSentences" 
     /> 

     <EditText 
      android:id="@+id/enterMessageText" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:hint="@string/enterMessageTextHint" 
      android:maxLines="1" 
      android:inputType="textCapSentences" 
      /> 

     <Button 
      android:id="@+id/sendTextButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:text="@string/sendTextButton" 
      /> 



     <fragment android:name="com.example.mysecondapplication.HeadlineFragment" 
      android:id="@+id/headlineFragment" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="25dp" 
      /> 

     <fragment android:name="com.example.mysecondapplication.MessageFragment" 
      android:id="@+id/messageFragment" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      /> 



</LinearLayout> 

i plik xml z TextView, który powinien zawierać tekst (headline_view.xml):

<?xml version="1.0" encoding="utf-8"?> 

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/headline_view" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textColor="#FFFFFF" 
    android:text="@string/hello_world" 
/> 

Odpowiedz

7
this.inflatedView = inflater.inflate(R.layout.headline_view, container, false); 

sendButton = (Button) inflatedView.findViewById(R.id.sendTextButton); 

szukasz przycisk z id sendTextButton wewnątrz headline_view.xml. Ale z opublikowanego układu nie ma przycisku z id sendTextButton. `S sposób można uzyskać zerowy

+0

Czy istnieje sposób wyszukiwania w pliku activity_main.xml z wnętrza mojego Fragmentu lub czy muszę uzyskać dostęp do przycisku z innego pliku? –

+0

co chcesz osiągnąć. Dlaczego potrzebny jest przycisk zadeklarowany wewnątrz układu działania wewnątrz fragmentu? Jeśli należy do widoku zwróconego przez fragment, powinieneś zadeklarować go wewnątrz headline_view.xm – Blackbelt

+0

Chciałem ustawić zdarzenia przycisków, które zmieniają zawartość frapowania wewnątrz fragmentów, ponieważ myślałem, że to będzie jakiś dobry styl. Ale teraz spróbuję zrobić to w inny sposób, dzięki! –

1

Twój przycisk jest wewnątrz activity_main.xml, ale jesteś pompowania R.layout.headline_view:

this.inflatedView = inflater.inflate(R.layout.headline_view, container, false); 
+1

@Ahmad dzięki za Edit :) –

1

To jest problem

sendButton.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     String headline = ((EditText) v.findViewById(R.id.enterHeadlineText)).getText().toString(); 
     ((TextView) v.findViewById(R.layout.headline_view)).setText(headline); 
    } 
}); 

View v jest widok kliknięciu, więc of coure v.findViewById() nie działa ..
Aby osiągnąć swój cel możesz zadeklarować, że jesteś EditText i TextView global, w onCreate() użyj inflater do ich utworzenia i metody onClick, której możesz użyć!

0

Musisz ustawić onClickListener wewnątrz Czynności, z której zarządzasz fragmentami, a nie wewnątrz fragmentu, jeśli przycisk nie znajduje się w układzie, który rozpycha Twój fragment.

Powiązane problemy