错误的第二个参数类型。找到:'android.app.Fragment',必填:'android.support.v4.app.Fragment'

时间:2017-09-16 07:31:36

标签: android android-fragments types arguments required

我在Youtube上观看教程视频。我从一开始就做过两次同样的事情,但仍然坚持到那里。我将继续收到此错误,但他没有收到错误。

代码就在这里。在private void DisplayFragment类下面有错误。

    package com.example.kenpachi.navigation_drawer;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;


public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@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;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}


private void DisplayFragment(int id) {

    Fragment fragment = null;
    switch (id) {
        case R.id.nav_one:
            fragment = new FragmentOne();
            break;
        case R.id.nav_two:
            fragment = new FragmentTwo();
            break;
        case R.id.nav_three:
            fragment = new FragmentThree();
    }

    if (fragment != null) {

        android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

        ft.replace(R.id.MyFrameLayout, fragment); // "fragment" gives me this error.

        ft.commit();
    }
}


@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_camera) {
        // Handle the camera action
    } else if (id == R.id.nav_gallery) {

    } else if (id == R.id.nav_slideshow) {

    } else if (id == R.id.nav_manage) {

    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
}

这是给我错误的一行(在DisplayFragment中):

ft.replace(R.id.MyFrameLayout, fragment);

感谢。如果有人在这里有趣的是我试图做的视频指南:https://youtu.be/iZ8Y7W2jbv8?t=386

3 个答案:

答案 0 :(得分:1)

android有两种类型的片段

  1. android.app.Fragment
  2. android.support.v4.app.Fragment
  3. 所以你必须检查你的FragmentOne,FragmentTwo ......你输入的类型......

    对于使用import android.app.Fragment的FragmentOne内部的示例,您必须在活动中使用import android.app.Fragment

    在FragmentOne中你使用import android.support.v4.app.Fragment,然后你必须在你的活动中使用import android.support.v4.app.Fragment

    片段创建和片段调用两者必须属于同一类型

答案 1 :(得分:0)

替换

import android.app.Fragment;

import android.support.v4.app.Fragment;

答案 2 :(得分:0)

谢谢你们。当我切换时 "import android.app.Fragment;""android.support.v4.app.Fragment;"
对于MainActivity和FragmentOne-Two-Three问题已经消失。
 我之前只为MainActivity做过这样的事情,所以我会得到新的错误  再次感谢。