2016-10-18 8 views
3

「ナビゲーション」ドロワーからのフラグメントには、2つの異なるアクティビティーにナビゲートするためのボタンとして動作する2つのRelativeLayoutsが含まれています。 これは、そのレイアウトのためのコードです:浮動アクションボタンを使用した場合の致命的な例外

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/activity_new_client" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="judeochalifu.teylur.activities.NewClientActivity"> 


<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_margin="14dp" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:src="@drawable/ic_menu_send" 
    app:fabSize="normal" /> 

</RelativeLayout> 

(関連する)フラグメントのコードは次のようになります。

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:background="#B3E5FC" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 


<RelativeLayout 
    android:id="@+id/existingClientChoiceLayout" 
    android:layout_width="match_parent" 
    android:layout_height="300dp" 
    android:layout_alignParentTop="true" 
    android:background="@color/colorAccent"> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="126dp" 
     android:text="Existing Client" 
     android:textColor="@android:color/white" 
     android:textSize="20sp" 
     android:textStyle="bold" /> 
</RelativeLayout> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:id="@+id/newClientChoiceLayout" 
    android:layout_height="300dp" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:background="@color/light_red"> 

    <TextView 
     android:id="@+id/textView7" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="138dp" 
     android:text="New Client" 
     android:textColor="@android:color/white" 
     android:textSize="20sp" 
     android:textStyle="bold" /> 

    </RelativeLayout> 
</RelativeLayout> 

活動のレイアウトでは、existingClientChoiceLayoutからに移動することになっています、私はこれを持っています:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    View rootView = inflater.inflate(R.layout.new_entry_layout, container, false); 
    existingClientChoiceLayout = (RelativeLayout) rootView.findViewById(R.id.existingClientChoiceLayout); 
    newClientChoiceLayout = (RelativeLayout) rootView.findViewById(R.id.newClientChoiceLayout); 

    existingClientChoiceLayout.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      startActivity(new Intent(mContext, ExistingClientsViewActivity.class)); 
      Toast.makeText(mContext, "Clicked", Toast.LENGTH_SHORT).show(); 
     } 
    }); 

    newClientChoiceLayout.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      startActivity(new Intent(mContext, NewClientActivity.class)); 
     } 
    }); 

    return rootView; 
} 

、私はコードのすべては、私がナビゲーションメニューから、このフラグメントのためにオプションをクリックfine.When、私は奇妙ERRを得るに見える実行またはメッセージ:

とjava.lang.ClassCastException:私はFABを削除した場合android.support.v7.widget.AppCompatTextViewが、しかしandroid.widget.RelativeLayout

E/AndroidRuntime: FATAL EXCEPTION: main 
       Process: judeochalifu.teylur, PID: 4746 
       java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.RelativeLayout 
        at judeochalifu.teylur.fragments.NewEntryFragment.onCreateView(NewEntryFragment.java:47) 
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:2080) 
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108) 
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290) 
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801) 
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1677) 
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:536) 
        at android.os.Handler.handleCallback(Handler.java:751) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6077) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
Application terminated. 

にキャストすることはできません、すべて正常に動作します。

私は類似のタイトルで関連する質問を検索しましたが、私の特定の問題と一致するものはありません。

+0

を再構築します。あなたはfabボタンを必要としません、それを削除しますが、必要ならばそれを宣言してください。次に、fabがnullでないかどうかをチェックし、そうでない場合は、それにクリックリスナーを追加します。 – Stanojkovic

+0

サンプルコードセクションのそれぞれにファイル名を付けてください。たとえば、FAB XMLがどこで使用されているかはすぐには分かりません。 – Knossos

+0

は、 に入っている行を表示できますonCreateView(NewEntryFragment.java:47) –

答えて

1

同様の問題に直面し、これは私が固定する方法である:(ビルド]メニューから)

  1. クリーンプロジェクト
  2. 場合は、プロジェクト(ビルド]メニューからに)
関連する問題