2013-08-01 1 views
5

私はアンドロイド開発には初めてです。私は小さくて簡単なアプリケーションを作成しようとしています。 このアプリケーションは、2つのEditTextsからテキストを読み取るだけです。ユーザーが「送信」ボタンを押すと、2つのTextViewにテキストを書き込む必要があります。これらのTextViewはそれぞれ1つのフラグメントにあります。ボタンを見つけるためにフラグメント内のfindViewByIdを呼び出すとnullが返されます

しかし、findViewById(R.id.sendTextButton)は常にnullを返すため、ボタンにonClickEventを追加できません。私はこのアプリケーションを働かせようとしましたが、今は解決策を見つけることができませんでした。

断片のonCreateView機能、最初の入力(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; 
} 

レイアウト(activity_main.xml持つXMLファイル):

:テキスト(headline_view.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> 

とのTextViewとxmlファイル、 10

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

答えて

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

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

あなたはheadline_view.xml内部ID sendTextButtonとボタンを探しています。しかし、投稿したレイアウトからは、idがsendTextButtonのButtonはありません。あなたはヌル

+0

私のFragment内からactivity_main.xmlを見る方法はありますか、別のファイルからボタンにアクセスする必要はありますか? –

+0

あなたは何をしたいですか?フラグメント内のアクティビティレイアウト内に宣言されたボタンが必要なのはなぜですか?それがフラグメントによって返されたビューに属している場合は、headline_view.xmの中で宣言する必要があります – Blackbelt

+0

ボタンイベントを設定したいと思いました。フラグメントの中身を変更するのは、これが良いスタイルだと思ったからです。しかし、私は今、何か他の方法でそれをやろうとします、ありがとう! –

1

あなたのボタンを取得That`s方法がactivity_main.xml内ですが、R.layout.headline_viewを膨張している:ビューVはそう、ビュークリックされた問題

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); 
    } 
}); 

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

@Ahmadが編集のために感謝します:) –

1

あなたのターゲットを達成するためにあなたはあなたのEditTextとTextViewを宣言できます。onCreate()でインフレータを使用してそれらをインスタンス化し、onClickメソッドを使用することができます。

0

フラグメントが膨らんでいるレイアウトにボタンがない場合は、フラグメントを管理しているアクティビティ内にonClickListenerを設定する必要があります。

関連する問題