2017-05-28 7 views
0

」のメソッド「replace(int)」をナビゲーション・ドロワー・アクティビティーを使用して解決できず、1つの問題が発生しました。このエラーメッセージが表示されますフラグメント「

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

と私は交換したいレイアウトフラグメントはこの1つである:

<?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

を必要としません。 of:

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 –

関連する問題