Activity(有时应用程序)在选择导航项时关闭

Activity (sometimes app) closes when Navigation item is selected

我有一个 android 应用程序,其中包含 MainActivityMain2ActivityNewVisitFragmentMain2Activity(我有导航视图)在单击 MainActivity 按钮后打开。

出于某些原因,当我 select 导航项 {'New visit'}(更准确地说 - 当我尝试打开新片段时)Main2Activity 关闭。有时整个应用程序都会关闭。

我试图在 android studio 中找到调试器的问题,"walked" 在以下 类:

Main2Activity --> NavigationView.java --> MenuItemImpl.java --> MenuBuilder.java --> NavigationMenuPresenter.java --> View.java --> handler.java -- looper.java

并在 looper.java 中完成且没有错误代码,在 loop 方法中:

 finally {
      if (traceTag != 0) {
          Trace.traceEnd(traceTag); //finished here
      }
 }

我已经从 templete 创建了 Main2Activity,所以我除了以下内容外没有做任何更改:

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {

    displayView(item.getItemId());
    return true;
}

public void displayView(int viewID){

    Fragment fragment = null;
    String title = getString(R.string.app_name);

    switch(viewID){
        case R.id.nav_new_visit:
            fragment = new NewVisitFragment();
            title = "New Visit";
            break;
        case R.id.nav_visits:
            title = "Visits";
            break;
    }

    if(fragment != null){
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.content_frame,fragment);
        ft.commit();
    }

    if(getSupportActionBar() != null){
        getSupportActionBar().setTitle(title);
    }

    DrawerLayout drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
    drawerLayout.closeDrawer(GravityCompat.START);
}

这是 activity_main2_drawer.xml 文件:

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

<group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_new_visit"
        android:icon="@drawable/ic_menu_camera"
        android:title="New Visit" />
    <item
        android:id="@+id/nav_visits"
        android:icon="@drawable/ic_menu_gallery"
        android:title="Visits" />
    <item
        android:id="@+id/nav_slideshow"
        android:icon="@drawable/ic_menu_slideshow"
        android:title="A" />
    <item
        android:id="@+id/nav_manage"
        android:icon="@drawable/ic_menu_manage"
        android:title="options" />
</group>

<item android:title="Other">
    <menu>
        <item
            android:id="@+id/nav_share"
            android:icon="@drawable/ic_menu_share"
            android:title="something 1" />
        <item
            android:id="@+id/nav_send"
            android:icon="@drawable/ic_menu_send"
            android:title="something 2" />
    </menu>
</item>

这是 activity_main2.xml 文件:

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

<android.support.v7.widget.ContentFrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/content_frame">

</android.support.v7.widget.ContentFrameLayout>

<include
    layout="@layout/app_bar_main2"
    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_main2"
    app:menu="@menu/activity_main2_drawer" />


</android.support.v4.widget.DrawerLayout>

NewVisitFragment 中的代码也会自动生成:

    package com.example.ddd;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link NewVisitFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link NewVisitFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class NewVisitFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

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

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment NewVisitFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static NewVisitFragment newInstance(String param1, String param2) {
        NewVisitFragment fragment = new NewVisitFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

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

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}

fragment_new_visit.xml 看起来像这样:

 <?xml version="1.0" encoding="utf-8"?>
<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"
    android:background="@color/colorPrimaryDark"
    tools:context=".NewVisitFragment">

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

</FrameLayout>

这是Logcat:

09-24 11:07:44.160 13926-13926/? I/com.example.dd: Not late-enabling -Xcheck:jni (already on)
09-24 11:07:44.221 13926-13926/? W/com.example.dd: Unexpected CPU variant for X86 using defaults: x86
09-24 11:07:44.243 13926-13937/? E/com.example.dd: Failed to send jdwp-handshake response.: Broken pipe
09-24 11:07:44.284 13926-13926/? I/com.example.dd: The ClassLoaderContext is a special shared library.
09-24 11:07:44.378 13926-13926/? W/com.example.dd: JIT profile information will not be recorded: profile file does not exits.
09-24 11:07:44.381 13926-13926/? I/chatty: uid=10086(com.example.ddd) identical 10 lines
09-24 11:07:44.381 13926-13926/? W/com.example.dd: JIT profile information will not be recorded: profile file does not exits.
09-24 11:07:44.394 13926-13926/? I/InstantRun: starting instant run server: is main process
09-24 11:07:44.528 13926-13926/? W/com.example.dd: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
    Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
09-24 11:07:44.632 13926-13926/? D/OpenGLRenderer: Skia GL Pipeline
09-24 11:07:44.684 13926-13951/? I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
09-24 11:07:44.685 13926-13951/? I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
09-24 11:07:44.685 13926-13951/? I/OpenGLRenderer: Initialized EGL, version 1.4
09-24 11:07:44.685 13926-13951/? D/OpenGLRenderer: Swap behavior 1
09-24 11:07:44.686 13926-13951/? W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
09-24 11:07:44.686 13926-13951/? D/OpenGLRenderer: Swap behavior 0
09-24 11:07:44.695 13926-13951/? D/EGL_emulation: eglCreateContext: 0xe0085360: maj 3 min 1 rcv 4
09-24 11:07:44.765 13926-13951/? D/EGL_emulation: eglMakeCurrent: 0xe0085360: ver 3 1 (tinfo 0xe0083680)
09-24 11:07:44.767 13926-13951/? E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
    glUtilsParamSize: unknow param 0x000082da
09-24 11:07:44.841 13926-13951/? D/EGL_emulation: eglMakeCurrent: 0xe0085360: ver 3 1 (tinfo 0xe0083680)
09-24 11:07:44.973 13926-13926/? I/AssistStructure: Flattened final assist data: 3360 bytes, containing 1 windows, 11 views

如果您的上下文未实现特定接口,您将在 NewVisitFragmentonAttach() 方法中抛出 RuntimeException。但是您的 activity 没有实现 OnFragmentInteractionListener 接口。这就是为什么当您打开片段时,您的应用程序崩溃了。