質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.51%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

解決済

1回答

1805閲覧

FragmentとActivityを連動させたいのですが、どうすればいいのかがわからなくなってしまいました。

edoooooo

総合スコア476

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

0クリップ

投稿2016/10/01 04:34

編集2016/10/01 07:37

簡単に文章で説明するとMyFragmentActivity1からスタートして、MyFragmentActivity1のメモを追加する(Button)を押すとMyFragmentActivity2に移動して、MyFragmentActivity2のEditTextにメモを入力してするところまではできているのですが、その後のButtonを押すとEditTextのデータが、データベースはまだ使えないためButtonの下に設置したLinearLayoutに表示したいのですが、それができません。
どうすれば良いのでしょうか?

Main Activity

java

1package com.example.kanehiro.fragmentapplication; 2 3import android.support.v4.app.Fragment; 4import android.support.v4.app.FragmentActivity; 5import android.support.v4.app.FragmentTransaction; 6import android.os.Bundle; 7 8public class MainActivity extends FragmentActivity { 9 10 @Override 11 protected void onCreate(Bundle savedInstanceState) { 12 super.onCreate(savedInstanceState); 13 setContentView(R.layout.activity_main); 14 15 Fragment fragment=getSupportFragmentManager().findFragmentById(R.id.contents); 16 if (fragment==null){ 17 fragment=new MyFragment1(); 18 FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction(); 19 fragmentTransaction.add(R.id.contents,fragment); 20 fragmentTransaction.commit(); 21 } 22 } 23}

MyFragmentActivity2

package com.example.kanehiro.fragmentapplication; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; public class MyFragment2 extends Fragment implements View.OnClickListener { //とりあえずここにさらなるステップの変数 private EditText mInputMessage; private Button mSendMessage; private LinearLayout mMessageLog; private TextView mMemoMessage; public MyFragment2() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.fragment_my_fragment2,container,false); //何に使うのかわからん Button btn = (Button) view.findViewById(R.id.button); view.findViewById(R.id.send_message).setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ } }); return view; } @Override public void onClick(View v) { if(v.equals(mSendMessage)) { if(mSendMessage==null||mSendMessage.length()==0){ Toast.makeText(getActivity(),"文字が入力されていません",Toast.LENGTH_SHORT).show(); }else { //新しくエラーを消すために作った場所 //XMLのViewを初期化する // ActivityからFragmentを取得 FragmentManager fragmentManager = getActivity().getSupportFragmentManager(); Fragment fragment = fragmentManager.findFragmentById(R.id.send_message); // FragmentのViewを取得 View view = fragment.getView(); //この下のコードに Can not resolve method 'findViewById'が出ます。 mInputMessage = (EditText)view.findViewById(R.id.input_message);//ユーザーが入力するフィールド mSendMessage = (Button) view.findViewById(R.id.send_message);//SENDボタン mMessageLog = (LinearLayout)view.findViewById(R.id.message_log);//入力履歴を表示するレイアウト mMemoMessage = (TextView) view.findViewById(R.id.memo_message);//メモの履歴 //この次はデータベースや mSendMessage.setOnClickListener(this); } } } }

MyFragmentActivity1

java

1package com.example.kanehiro.fragmentapplication; 2 3import android.os.Bundle; 4import android.support.v4.app.Fragment; 5import android.support.v4.app.FragmentTransaction; 6import android.view.LayoutInflater; 7import android.view.View; 8import android.view.ViewGroup; 9 10/** 11 * A simple {@link Fragment} subclass. 12 */ 13public class MyFragment1 extends Fragment { 14 public MyFragment1() { 15 16 } 17 @Override 18 public View onCreateView(LayoutInflater inflater, ViewGroup container, 19 Bundle savedInstanceState) { 20 21 View view=inflater.inflate(R.layout.fragment_my_fragment1,container,false); 22 view.findViewById(R.id.button).setOnClickListener(new View.OnClickListener(){ 23 @Override 24 public void onClick(View v){ 25 Fragment fragment=new MyFragment2(); 26 FragmentTransaction fragmentTransaction=getFragmentManager().beginTransaction(); 27 fragmentTransaction.replace(R.id.contents,fragment); 28 fragmentTransaction.addToBackStack(null); 29 fragmentTransaction.commit(); 30 //もともとの処理の方画面とは変えよう 31 //新しいの作ろう とりあえず押すだけのやつ メモを追加するにしよう 32 //だからButtonのTextだけでいい 33 } 34 }); 35 return view; 36 } 37}

fragment_my_fragment1

xml

1<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:gravity="center" 6 android:orientation="vertical" 7 android:background="#419932" 8 tools:context="com.example.kanehiro.fragmentapplication.MyFragment1"> 9 10 <Button 11 android:id="@+id/button" 12 android:layout_width="211dp" 13 android:layout_height="wrap_content" 14 android:layout_gravity="center_horizontal" 15 android:text="メモを追加する"/> 16 17</LinearLayout>

fragment_my_fragment2

java

1<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.example.kanehiro.fragmentapplication.MyFragment2"> 11 12 <EditText 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:id="@+id/input_message" 16 android:layout_alignParentLeft="true" 17 android:layout_alignParentStart="true" 18 android:layout_toLeftOf="@+id/send_message" 19 android:layout_toStartOf="@+id/send_message" /> 20 21 <Button 22 android:id="@+id/send_message" 23 android:layout_width="wrap_content" 24 android:layout_height="wrap_content" 25 android:layout_alignParentEnd="true" 26 android:layout_alignParentRight="true" 27 android:text="SEND"/> 28 29 <ScrollView 30 android:layout_width="fill_parent" 31 android:layout_height="fill_parent" 32 android:layout_alignParentLeft="true" 33 android:layout_alignParentStart="true" 34 android:layout_below="@+id/input_message"> 35 <LinearLayout 36 android:layout_width="fill_parent" 37 android:layout_height="wrap_content" 38 android:orientation="vertical" 39 android:id="@+id/message_log"> 40 41 <TextView 42 android:id="@+id/memo_message" 43 android:layout_width="wrap_content" 44 android:layout_height="wrap_content" 45 android:layout_gravity="start" 46 android:text="memo一覧"/> 47 </LinearLayout> 48 </ScrollView> 49 50</RelativeLayout>

activity_main_xml

xml

1<?xml version="1.0" encoding="utf-8"?> 2<merge xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 tools:context=".MainActivity"> 7 8 <FrameLayout 9 android:id="@+id/contents" 10 android:paddingBottom="@dimen/activity_vertical_margin" 11 android:paddingLeft="@dimen/activity_horizontal_margin" 12 android:paddingRight="@dimen/activity_horizontal_margin" 13 android:paddingTop="@dimen/activity_vertical_margin" 14 android:layout_width="match_parent" 15 android:layout_height="match_parent" /> 16</merge>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

Fragmentの中ですべて処理を書くならMainActivityに何かを書く必要なんてありません。

やりたいことを明確に書く方法がわからないのはいいですが、正しい情報を書きましょう。
質問にはMyFragmentActivity1やMyFragmentActivity2は存在しません。
また、質問を呼んでも結局何ができないのかわかりません。

投稿2016/10/01 06:02

yona

総合スコア18155

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

edoooooo

2016/10/01 07:38

申し訳ありませんでした。丁寧な説明に変更し、MyFragment1、2をMyFragmentActivity1、2としました。どうぞよろしくお願いします。
edoooooo

2016/10/01 09:20 編集

TextView memoMessage=new TextView(this); memoMessage.setText(inputText); mMessageLog.addView(memoMessage,0); のコードを追加することを忘れていました。 しかしthisをnew TextView()メソッドの引数としたところでそこがエラーとなってしまっているため別に質問をいたしました。どうぞよろしくお願いします。 一通り解決したため質問を終わらせていただきます。 https://teratail.com/questions/49929?complete=
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.51%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問