簡體   English   中英

我該如何解決:多個片段和Activity_main之間的事務?

[英]How do I fix: Transaction between multiple fragments and the Activity_main?

當我打開導航抽屜並滾動瀏覽不同的選項時(以下分別稱為A,B和C)。 當我單擊A,B或C時, Fragment布局不會打開,並且卡在Activity_main布局中。

主要活動圖片

導航抽屜圖像

以前,我將content_mainActivity_main作為兩個單獨的XML文件,並被告知必須將content_main代碼放入Activity_main (這樣做了),這刪除了no view found for id錯誤的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;
}
}

我不認為您應該替換主要活動布局。 如果您在主活動布局中具有與其父代寬度和高度相匹配的視圖(如框架布局),則效果會更好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM