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

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

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

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

Android Studio

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

Q&A

解決済

1回答

3017閲覧

FragmentTabHostを使いたいです

nichinichi

総合スコア8

Android

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

Android Studio

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

0グッド

0クリップ

投稿2015/05/05 01:50

編集2015/05/05 01:52

質問させていただきます。

FragmentTabHostを使って、タブでフラグメントを切り替えたいのですが、
エラーが出てしまい上手くいきません。

このサイトの記事を参照して、importを変更したことでひとつエラーが解決しましたが、次に出たエラーが解決できません。

私が試したことはOnFragmentInteractionListenerを3つMainActivityにimplementsしたことですが、エラーが解決できませんでした。

下にLogcatでエラー元となっている部分等を記述します。

アドバイスをいただける方がいましたら、よろしくお願いします。

###■logcatのエラー部分
java.lang.ClassCastException: com.example.yamasaki.tabsample_02.MainActivity@434d1088 must implement OnFragmentInteractionListener
at com.example.yamasaki.tabsample_02.Fragment01.onAttach(Fragment01.java:85)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:907)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)
at android.support.v4.app.FragmentTabHost.onAttachedToWindow(FragmentTabHost.java:283)
at android.view.View.dispatchAttachedToWindow(View.java:13665)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2719)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2727)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2727)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2727)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1576)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1316)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6806)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:813)
at android.view.Choreographer.doCallbacks(Choreographer.java:613)
at android.view.Choreographer.doFrame(Choreographer.java:583)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:799)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5752)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)

■XML

<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_weight="0" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0" /> <FrameLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout>

</android.support.v4.app.FragmentTabHost>

###■MainActivity.java

package com.example.yamasaki.tabsample_02;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;

public class MainActivity extends FragmentActivity implements FragmentTabHost.OnTabChangeListener{

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // FragmentTabHostを取得する FragmentTabHost tabHost = (FragmentTabHost)findViewById(android.R.id.tabhost); tabHost.setup(this, getSupportFragmentManager(), R.id.container); TabHost.TabSpec tabSpec1, tabSpec2, tabSpec3; // TabSpecを生成する tabSpec1 = tabHost.newTabSpec("tab1"); tabSpec1.setIndicator("tab1"); // TabHostに追加 tabHost.addTab(tabSpec1, Fragment01.class, null); // TabSpecを生成する tabSpec2 = tabHost.newTabSpec("tab2"); tabSpec2.setIndicator("tab2"); // TabHostに追加 tabHost.addTab(tabSpec2, Fragment02.class, null); // TabSpecを生成する tabSpec3 = tabHost.newTabSpec("tab3"); tabSpec3.setIndicator("tab3"); // TabHostに追加 tabHost.addTab(tabSpec3, Fragment03.class, null); // リスナー登録 tabHost.setOnTabChangedListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override public void onTabChanged(String tabId) { Log.d("onTabChanged", "tabId: " + tabId); }

}

###■フラグメントの変更した部分

package com.example.yamasaki.tabsample_02;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
//import android.app.Fragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

エラーが示している場所(Fragment内)

@Override public void onAttach(Activity activity) { super.onAttach(activity); try { mListener = (OnFragmentInteractionListener) activity; } catch (ClassCastException e) {

エラーの出ている部分ここから
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
ここまで
}
}

使用環境

AndroidStudio 1.2
最低API 14
ターゲットAPI 21

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

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

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

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

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

guest

回答1

0

ベストアンサー

MainActivityのコードをもう一度よく見てください.
実装しているインタフェースはFragmentTabHost.OnTabChangeListenerだけで,
問題の箇所で要求されているOnFragmentInteractionListenerは実装されていません.

投稿2015/05/05 04:05

swordone

総合スコア20649

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

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

nichinichi

2015/05/05 11:10

回答ありがとうございました。 OnFragmentInteractionListenerを3つのフラグメントの分implementsして、onFragmentInteractionをオーバーライドしたところ実機で動作確認ができました。 ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問