2017-05-28 41 views
0

即時通訊問題。我收到此錯誤消息無法使用導航抽屜活動解決方法'替換(int)'片段

Cannot resolve Method 'replace (int) 

我正在看一個教程,但該傢伙不知道該錯誤消息。我也試圖通過閱讀其他帖子來解決這個問題,但它讓我更加困惑。

我的代碼看起來是這樣的:

MainActivity.java 

package androfenix.mycvapp; 

import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v4.app.FragmentManager; 
import android.view.View; 
import android.support.design.widget.NavigationView; 
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; 

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.addDrawerListener(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); 
    } 

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

     if (id == R.id.nav_aboutme) { 
      FragmentAboutme fragmentAboutme = new FragmentAboutme(); 
      FragmentManager manager = getSupportFragmentManager(); 
      manager.beginTransaction().replace(R.id.relativelayout_for_fragment).commit(fragmentAboutme); 

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

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

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

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

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

     } 

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

而我想更換佈局片段是這一個:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 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:id="@+id/relativelayout_for_fragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="androfenix.mycvapp.MainActivity" 
    tools:showIn="@layout/app_bar_main"> 

</android.support.constraint.ConstraintLayout> 

回答

0

這裏FragmentTransaction.Commit()不需要Arguement

所以不是作者:

manager.beginTransaction().replace(R.id.relativelayout_for_fragment).commit(fragmentAboutme); 

使用:

manager.beginTransaction().replace(R.id.relativelayout_for_fragment, fragmentAboutme).commit(); 

另外,還要確保你的FragmentAboutme是擴展支持庫

import android.support.v4.app.Fragment; 
0

請仔細閱讀有關片段

replace(R.id.relativelayout_for_fragment,"your about me fragment").commit(); 

,爲什麼你會甚至通過在提交片段對象()方法沒有這樣的方法;

+0

Omg im so dumb!謝謝..我把它寫入錯誤的支架..「fragmentAboutme」應該進入替換部分 –

+0

是的,所以在這裏問任何問題之前,請仔細研究任何主題請參閱文檔https://stackoverflow.com/documentation/android/話題和這裏https://developer.android.com/index.html –