错误:The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, MyFragment)

Fragment newfragment =new MyFragment();
fragmentTransaction.replace(R.layout.activity_main,newfragment ).commit();

提示错误:The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, MyFragment)

妈蛋,找了好久!一直以为子类对象不能赋值给父类引用。这部科学啊!

错误代码:

package com.example.testforfragment;

import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v4.app.Fragment;
import android.support.v4.app.Fragment.*;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction  fragmentTransaction = fragmentManager.beginTransaction();
        Fragment newfragment =new MyFragment();
        fragmentTransaction.replace(R.layout.activity_main,newfragment ).commit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

修改:

package com.example.testforfragment;

import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction  fragmentTransaction = fragmentManager.beginTransaction();
        Fragment newfragment =new MyFragment();
        fragmentTransaction.replace(R.layout.activity_main,newfragment ).commit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

1.导入的包

2.extends

3.getSurpport……

错误:The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, MyFragment)

时间: 2024-11-23 20:46:24

错误:The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, MyFragment)的相关文章

The method load(Class, Serializable) in the type Session is not applicable for the arguments (Class<

Transaction transaction = session.beginTransaction(); //load是通过主键属性,获取对象的实例 Employee employee  =(Employee) session.load(Employee.class, 1); employee.setName("demo"); transaction.commit(); session.close(); 报错The method load(Class, Serializable) i

The method load(Class, Serializable) in the type HibernateTemplate is not applicable for the arguments (Class, int)

引入别人的项目发现利用HibernateTemplate的load的方法报错了.错误提示为: 意思为load方法的第二个参数是实现Serializable接口的对象,int类型不符合.但jdk自动装箱,int会自转换为Integer,而Integer是实现了Serializable的,所以应该是可以的.但myeclipse偏偏报错了,原因是我的myeclipse中window->preferences->java-compiler中的Compiler compliance level设置为1.

The method sort(Comparable[]) in the type Shell is not applicable for the arguments (int[])

看算法第四版的希尔排序时,打算把例中的String数组换成int数组,却出现了上面的问题.最后在StackOverflow找到答案原代码: package sort; import edu.princeton.cs.algs4.In; public class Shell{ public static void sort(Comparable[] a) { //将a[]升序排列 int N = a.length; int h = 1; while (h<N/3) h = 3*h + 1; //

The method replace(int, Fragment, String) in the type FragmentTransaction is not applicable for the

 The method replace(int, Fragment, String) in the type FragmentTransaction is not applicable for the arguments (int, SettingFragment, String) 今天遇到这样一个很奇葩的错误信息,后来查到我导入的包有问题  import android.app.Fragment; import android.app.FragmentManager;   其实我应该使用的

The method setItems(String) in the type ForTokensTag is not applicable for the arguments (Object)

1. 问题 看到这个错误以为是貌似jsp页面有误,c:forTokens标签用错了?? An error occurred at line: 444 in the jsp file: /WEB-INF/pages/countOrder/viewCountOrderDetails.jsp The method setItems(String) in the type ForTokensTag is not applicable for the arguments (Object) 441: </t

The method sendKeys(CharSequence[]) in the type WebElement is not applicable for the arguments (String)

写了一段测试代码运行时一直报错:The method sendKeys(CharSequence[]) in the type WebElement is not applicable for the arguments (String) 测试代码: driver.findElement(By.name("wd")).sendKeys("selnium"); 原因:旧版本的Java不理解非随机变量参数 解决方法:在工程上点击右键选择Build Path -> 

提示错误:The method replace in the type.. is not applicable for the argument......

在最近新学fragment的时候出现了一个错误: The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, MyFragment) 新建fragment的时候 Fragment newfragment =new MyFragment();fragmentTransaction.replace(R.layout.activity_main,n

ruby on rails错误undefined method `title&#39; for nil:NilClass

首先搞清楚这句话,在 Ruby 中,方法分为 public.private 和 protected 三种,只有 public 方法才能作为控制器的动作. 我的出错的代码如下: controlle class ArticlesController < ApplicationController def new end def create params.permit! @article = Article.new(params[:article]) @article.save redirect_t

ruby on rails错误undefined method `title&amp;#39; for nil:NilClass

首先搞清楚这句话,在 Ruby 中,方法分为 public.private 和 protected 三种,仅仅有 public 方法才干作为控制器的动作. 我的出错的代码例如以下: controlle class ArticlesController < ApplicationController def new end def create params.permit! @article = Article.new(params[:article]) @article.save redirec