我该如何解决:多个片段和Activity_main之间的事务?

时间:2017-01-03 18:29:46

标签: java android android-fragments navigation-drawer

当我打开导航抽屉并滚动浏览不同的选项时(我们称之为A,B和C)。当我点击A,B或C时,Fragment布局无法打开,我卡在我的Activity_main版面上。

Main Activity Image

Nav drawer image

之前我将content_mainActivity_main作为两个单独的XML文件,我被告知我必须将content_main代码放入Activity_main(其中我做了,这删除了no view found for id错误。

这是我的Activity_main(现在):

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/e"
        android:id="@+id/imageView7"
        android:layout_marginTop="96dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:contentDescription=""/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/welcome"
        android:id="@+id/imageView2"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>
</LinearLayout>

非常感谢帮助! 如果还有其他我可以提供的,请告诉我!

这是java代码:

package mcshreker.pocketmath;

import android.app.FragmentTransaction;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.FragmentManager;
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.text.Layout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

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

    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();


    return super.onOptionsItemSelected(item);
}

@RequiresApi(api = Build.VERSION_CODES.M)
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    FragmentManager fragmentManager = getSupportFragmentManager();

    if (id == R.id.nav_first_layout) {
        fragmentManager.beginTransaction().replace
                (R.id.drawer_layout, new firstFragment()).commit();

        // Handle the camera action

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

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

    } else if (id == R.id.nav_second_layout) {
        fragmentManager.beginTransaction().replace
                (R.id.drawer_layout, new secondFragment()).commit();
    } else if (id == R.id.nav_third_layout) {
        fragmentManager.beginTransaction().replace
                (R.id.drawer_layout, new thirdFragment()).commit();
    } else if (id == R.id.nav_fourth_layout) {
        fragmentManager.beginTransaction().replace
                (R.id.drawer_layout, new fourthFragment()).commit();
    } else if (id == R.id.nav_fifth_layout) {
        fragmentManager.beginTransaction().replace
                (R.id.drawer_layout, new fifthFragment()).commit();
    } else if (id == R.id.nav_sixth_layout) {
        fragmentManager.beginTransaction().replace
                (R.id.drawer_layout, new sixthFragment()).commit();
    } else if (id == R.id.nav_seventh_layout) {
        fragmentManager.beginTransaction().replace
                (R.id.drawer_layout, new seventhFragment()).commit();
    } else if (id == R.id.nav_eighth_layout) {
        fragmentManager.beginTransaction().replace
                (R.id.drawer_layout, new eighthFragment()).commit();
    } else if (id == R.id.nav_ninth_layout) {
        fragmentManager.beginTransaction().replace
                (R.id.drawer_layout, new ninthFragment()).commit();
    }

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

1 个答案:

答案 0 :(得分:0)

我认为你不应该替换主要的活动布局。 如果您的主活动布局中的视图(如框架布局)与其父宽度和高度匹配,则会更好。