2017-02-23 72 views
1

我是一名android工作室的begginer。我只是試圖從導航抽屜菜單中選擇一個項目時打開一個片段,但應用程序不斷崩潰。取代導航抽屜中的一個片段不斷崩潰應用

這是我的導覽活動:

package georgia.languagelandscape2; 
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.app.FragmentManager; 
import android.support.v4.app.FragmentTransaction; 
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; 

import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.OnMapReadyCallback; 
import com.google.android.gms.maps.SupportMapFragment; 

public class NavigationDrawerActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener, OnMapReadyCallback { 

    SupportMapFragment sMapFragment; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     sMapFragment=SupportMapFragment.newInstance(); 


     setContentView(R.layout.activity_navigation_drawer); 
     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); 

     FragmentManager fm=getSupportFragmentManager(); 
     fm.beginTransaction().replace(R.id.content_frame, new MainFragment()).commit(); 

     sMapFragment.getMapAsync(this); 

     android.support.v4.app.FragmentManager sFm=getSupportFragmentManager(); 
     fm.beginTransaction().replace(R.id.content_frame, new ImportFragment()).commit(); 
     sFm.beginTransaction().add(R.id.map, sMapFragment).commit(); 
    } 

    @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.navigation_drawer, 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. 

     android.support.v4.app.FragmentManager sFm=getSupportFragmentManager(); 
     FragmentManager fm=getSupportFragmentManager(); 

     int id = item.getItemId(); 

     if(sMapFragment.isAdded()) 
      sFm.beginTransaction().hide(sMapFragment).commit(); 

     if (id == R.id.nav_camera) { 
      // Handle the camera action 
      if(!sMapFragment.isAdded()) 
       sFm.beginTransaction().add(R.id.map, sMapFragment).commit(); 
      else 
       sFm.beginTransaction().show(sMapFragment).commit(); 
     } else if (id == R.id.nav_gallery) { 

      // ProfileFragment proFrag= new ProfileFragment(); 
      // sFm.beginTransaction().show(proFrag).commit() 

      FragmentManager fragmentManager = getSupportFragmentManager(); 
      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 

      ProfileFragment myFragment = new ProfileFragment(); 
      fragmentTransaction.replace(R.id.map, myFragment); 
      fragmentTransaction.commit(); 

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

    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     //markers, locations, everything, here 
    } 
} 

和我的抽屜式導航欄內容的XML文件 `

<FrameLayout 
    android:id="@+id/content_frame" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"></FrameLayout> 

<FrameLayout 
    android:id="@+id/map" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"></FrameLayout> 

`

Pleeeease幫助我,我很絕望

崩潰日誌:

FATAL EXCEPTION: main 
                     Process: georgia.a50000incercare, PID: 4165 
                     java.lang.RuntimeException: [email protected] must implement OnFragmentInteractionListener 
                      at georgia.a50000incercare.ProfileFragment.onAttach(ProfileFragment.java:83) 
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1230) 
                      at android.support.v4.app.FragmentManagerImpl.moveFragmentsToInvisible(FragmentManager.java:2323) 
                      at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2136) 
                      at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2092) 
                      at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1998) 
                      at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:709) 
                      at android.os.Handler.handleCallback(Handler.java:751) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6077) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
+1

後的崩潰日誌。 – rafsanahmad007

+0

嘗試下面的答案,它會工作... – rafsanahmad007

回答

0

由於錯誤建議你應該在你的活動實施OnFragmentInteractionListener

public class NavigationDrawerActivity extends AppCompactActivity implements 
      NavigationView.OnNavigationItemSelectedListener,OnMapReadyCallback 
     ,ProfileFragment.OnFragmentInteractionListener{ //this need to be implemented 

而且覆蓋的方法在你的活動

@Override 
    public void onFragmentInteraction(int data){ //method that need to be implemented 
    //your implementation 
    } 

this