-2

我刚刚开始学习Android Studio的布局和基本 UI 元素,以及它如何组织文件drawable, layouts, strings, activities等。

因此,通过遵循一些 youtube 教程,我设法将一个基本navigation小部件添加到activity具有 5 个导航项的小部件中。唯一navigation item响应点击的是第一个是仪表板。其余的返回一个“不幸的是应用程序已停止”对话框。

这是我的代码Activity.java

import android.app.AlertDialog;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;

public class StudentActivity extends AppCompatActivity {
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {

                case R.id.navigation_dashboard: //this works
                    AlertDialog.Builder builder1 = new AlertDialog.Builder(StudentActivity.this);
                    builder1.setMessage("Dashboard To.");
                    builder1.setCancelable(true);
                    builder1.show(); // I'm able to see the dialog here

                    setTitle("Dashboard");
                    DashboardFragment dashboard_fragment = new DashboardFragment();
                    FragmentTransaction transaction1 = getFragmentManager().beginTransaction();
                    transaction1.replace(R.id.frameLayout,dashboard_fragment);
                    return true;

                case R.id.navigation_learn: //this doesn't work
                    try {
                        AlertDialog.Builder builder2 = new AlertDialog.Builder(StudentActivity.this);
                        builder2.setMessage("Learn To.");
                        builder2.setCancelable(true);
                        builder2.show(); //dialog doesn't even show up

                        setTitle("Learn");
                        DashboardFragment learn_fragment = new DashboardFragment();
                        FragmentTransaction manager1 = getFragmentManager().beginTransaction();
                        manager1.replace(R.id.frameLayout, learn_fragment);
                        manager1.commit();
                    }catch(Exception e){
                        AlertDialog.Builder errorDialog = new AlertDialog.Builder(StudentActivity.this);
                        errorDialog.setMessage(e.getMessage());
                        errorDialog.setCancelable(true);
                        errorDialog.show();
                    }

                    return true;
           }
            return false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_student);


        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }

}

下面是我的navigation.xml样子

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/navigation_dashboard"
        android:icon="@drawable/ic_dashboard"
        android:title="Dashboard" />

    <item
        android:id="@+id/navigation_learn"
        android:icon="@drawable/ic_openbook2"
        android:title="Learn" />

    <item
        android:id="@+id/navigation_play"
        android:icon="@drawable/ic_game2"
        android:title="Play" />

    <item
        android:id="@+id/navigation_review"
        android:icon="@drawable/ic_shapes"
        android:title="Review" />

    <item
        android:id="@+id/navigation_profile"
        android:icon="@drawable/ic_user2"
        android:title="My Profile" />

</menu>

我已验证所有drawable图标都存在且名称均为小写

我确保包含正确的导入import android.app.FragmentTransaction;。我做了一些研究,发现还有一个android.support.v4.app.FragmentTransaction不正确的。

构建成功,我可以使用Nox模拟器启动应用程序。Message Gradle Build窗口中没有消息。

在此处输入图像描述 在此处输入图像描述

可能是什么原因?关于如何解决这个问题的任何建议?

提前致谢。编辑:这是我点击第二个导航项时遇到的错误。

java.lang.IllegalArgumentException: No view found for id 0x7f0a004b (com.example.peace:id/frameLayout) for fragment LearnFragment{4bbf1c20 #1 id=0x7f0a004b}
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:882)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.BackStackRecord.run(BackStackRecord.java:684)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)

这是LearnFragment.java

package com.example.peace.cai;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 */
public class LearnFragment extends Fragment {


    public LearnFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_learn, container, false);
    }
}

在这里fragment_learn.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.peace.cai.LearnFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>
4

3 回答 3

0

在您的 Activity.java 类中,在第一种情况下 R.id.navigation_dashboard 您正在用dashboard_fragment 替换容器 R.id.frameLayout 中的任何内容,但您没有提交该事务。在第二种情况下 R.id.navigation_learn 您正在用另一个片段 LearnFragment 替换同一个容器 R.id.frameLayout 中的任何内容并执行提交,因此此时在同一个事务中您尝试将两个片段推送到相同的布局中这可能是导致崩溃的原因。通过像提交第二个事务一样提交第一个事务来解决此问题。请参阅下面的代码。

case R.id.navigation_dashboard: //this works
                    AlertDialog.Builder builder1 = new AlertDialog.Builder(StudentActivity.this);
                    builder1.setMessage("Dashboard To.");
                    builder1.setCancelable(true);
                    builder1.show(); // I'm able to see the dialog here

                    setTitle("Dashboard");
                    DashboardFragment dashboard_fragment = new DashboardFragment();
                    FragmentTransaction transaction1 = getFragmentManager().beginTransaction();
                  transaction1.replace(R.id.frameLayout,dashboard_fragment).commit();
                    return true;

                case R.id.navigation_learn: //this doesn't work
                    try {
                        AlertDialog.Builder builder2 = new AlertDialog.Builder(StudentActivity.this);
                        builder2.setMessage("Learn To.");
                        builder2.setCancelable(true);
                        builder2.show(); //dialog doesn't even show up

                        setTitle("Learn");
                        LearnFragment learn_fragment = new LearnFragment ();
                        FragmentTransaction manager1 = getFragmentManager().beginTransaction();
                        manager1.replace(R.id.frameLayout, learn_fragment);
                        manager1.commit();
                    }catch(Exception e){
                        AlertDialog.Builder errorDialog = new AlertDialog.Builder(StudentActivity.this);
                        errorDialog.setMessage(e.getMessage());
                        errorDialog.setCancelable(true);
                        errorDialog.show();
                    }

                    return true;
于 2018-02-07T13:02:49.940 回答
0

您对两个底部菜单按钮使用相同的片段。您正在从两个 bottomNav 按钮调用相同的片段,仅更改参考。

DashboardFragment learn_fragment = new DashboardFragment();在此处更改DashboardFragment()LearnFragment learn_fragment = new LearnFragment ();或您所拥有的。

以下是您的代码:

 case R.id.navigation_dashboard: //this works
                AlertDialog.Builder builder1 = new AlertDialog.Builder(StudentActivity.this);
                builder1.setMessage("Dashboard To.");
                builder1.setCancelable(true);
                builder1.show(); // I'm able to see the dialog here

                setTitle("Dashboard");
                DashboardFragment dashboard_fragment = new DashboardFragment();
                FragmentTransaction transaction1 = getFragmentManager().beginTransaction();
                transaction1.replace(R.id.frameLayout,dashboard_fragment);
                return true;

            case R.id.navigation_learn: //this doesn't work
                try {
                    AlertDialog.Builder builder2 = new AlertDialog.Builder(StudentActivity.this);
                    builder2.setMessage("Learn To.");
                    builder2.setCancelable(true);
                    builder2.show(); //dialog doesn't even show up

                    setTitle("Learn");
                    DashboardFragment learn_fragment = new DashboardFragment(); // Here need to change LearnFragment() 
                    FragmentTransaction manager1 = getFragmentManager().beginTransaction();
                    manager1.replace(R.id.frameLayout, learn_fragment);
                    manager1.commit();

将您的片段更改fragment_learn.xmlLinearLayoutRelativeLayout

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"   
    tools:context="com.example.peace.cai.LearnFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</LinearLayout >
于 2018-02-07T11:26:57.460 回答
0

在您的片段中添加以下行。

if(getActivity() != null && isAdded) {
  //do your operation
}
于 2020-09-06T06:34:24.587 回答