2017-11-04 15 views
-1

主な活動私はAndroidの初心者です。互換性のないタイプの私は、フラグメントの対象

package com.example.richu.fragment; 
import android.app.Fragment; 
import android.app.FragmentManager; 
import android.app.FragmentTransaction; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 

public class MainActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
} 
public void changeFragment(View view){ 
    Fragment fragment; 
    if(view == findViewById(R.id.button3)){ 
     fragment = new FragmentOne(); 
     FragmentManager fm = getFragmentManager(); 
     FragmentTransaction ft = fm.beginTransaction(); 
     ft.replace(R.id.fragment1,fragment); 
     ft.commit(); 
    } 
    if(view == findViewById(R.id.button4)){ 
     fragment = new FragmentTwo(); 
     FragmentManager fm = getFragmentManager(); 
     FragmentTransaction ft = fm.beginTransaction(); 
     ft.replace(R.id.fragment1,fragment); 
     ft.commit(); 
    } 
} 
} 

MainActivity.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.richu.fragment.MainActivity"> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" 
     tools:layout_editor_absoluteX="2dp" 
     tools:layout_editor_absoluteY="5dp" /> 

    <Button 
     android:id="@+id/button4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" 
     tools:layout_editor_absoluteX="292dp" 
     tools:layout_editor_absoluteY="5dp" /> 

    <fragment 
     android:id="@+id/fragment1" 
     android:name="layout.FragmentOne" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     tools:layout_editor_absoluteX="13dp" 
     tools:layout_editor_absoluteY="13dp" /> 
</LinearLayout> 

FragmentOne.javaを作成しようとしたときに私が得たエラーです

package layout; 
    import android.content.Context; 
     import android.net.Uri; 
     import android.os.Bundle; 
     import android.support.v4.app.Fragment; 
     import android.view.LayoutInflater; 
     import android.view.View; 
     import android.view.ViewGroup; 

     import com.example.richu.fragment.R; 


     public class FragmentOne extends Fragment { 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
           Bundle savedInstanceState) { 
      // Inflate the layout for this fragment 
      return inflater.inflate(R.layout.fragment_fragment_one, container, false); 
     } 

     } 

FragmentOne.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" 
    android:background="@color/colorAccent" 
    tools:context="layout.FragmentOne"> 


</FrameLayout> 

FragmentTwo.java

package layout; 

import android.content.Context; 
import android.net.Uri; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

import com.example.richu.fragment.R; 

public class FragmentTwo extends Fragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.fragment_fragment_two, container, false); 
    } 


} 

FragmentTwo.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" 
    android:background="@color/colorAccent" 
    tools:context="layout.FragmentTwo"> 


    <TextView 
     android:id="@+id/textView" 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:text="Hello" /> 
</FrameLayout> 

私は断片の基礎を学ぼうとしていました。アクティビティに2つのボタンが追加されました。 1つのボタンをクリックするとFragmentOneが読み込まれ、2番目のボタンをクリックするとFragmentTwoが読み込まれます。 MainActivity.javaの次のコードにエラーが示されていますが、そのエラーは互換性のないタイプです。 FragmentOneFragmentTwoは私が作成した2つのフラグメントです。 - fragment = new FragmentOne(); - fragment = new FragmentTwo(); 解決策を見つけてください。ありがとうございました。

答えて

0

メインアクティビティでは、import android.app.Fragment;を使用します。

(FragmentTwoとFragmentOne)import android.support.v4.app.Fragment;を使用します。

あなたのバージョンはあなたに適していると判断する必要があります。

リンクは違いを示しています。 Difference between android.app.Fragment and android.support.v4.app.Fragment

0

問題がobjFragmentとfragmentManager間の断片の種類が一致しないということです。最初はパッケージandroid.support.v4.appから、2番目はandroid.app packageからです。問題を解決するにはgetFragmentManager()getSupportFragmentManager()に変更してください。ただ、コード行を置き換え

FragmentManager fm = getFragmentManager(); 
    FragmentTransaction ft = fm.beginTransaction(); 

android.support.v4.app.FragmentManager fm = getSupportFragmentManager(); 
    android.support.v4.app.FragmentTransaction ft = fm.beginTransaction(); 
関連する問題