2016-07-01 9 views
-1
//Main activity.java 

package com.example.sahilnitish.easyyagriculture; 
import android.app.Activity; 
import android.app.FragmentManager; 
import android.app.FragmentTransaction; 
import android.support.v4.app.Fragment; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 

public class MainActivity extends Activity 
{ 
boolean status=false; 
    Button bn; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) 
{  
super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     bn=(Button)findViewById(R.id.bn); 
     bn.setOnClickListener(new View.OnClickListener() 
{ 
      @Override 
      public void onClick(View v) 
      { 
       FragmentManager fragmentManager = getFragmentManager(); 
       FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
       if(!status) 
       { 
        Fragmentone f1 =new Fragmentone(); 
        fragmentTransaction.add(R.id.fragment_container,f1); 
        fragmentTransaction.commit(); 
        bn.setText("Load Second fragment"); 
        status=true; 
       } 
       else 
       { 
        FragmentTwo f2 = new FragmentTwo(); 
        fragmentTransaction.add(R.id.fragment_container,f2); 
        fragmentTransaction.commit(); 
        bn.setText("Load first fragment"); 
        status=false; 
       } 
      } 
     }); 
    } 
} 

エラー: -のAndroid Studioの2.1リンクフラグメント

Error:(29, 40) error: no suitable method found for add(int,Fragmentone) method FragmentTransaction.add(Fragment,String) is not applicable (argument mismatch; int cannot be converted to Fragment) method FragmentTransaction.add(int,Fragment) is not applicable (argument mismatch; Fragmentone cannot be converted to Fragment)

fragmentone.java

package com.example.sahilnitish.easyyagriculture; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

/** 
* A simple {@link Fragment} subclass. 
*/ 
public class Fragmentone extends Fragment 
{ 
    public Fragmentone() 
    // Required empty public constructor 
    } 

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

あなたの 'Fragmentone'を投稿してください。このエラーメッセージは、 'Fragmentone'が正しいクラスを拡張していないことを伝えています。 –

+0

このような質問を投稿すると、エラーがどの行で不平を言っているのかを指摘することができます。 –

+0

fragmentTransaction.add(R.id.fragment_container、f1);これはエラーラインです@GleenHowes –

答えて

0

あなたがサポート断片とFragmentManagerを使用しています。

MainActivityでActivityCompatを拡張し、supportFragmentManagerを使用するか、フラグメントが「通常の」フラグメントを延長できるようにします。

//Main activity.java 
package com.example.sahilnitish.easyyagriculture; 

import android.support.v7.app.AppCompatActivity; 
import android.support.v4.app.FragmentTransaction; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.os.Bundle; 
import android.view.View; 

import android.widget.Button; 

public class MainActivity extends AppCompatActivity 
{ 

    boolean status=false; 
    Button bn; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 

     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_main); 
     bn=(Button)findViewById(R.id.bn); 

     bn.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       FragmentManager fragmentManager = getSupportFragmentManager(); 

       FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
       if(!status) 
       { 
        Fragmentone f1 =new Fragmentone(); 

        fragmentTransaction.add(R.id.fragment_container,f1); 
        fragmentTransaction.commit(); 
        bn.setText("Load Second fragment"); 
        status=true; 
       } 
       else 
       { 
        FragmentTwo f2 = new FragmentTwo(); 
        fragmentTransaction.add(R.id.fragment_container,f2); 

        fragmentTransaction.commit(); 
        bn.setText("Load first fragment"); 
        status=false; 
       } 

      } 
     }); 
    } 
} 
+0

本当にありがとう、本当にありがとうございました。 @babadaba –

0

これは間違ったFragmentManagerを使用しているために起こります。

import android.app.FragmentManager; 

だから、最初から自分のimport文を変更

import android.support.v4.app.FragmentManager; 

ともあなたはあなたのケースでgetSupportFragmentManager()を使用する必要があります。

ので変更:

FragmentManager fragmentManager = getFragmentManager(); 

へ:あなたのケースでMainActivityある

FragmentManager fragmentManager = getSupportFragmentManager(); 

また、あなたのActivity classは、AppCompatActivity代わりのActivityを拡張する必要があります。

+0

変更を加えた後でも、エラーが発生します。 - getSupportFragmentManagerを解決できません。 –

+0

'MainActivity'は' Activity'の代わりに 'AppCompatActivity'を拡張する必要があります。それも変更して、動作するかどうかを確認してください。 –

+0

私の答えを見て、あなたのアクティビティはAppCompatActivityを拡張する必要があります – babadaba

関連する問題