-1

I have an android app which contains MainActivity, Main2Activity and NewVisitFragment. Main2Activity (where I have navigation view) opens after button is clicked on MainActivity.

For some reasons, when I select navigation item {'New visit'} (more precisely - when I try to open new Fragment) Main2Activity closes. Sometimes the whole app closes.

I tried to find problem with debugger in android studio, which "walked" in the following classes:

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

and finished with no error code in looper.java, in loop method:

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

I have created Main2Activity from templete, so I have changed nothing except the following:

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

This is activity_main2_drawer.xml file:

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

This is activity_main2.xml file:

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

Code in NewVisitFragment is also automatically Generated:

    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 looks like this:

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

This is 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

1 Answers1

1

You will throw RuntimeException in onAttach() method of NewVisitFragment if your context not implement specific interface. But your activity doesn't implement OnFragmentInteractionListener interface. That's why when you open fragment, your app is crashed.

ConstOrVar
  • 2,025
  • 1
  • 11
  • 14