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

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

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

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

継承

継承(インヘリタンス)はオブジェクト指向プログラミングに存在するシステムです。継承はオブジェクトが各自定義する必要をなくし、継承元のオブジェクトで定義されている内容を引き継ぎます。

Android Wear

Android Wearとは、Googleが発表した腕時計型ウェアラブルデバイス(スマートウォッチ)向けのプラットフォームです。GoogleのAndroid OSをベースにしており、情報の入手・管理などを行うことができます。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

Q&A

解決済

1回答

3745閲覧

WearableActivityを継承したクラスのレイアウトにfragmentをinflateできない

EnderMan

総合スコア7

Android

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

継承

継承(インヘリタンス)はオブジェクト指向プログラミングに存在するシステムです。継承はオブジェクトが各自定義する必要をなくし、継承元のオブジェクトで定義されている内容を引き継ぎます。

Android Wear

Android Wearとは、Googleが発表した腕時計型ウェアラブルデバイス(スマートウォッチ)向けのプラットフォームです。GoogleのAndroid OSをベースにしており、情報の入手・管理などを行うことができます。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

0グッド

0クリップ

投稿2019/08/21 07:53

編集2019/08/21 07:58

前提・実現したいこと

Wear OS 向けにスタンドアロンアプリを作成しようとしています。
fragmentを使用してレイアウトを作成しようとしたところfragmentを読み込めない旨のエラーメッセージが出力されました。

発生している問題・エラーメッセージ

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplicationtest/com.example.myapplicationtest.Main2Activity}: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class fragment Caused by: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class fragment Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class fragment Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class com.example.myapplicationtest.BlankFragment that is not a Fragment    Caused by: java.lang.ClassCastException

該当のソースコード

BlankFragment.kt

Kotlin

1 2package com.example.myapplicationtest 3 4import android.os.Bundle 5import androidx.fragment.app.Fragment 6import android.view.LayoutInflater 7import android.view.View 8import android.view.ViewGroup 9 10class BlankFragment : Fragment() { 11 override fun onCreateView( 12 inflater: LayoutInflater, container: ViewGroup?, 13 savedInstanceState: Bundle? 14 ): View? { 15 // Inflate the layout for this fragment 16 return inflater.inflate(R.layout.fragment_blank, container, false) 17 } 18}

fragment_blank.xml

xml

1<?xml version="1.0" encoding="utf-8"?> 2<FrameLayout 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=".BlankFragment"> 7 <TextView 8 android:layout_width="match_parent" 9 android:layout_height="match_parent" 10 android:gravity="center" 11 android:text="@string/hello_blank_fragment" /> 12</FrameLayout>

Main2Activity.kt

Kotlin

1package com.example.myapplicationtest 2 3import android.os.Bundle 4import android.support.wearable.activity.WearableActivity 5import android.view.View 6import android.widget.Button 7 8class Main2Activity : WearableActivity() { 9 override fun onCreate(savedInstanceState: Bundle?) { 10 super.onCreate(savedInstanceState) 11 setContentView(R.layout.activity_main2) 12 13 //リスナークラスのインスタンスを生成 14 val listener = HelloListener() 15 //リソースからボタンの読み込み 16 val button_click = findViewById<Button>(R.id.button2) 17 //ボタンにリスナを設定 18 button_click.setOnClickListener(listener) 19 } 20 //HalloListenerクラスの定義 21 private inner class HelloListener : View.OnClickListener{ 22 //onClickメソッドの定義 23 override fun onClick(view: View){ 24 //idのR値に応じて処理の分岐 25 when(view.id){ 26 //ボタン動作 27 R.id.button2 -> { 28 finish() 29 } 30 }//End of When 31 } 32 } 33}

activity_main2.xml

xml

1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:tools="http://schemas.android.com/tools" 5 xmlns:app="http://schemas.android.com/apk/res-auto" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent" 8 tools:context=".Main2Activity"> 9 <fragment 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:name="com.example.myapplicationtest.BlankFragment" 13 android:id="@+id/fragment" 14 android:layout_marginTop="8dp" 15 app:layout_constraintTop_toTopOf="parent" android:layout_marginBottom="8dp" 16 app:layout_constraintBottom_toTopOf="@+id/button2" app:layout_constraintStart_toStartOf="parent" 17 android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp"/> 18 <Button 19 android:text="Button" 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:id="@+id/button2" android:layout_marginTop="8dp" 23 app:layout_constraintTop_toTopOf="parent" android:layout_marginBottom="8dp" 24 app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" 25 android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp"/> 26</androidx.constraintlayout.widget.ConstraintLayout>

試したこと

ADSのツールで追加したBlankFragmentをMain2Activityのレイアウトに表示させようとしましたがエラーでAppStop。
元アプリはNewProjectで作成できるWear向けBlankAppにMain2Activityを追加してActivity間をボタンで行き来できるようにしただけの状態です。提示したソースのほかにMainActivityとレイアウト/リソース関連ファイルが存在しています。
WearableActivityではなくAppCompatActivityを継承させてスマホ形式で実行した場合すんなりと動いてしまいます。

検索しても恐らくFragmentを扱ううえで基本的なことすぎて原因が判らず助けてください。

補足情報(FW/ツールのバージョンなど)

テスト環境:Wear OS Round Chin API 26 Resolution 300*320:hdpi
ビルド環境: Android Gradle Plugin Version 3.5.0 Gradle Version 5.5.1

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

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

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

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

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

guest

回答1

0

ベストアンサー

WearableActivityはFragmentActivityを継承していませんので、サポートライブラリ(もしくはAndroidX)のFragmentを利用することはできません。Android標準のandroid.app.FragmentであればWearableActivityでも利用できますが、こちらは現在非推奨となっています。

ドキュメントによると、WearableActivityは利用せず、AppCompatActivityにAmbientModeの機能を追加する方法がサポートライブラリに用意されていて、現在はこちらが推奨されています。詳しくはドキュメントをご参照ください。

投稿2019/08/22 02:56

kakajika

総合スコア3131

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

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

EnderMan

2019/08/22 04:19

回答ありがとうございます。 FragmentActivityを継承しているものだとばかり思っていました。 (Webで拾えるサンプルの殆どが普通にWearableActivityでFragmentを使用していたので。) 現在の推奨も教えていただきありがとうございます。 (Wear関連の情報が少なすぎて困っていたので助かりました。)
EnderMan

2019/08/22 23:38 編集

回答に追記しましてGoogleのドキュメントが間違っておりAppCompatActivityではなくFragmentActivityを使用するのが正のようです。 AppCompatActivityはWatchハードウェアをサポートしていないためAppstopエラーとなってしまいます。 AlwaysOnサンプルのJavaソースはFragmentActivityになっているのですがドキュメントがFragmentActivityに実装しろと書いてるのに提示ソース部分がAppCompatActivity表記になっていました。 色々と酷いドキュメントです。 FragmentActivityを使用することで狙った動作を確認できました。
kakajika

2019/08/23 03:46

なるほど、情報ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問