• 締切済み

AndroidのFragmentの画面遷移について

AndroidのFragmentを使った画面遷移のプログラムが、実機で動作確認をしようとした瞬間、「問題が発生したため・・・・」と出て、動きません。ビルドは問題ないので、レイアウトが怪しいと思うのですが、わかりません。 ----------------------fragment_first.xml------------------------ <FrameLayout 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" tools:context="com.example.ya_ka_do.test001.FirstFragment"> <!-- TODO: Update blank fragment layout --> <TextView android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/hello_blank_fragment" /> </FrameLayout> ----------------------MainActivity.java-------------------------- protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); FirstFragment firstfragment = new FirstFragment(); fragmentTransaction.replace(R.id.container,firstfragment); fragmentTransaction.commit(); } です。他は、とくに変更してないのですがどこか間違ってますか?

みんなの回答

  • HNEX
  • ベストアンサー率62% (43/69)
回答No.2

activity_main.xmlにcontainerのIDを持つViewGroupが無いんじゃないでしょうか? 示されているfragment_first.xmlがactivity_main.xmlの間違いだとしたら、containerのIDを持つViewを別のコンテナ系のViewに変えてみるといいかもしれません

noname#247307
noname#247307
回答No.1

fragment_first.xmlは、アクティビティのレイアウトですか? これに別のレイアウトがフレグメントとして追加されるのでしょうか。ベースとなるアクティビティのレイアウトと、それに組み込まれる側のレイアウトがどうなっているのか、これだけではよくわからないのですが。 また、フラグメント側のFirstFragmentクラスのコードはどうなっていますでしょう。

関連するQ&A

  • マップとボタン同時表示|AndroidStudio

    下記のサイトを参考にしてボタンとマップを同時に表示させるxmlを記述したつもりなのですが、 実行すると一瞬だけボタンが映り、その後はマップのみが表示されました。 https://groups.google.com/forum/?hl=ja&fromgroups#!topic/shikokugtug/NGseYmNKQQQ 何がおかしいのでしょうか。 記述したxmlは以下の通りです。 ちなみにfragmentをwrap_contentに変更しても同じでした。 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent"> <fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:context="com.example.~.MapsActivity" android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" android:name="com.google.android.gms.maps.SupportMapFragment" /> <LinearLayout android:id="@+id/level" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/large_up" android:text="@string/large_up" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/large_down" android:text="@string/large_down" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/small_down" android:text="@string/small_down" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/small_up" android:text="@string/small_up" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> </FrameLayout>

  • android javaについて質問です

    android javaについて質問です public class MailMotion extends Activity implements OnTouchListener, OnClickListener { private FrameLayout frameLayout01; private ImageView target; private Button trash; private int targetLocalX; private int targetLocalY; private int screenX; private int screenY; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.motion); frameLayout01 = (FrameLayout)findViewById(R.id.FrameLayout01); target = (ImageView)findViewById(R.drawable.toudai); target.setOnTouchListener(this); trash = (Button)findViewById(R.id.trash); trash.setOnClickListener(this); } @Override public boolean onTouch(View v, MotionEvent event) { int x = (int)event.getRawX(); int y = (int)event.getRawY(); switch(event.getAction()) { case MotionEvent.ACTION_DOWN: targetLocalX = target.getLeft(); targetLocalY = target.getTop(); screenX = x; screenY = y; break; case MotionEvent.ACTION_MOVE: int diffX = screenX - x; int diffY = screenY - y; targetLocalX -= diffX; targetLocalY -= diffY; target.layout(targetLocalX, targetLocalY, targetLocalX + target.getWidth(); targetLocalY + target.getHeight()); screenX = x; screenY = y; break; case MotionEvent.ACTION_UP: int trashLeft = trash.getLeft() + trash.getWidth()/2; int trashTop = trash.getTop() + trash.getHeight()/2; int targetRight = target.getLeft() + target.getWidth(); int targetBottom = target.getTop() + target.getHeight(); if (targetRight > trashLeft && targetBottom > trashTop) { frameLayout01.removeView(target); } break; } return true; } @Override public void onClick(View v) { int childCount = frameLayout01.getChildCount(); if(childCount == 1) { frameLayout01.addView(target); }}} 上記のものを起動させると、nullpointが出てきます 前回の質問をもとに、デバッグをしてみたのですが、touchonLisnerのところでエラーが起きました 勉強不足なので原因がよくわかっておりません 間違いを教えていただけないでしょうか? stringは <drawable name="custom_button">#ffff00</drawable> で設定して、おそらく大丈夫だと思います <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/FrameLayout01" android:layout_height="match_parent" android:layout_width="match_parent" android:layout_weight="10" > <Button android:layout_gravity="bottom|right" android:layout_width="30dp" android:layout_height="30dp" android:id="@+id/trash" android:background="@drawable/custom_button"/> <ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/aaaa" android:src="@drawable/aaa" android:layout_gravity="center" /> </FrameLayout>

    • ベストアンサー
    • Java
  • すいません、Android初心者です

    最近EclipseでAndroidの勉強を始めたものです。 早速ですが、以下のソースを実行すると、何故か「[アプリ名](パッケージ名)が予期せず停止 しました。やり直してください」と出てきます・・・コメントの部分をコメントアウトするとうまく動作 するのですが、何故でしょうか?なお、ターゲットは2.3.3のものを利用しています。 /* +++ Javaソース +++ */ import android.app.Activity; import android.os.Bundle; import android.widget.Button; public class Android_Test01Activity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Button button1 = (Button) findViewById(R.id.button1); //button1.setText("button1"); setContentView(R.layout.main); } } /* +++ XMLソース +++ */ <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> </LinearLayout>

    • ベストアンサー
    • Java
  • androidアプリ開発で困っています。

    すれ違い通信ができるandroidアプリ開発をしているのですが、どうしてもわからないところがあります。 現在、googleのサンプルアプリであるbluetoothchatをいじって、起動時の画面にマップを加えるように改造しています。 そのために起動時に表示するxmlに <fragment android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" class="com.google.android.gms.maps.SupportMapFragment"/> を加えてマップが表示できるようにしたのですが、実端末でプログラムを動かそうとしたところ最初の黒い画面が出た直後に「問題が発生しました」という表示がでてプログラムがとまってしまいます。 buttonなど、fragment以外のものを追加した場合は問題は発生しませんでした。 どうしてもbluetoothcatを組み合わせてマップが表示できるようにしたいのですが、どうすればよいでしょうが?

  • Android開発についての初心者質問です。

    Android開発についての質問です。初心者でAndroidアプリを作っています。 初心者でAndroidアプリを作っています。 XMLからの実装をしたいのですが、 やり方がよく分かりません。 どなたかご教授頂けないでしょうか? よろしくお願いいたします。 やりたいことは、imageButtonをクリックしたら imageViewの画像部分が変わる。 imageButton1をクリック → imageView1が表示 imageButton2をクリック → imageView2が表示 デフォルトでは、imageView1が表示している状態 buttonのリスナーを登録するのが関の山で、ボタンクリック時の imageView表示切替処理等ちんぷんかんぷんです。 XMLはこういった感じで書いてます。 よろしくお願いいたします。 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> <FrameLayout android:id="@+id/frameLayout1" android:layout_height="wrap_content" android:foregroundGravity="center" android:layout_width="match_parent" > <ImageView android:layout_height="wrap_content" android:src="@drawable/icon" android:id="@+id/imageView1" android:layout_gravity="center" android:layout_width="match_parent" > </ImageView> </FrameLayout> <LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:layout_width="match_parent" > <ImageButton android:id="@+id/imageButton1" android:layout_height="wrap_content" android:src="@drawable/icon" android:layout_width="wrap_content" > </ImageButton> <ImageButton android:id="@+id/imageButton2" android:layout_width="wrap_content" android:src="@drawable/icon" android:layout_height="wrap_content" > </ImageButton> <ImageButton android:id="@+id/imageButton3" android:layout_width="wrap_content" android:src="@drawable/icon" android:layout_height="wrap_content" > </ImageButton> </LinearLayout> </LinearLayout>

  • Androidアプリ Listviewが

    アドバイスください。初心者です。Listviewを作りたいだけなんです。”データがありません”と表示されてしまいます・・。 ●layout(.xml) <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ListView android:id="@+id/android:list" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <TextView android:id="@+id/android:empty" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="データが存在しません"/> </LinearLayout> ●listview(.java) import android.app.Activity; import android.app.ListActivity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; //ListActivityを継承 import com.example.yamato.myapplication001.R; public class listview extends ListActivity { private String[] items = { "a10", "a11", "a21", "b10" }; //(2) // private String[] items = {};(2) @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //リストビューを含むレイアウト指定(1) setContentView(R.layout.activity_main_activity001); // ArrayAdapterオブジェクトの生成 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items); // Adapterの指定 setListAdapter(adapter); // ListActivity内部にあるListViewオブジェクトを取得する場合 ListView listView = getListView(); // ListViewオブジェクトを取得したので以下のメソッド等が利用できる // 選択する要素の位置の指定 listView.setSelection(3); } } ソースは参考本からです。

  • androidのサンプルソースについての質問

    このページを参考に学習をしていたのですが http://libro99.appspot.com/index3?id=29&page=2&label=android .javaに追加するぶぶんで text.setText(selradio.getText()); ~~~~ の波線の部分のエラーをどうしても消すことができず 困っています package test.radiobutton; import android.app.Activity; import android.os.Bundle; import android.widget.RadioButton; import android.widget.RadioGroup; public class radiobuttontest extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); RadioGroup group = (RadioGroup)this.findViewById(R.id.group); RadioButton radio1 = (RadioButton)this.findViewById(R.id.radio1); RadioButton radio2 = (RadioButton)this.findViewById(R.id.radio2); radio2.setChecked(true); int selid = group.getCheckedRadioButtonId(); RadioButton selradio = (RadioButton)this.findViewById(selid); text.setText(selradio.getText()); } } main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <RadioGroup android:id="@+id/group" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RadioButton android:text="@string/radio1_label" android:id="@+id/radio1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RadioButton android:text="@string/radio2_label" android:id="@+id/radio2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RadioGroup> </LinearLayout> これ以上はかけませんがページの指示通りにやってるつもりです どなたかご教授お願いします

    • ベストアンサー
    • Java
  • androidプログラムのエラーについて

    自分は今、本を読みながらandroidプログラミングの勉強をしています。 XMLファイルをよく見直して間違いは見当たらないのですが 起動しようとすると「your project contains error」と表示されます。 コンソールには「No resource found that matches the given name (at 'title' with value '@string/menu_settings')」と書いてあります。 ネットで調べたらソースをクリーンすればいいと書いてありましたが クリーンすると今度はjavaファイルにエラーが起きます。 以下が自分が打ち込んだXMLのソースコードです。 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:text="お申込みフォーム" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:text="お名前 :" android:layout_width="70sp" android:layout_height="wrap_content"/> <EditText android:id="@+id/name" android:layout_width="200dp" android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:text="住所 :" android:layout_width="70sp" android:layout_height="wrap_content"/> <EditText android:id="@+id/address" android:layout_width="200dp" android:layout_height="wrap_content"/> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:text="生年月日 :" android:layout_width="70sp" android:layout_height="wrap_content"/> <Spinner android:id="@+id/month" android:entries="@array/month" android:layout_width="85dp" android:layout_height="wrap_content"/> <TextView android:text="月" android:layout_width="20sp" android:layout_height="wrap_content"/> <Spinner android:id="@+id/day" android:entries="@array/day" android:layout_width="85dp" android:layout_height="wrap_content"/> <TextView android:text="日" android:layout_width="20sp" android:layout_height="wrap_content"/> </LinearLayout> </LinearLayout> どうかよろしくお願い致します。

  • android開発 map コンテナのエラー

    今学校でandroid開発の課題が出ているのですが、エラーの原因が分かりません。 javaの勉強はしていますが、android開発は初めてです。 http://note.chiebukuro.yahoo.co.jp/detail/n237584 上のURLのmapを作成しているのですが、MainActivity.javaでエラーが検出されました。 public class MainActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment()) .commit(); } } ・ ・ ・ 上のcontainerでエラーとなりました。どうしたらエラーがなくなるのか教えてください。

  • Bootstrapの利用について

    新規で立ち上げたまっさらのプロジェクトです。 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); (1)Button btn = new Button(this); (2)Button btn = (Button)findViewById(R.id.btnBig); } } (1)を使うと強制終了しないのに (2)を使うと強制終了してしまいます。 XMLファイルには <com.beardedhen.androidbootstrap.BootstrapButton android:id="@+id/btnBig" android:layout_width="fill_parent" android:layout_height="200dp" android:layout_margin="10dp" android:text="Primary" bootstrapbutton:bb_icon_right="fa-cloud" bootstrapbutton:bb_roundedCorners="true" bootstrapbutton:bb_type="primary" bootstrapbutton:bb_text_gravity="right" /> というシンプルなソースのみです。 (1)で起動しているのに (2)で強制終了する理由は何故なのでしょうか? XMLを <Button android:id="@+id/btnBig" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="test" /> と書き換えると正常に起動するのは確認済みです。 AndroidどころかJAVAもまだ使いはじめて数日なのですが 言語特有の何かがあるのでしょうか?

    • ベストアンサー
    • Java

専門家に質問してみよう