1. Download our Official Android App: Forums for Android!

Fragment causes crash

Discussion in 'Android Development' started by Ken Gordon, May 7, 2018.

  1. Ken Gordon

    Ken Gordon Newbie
    Thread Starter
    Rank:
     #730
    Points:
    16
    Posts:
    27
    Joined:
    Feb 12, 2017

    Feb 12, 2017
    27
    2
    16
    Male
    I have my navigation drawer all working except for one fragment that causes a crash. I have deleted it several times and added a new one several times, only for whatever reason it crashes when that fragment is selected.

    I am getting endless spinning (waiting for build to finish) on the navigation activity drawer.xml file even though I waited until it was done before looking at it. When I go to the text tab, not all the icons show up, yet they show up in the app. For some reason on that page, I get a null error. Not sure why.

    null
    java.lang.NullPointerException
    at com.android.tools.idea.uibuilder.scene.LayoutlibSceneManager.getRenderResult(LayoutlibSceneManager.java:602)
    at com.android.tools.idea.uibuilder.surface.ScreenViewBase.getResult(ScreenViewBase.java:109)
    at com.android.tools.idea.uibuilder.surface.ScreenView.getResult(ScreenView.java:30)
    at com.android.tools.idea.uibuilder.menu.NavigationViewSceneView.getPreferredSize(NavigationViewSceneView.java:64)
    at com.android.tools.idea.common.surface.SceneView.getPreferredSize(SceneView.java:95)
    at com.android.tools.idea.uibuilder.scene.LayoutlibSceneManager.createSceneViewsForMenu(LayoutlibSceneManager.java:265)
    at com.android.tools.idea.uibuilder.scene.LayoutlibSceneManager.doCreateSceneView(LayoutlibSceneManager.java:234)
    at com.android.tools.idea.common.scene.SceneManager.createSceneView(SceneManager.java:73)
    at com.android.tools.idea.common.scene.SceneManager.<init>(SceneManager.java:66)
    at com.android.tools.idea.uibuilder.scene.LayoutlibSceneManager.<init>(LayoutlibSceneManager.java:157)
    at com.android.tools.idea.uibuilder.surface.NlDesignSurface.createSceneManager(NlDesignSurface.java:145)
    at com.android.tools.idea.common.surface.DesignSurface.setModel(DesignSurface.java:281)
    at com.android.tools.idea.common.editor.NlEditorPanel.initNeleModelOnEventDispatchThread(NlEditorPanel.java:143)
    at com.android.tools.idea.common.editor.NlEditorPanel.lambda$null$4(NlEditorPanel.java:139)
    at com.intellij.openapi.project.DumbServiceImpl.lambda$smartInvokeLater$7(DumbServiceImpl.java:366)
    at com.intellij.openapi.application.TransactionGuardImpl$2.run(TransactionGuardImpl.java:315)
    at com.intellij.openapi.application.impl.LaterInvocator$FlushQueue.runNextEvent(LaterInvocator.java:424)
    at com.intellij.openapi.application.impl.LaterInvocator$FlushQueue.run(LaterInvocator.java:407)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:762)
    at java.awt.EventQueue.access$500(EventQueue.java:98)
    at java.awt.EventQueue$3.run(EventQueue.java:715)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:732)
    at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:822)
    at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:650)
    at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:366)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

    Where do I look to fix the problem? I thought maybe some corruption was showing up somewhere, but when I deleted another fragment that was a problem and recreated it, it worked fine.
    Are there only a certain number of items you can have in a drawer?

    Code (Java):
    1.  
    2. Here is the Navigation activity
    3. @SuppressWarnings("StatementWithEmptyBody")
    4.     @Override
    5.     public boolean onNavigationItemSelected(MenuItem item)
    6.     {
    7.         Fragment fragment=null;
    8.         // Handle navigation view item clicks here.
    9.         int id = item.getItemId();
    10.  
    11.         if (id == R.id.oil_changes)
    12.         {
    13.             fragment=new OilFragment();
    14.  
    15.         }
    16.         else if (id == R.id.battery)
    17.         {
    18.             fragment=new BatteryFragment();
    19.  
    20.         }
    21.         else if (id == R.id.brakes)
    22.         {
    23.             fragment=new BrakesFragment();
    24.         }
    25.         else if (id == R.id.muffler)
    26.         {
    27.             fragment=new MufflerFragment();
    28.  
    29.         }
    30.         else if (id == R.id.plugs_wires)
    31.         {
    32.             fragment=new PlugsFragment();
    33.         }
    34.         else if (id == R.id.tires)
    35.         {
    36.             fragment=new TireFragment();
    37.         }
    38.         else if (id == R.id.custom)
    39.         {
    40.             fragment=new CustomFragment();
    41.         }
    42.         else if (id == R.id.eos)
    43.         {
    44.             fragment=new EOSFragment();
    45.         }
    46.  
    47.         if(fragment != null)
    48.         {
    49.  
    50.             FragmentManager fragmentManager=getSupportFragmentManager();
    51.             FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
    52.             fragmentTransaction.replace(R.id.screen_area, fragment);
    53.             fragmentTransaction.commit();
    54.         }
    55.  
    56.  
    57.         DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    58.         drawer.closeDrawer(GravityCompat.START);
    59.         return true;
    60.     }
    Code (Java):
    1.  
    2. Here is the XML for the drawer.
    3. <?xml version="1.0" encoding="utf-8"?>
    4. <menu
    5.     xmlns:android="http://schemas.android.com/apk/res/android"
    6.     xmlns:tools="http://schemas.android.com/tools"
    7.     tools:showIn="navigation_view">
    8.  
    9.     <group android:checkableBehavior="single">
    10.         <item
    11.             android:id="@+id/oil_changes"
    12.             android:icon="@drawable/oilicon"
    13.             android:title="Oil Changes" />
    14.         <item
    15.             android:id="@+id/battery"
    16.             android:icon="@drawable/batteryicon"
    17.             android:title="Batteries" />
    18.  
    19.         <item android:id="@+id/brakes"
    20.               android:icon="@drawable/brakesicon"
    21.               android:title="Brakes" />
    22.  
    23.         <item android:id="@+id/tires"
    24.             android:icon="@drawable/tiresicon"
    25.             android:title="Tires" />
    26.         <item
    27.             android:id="@+id/plugs_wires"
    28.             android:icon="@drawable/plugsicon"
    29.             android:title="Spark Plugs/Wires" />
    30.         <item
    31.             android:id="@+id/muffler"
    32.             android:icon="@drawable/mufflericon"
    33.             android:title="Muffler/Exhaust" />
    34.         <item
    35.             android:id="@+id/custom"
    36.             android:icon="@drawable/customicon"
    37.             android:title="Custom" />
    38.         <item
    39.             android:id="@+id/eos"
    40.             android:icon="@drawable/eosicon"
    41.             android:title="End of Service" />
    42.     </group>
    43.  
    44.  
    45.  
    46. </menu>
    47.  
    Brake Fragment XML Just changed the TextView
    Code (Java):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3.     xmlns:tools="http://schemas.android.com/tools"
    4.     android:layout_width="match_parent"
    5.     android:layout_height="match_parent"
    6.     tools:context=".BrakesFragment">
    7.  
    8.     <!-- TODO: Update blank fragment layout -->
    9.     <TextView
    10.         android:layout_width="match_parent"
    11.         android:layout_height="match_parent"
    12.         android:text="Hello Brake Fragment" />
    13.  
    14. </FrameLayout>
    Finally, the Fragment Class which is unchanged.
    Code (Java):
    1. import android.content.Context;
    2. import android.net.Uri;
    3. import android.os.Bundle;
    4. import android.support.v4.app.Fragment;
    5. import android.view.LayoutInflater;
    6. import android.view.View;
    7. import android.view.ViewGroup;
    8.  
    9.  
    10. /**
    11. * A simple {@link Fragment} subclass.
    12. * Activities that contain this fragment must implement the
    13. * {@link BrakesFragment.OnFragmentInteractionListener} interface
    14. * to handle interaction events.
    15. * Use the {@link BrakesFragment#newInstance} factory method to
    16. * create an instance of this fragment.
    17. */
    18. public class BrakesFragment extends Fragment {
    19.     // TODO: Rename parameter arguments, choose names that match
    20.     // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    21.     private static final String ARG_PARAM1 = "param1";
    22.     private static final String ARG_PARAM2 = "param2";
    23.  
    24.     // TODO: Rename and change types of parameters
    25.     private String mParam1;
    26.     private String mParam2;
    27.  
    28.     private OnFragmentInteractionListener mListener;
    29.  
    30.     public BrakesFragment() {
    31.         // Required empty public constructor
    32.     }
    33.  
    34.     /**
    35.      * Use this factory method to create a new instance of
    36.      * this fragment using the provided parameters.
    37.      *
    38.      * @param param1 Parameter 1.
    39.      * @param param2 Parameter 2.
    40.      * @return A new instance of fragment BrakesFragment.
    41.      */
    42.     // TODO: Rename and change types and number of parameters
    43.     public static BrakesFragment newInstance(String param1, String param2) {
    44.         BrakesFragment fragment = new BrakesFragment();
    45.         Bundle args = new Bundle();
    46.         args.putString(ARG_PARAM1, param1);
    47.         args.putString(ARG_PARAM2, param2);
    48.         fragment.setArguments(args);
    49.         return fragment;
    50.     }
    51.  
    52.     @Override
    53.     public void onCreate(Bundle savedInstanceState) {
    54.         super.onCreate(savedInstanceState);
    55.         if (getArguments() != null) {
    56.             mParam1 = getArguments().getString(ARG_PARAM1);
    57.             mParam2 = getArguments().getString(ARG_PARAM2);
    58.         }
    59.     }
    60.  
    61.     @Override
    62.     public View onCreateView(LayoutInflater inflater, ViewGroup container,
    63.                              Bundle savedInstanceState) {
    64.         // Inflate the layout for this fragment
    65.         return inflater.inflate(R.layout.fragment_brakes, container, false);
    66.     }
    67.  
    68.     // TODO: Rename method, update argument and hook method into UI event
    69.     public void onButtonPressed(Uri uri) {
    70.         if (mListener != null) {
    71.             mListener.onFragmentInteraction(uri);
    72.         }
    73.     }
    74.  
    75.     @Override
    76.     public void onAttach(Context context) {
    77.         super.onAttach(context);
    78.         if (context instanceof OnFragmentInteractionListener) {
    79.             mListener = (OnFragmentInteractionListener) context;
    80.         } else {
    81.             throw new RuntimeException(context.toString()
    82.                     + " must implement OnFragmentInteractionListener");
    83.         }
    84.     }
    85.  
    86.     @Override
    87.     public void onDetach() {
    88.         super.onDetach();
    89.         mListener = null;
    90.     }
    91.  
    92.     /**
    93.      * This interface must be implemented by activities that contain this
    94.      * fragment to allow an interaction in this fragment to be communicated
    95.      * to the activity and potentially other fragments contained in that
    96.      * activity.
    97.      * <p>
    98.      * See the Android Training lesson <a href=
    99.      * "http://developer.android.com/training/basics/fragments/communicating.html"
    100.      * >Communicating with Other Fragments</a> for more information.
    101.      */
    102.     public interface OnFragmentInteractionListener {
    103.         // TODO: Update argument type and name
    104.         void onFragmentInteraction(Uri uri);
    105.     }
    106. }
    107.  
    Thanks for your help, I know this is a long one. I'm sure the answer is much shorter.
     

    Advertisement

  2. v777779

    v777779 Well-Known Member
    Rank:
    None
    Points:
    68
    Posts:
    126
    Joined:
    Mar 24, 2018

    Mar 24, 2018
    126
    20
    68
  3. Ken Gordon

    Ken Gordon Newbie
    Thread Starter
    Rank:
     #730
    Points:
    16
    Posts:
    27
    Joined:
    Feb 12, 2017

    Feb 12, 2017
    27
    2
    16
    Male
    Thanks!
    It's the brake fragment that is the problem. The oil fragment works fine. I am only working with 2 gigs of memory, left over from when I had XP. Could it be a memory issue that's causing the problem? I'm thinking about just deleting the physical class file and replacing it with a demo that I modified to make sure it worked. I will d/l the branch and take a look.
     
  4. v777779

    v777779 Well-Known Member
    Rank:
    None
    Points:
    68
    Posts:
    126
    Joined:
    Mar 24, 2018

    Mar 24, 2018
    126
    20
    68
    Your code of BrakesFragment works fine. I used it in demo project actually without changes.
     
  5. Ken Gordon

    Ken Gordon Newbie
    Thread Starter
    Rank:
     #730
    Points:
    16
    Posts:
    27
    Joined:
    Feb 12, 2017

    Feb 12, 2017
    27
    2
    16
    Male
    I wonder why it crashes on my tablet... It's an RCA Voyager. The other fragments work fine, just that one...
     
    #5 Ken Gordon, May 16, 2018
    Last edited: May 16, 2018
  6. Ken Gordon

    Ken Gordon Newbie
    Thread Starter
    Rank:
     #730
    Points:
    16
    Posts:
    27
    Joined:
    Feb 12, 2017

    Feb 12, 2017
    27
    2
    16
    Male
    UPDATE:
    After not having time to work on it due to other projects, I opened things up today, set up the layouts for both battery and brakes, made the APK, just to check things out. Both the brake fragment AND the battery fragment caused the app to stop. Not a crash, but a stop. Very strange.
     

Share This Page

Loading...