• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Fragment causes crash

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?

Java:
Here is the Navigation activity
@SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item)
    {
        Fragment fragment=null;
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.oil_changes)
        {
            fragment=new OilFragment();

        }
        else if (id == R.id.battery)
        {
            fragment=new BatteryFragment();

        }
        else if (id == R.id.brakes)
        {
            fragment=new BrakesFragment();
        }
        else if (id == R.id.muffler)
        {
            fragment=new MufflerFragment();

        }
        else if (id == R.id.plugs_wires)
        {
            fragment=new PlugsFragment();
        }
        else if (id == R.id.tires)
        {
            fragment=new TireFragment();
        }
        else if (id == R.id.custom)
        {
            fragment=new CustomFragment();
        }
        else if (id == R.id.eos)
        {
            fragment=new EOSFragment();
        }

        if(fragment != null)
        {

            FragmentManager fragmentManager=getSupportFragmentManager();
            FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.screen_area, fragment);
            fragmentTransaction.commit();
        }


        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

Java:
Here is the XML for the drawer.
<?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/oil_changes"
            android:icon="@drawable/oilicon"
            android:title="Oil Changes" />
        <item
            android:id="@+id/battery"
            android:icon="@drawable/batteryicon"
            android:title="Batteries" />

        <item android:id="@+id/brakes"
              android:icon="@drawable/brakesicon"
              android:title="Brakes" />

        <item android:id="@+id/tires"
            android:icon="@drawable/tiresicon"
            android:title="Tires" />
        <item
            android:id="@+id/plugs_wires"
            android:icon="@drawable/plugsicon"
            android:title="Spark Plugs/Wires" />
        <item
            android:id="@+id/muffler"
            android:icon="@drawable/mufflericon"
            android:title="Muffler/Exhaust" />
        <item
            android:id="@+id/custom"
            android:icon="@drawable/customicon"
            android:title="Custom" />
        <item
            android:id="@+id/eos"
            android:icon="@drawable/eosicon"
            android:title="End of Service" />
    </group>



</menu>

Brake Fragment XML Just changed the TextView
Java:
<?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"
    tools:context=".BrakesFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Hello Brake Fragment" />

</FrameLayout>

Finally, the Fragment Class which is unchanged.
Java:
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 BrakesFragment.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link BrakesFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class BrakesFragment 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 BrakesFragment() {
        // 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 BrakesFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static BrakesFragment newInstance(String param1, String param2) {
        BrakesFragment fragment = new BrakesFragment();
        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_brakes, 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);
    }
}

Thanks for your help, I know this is a long one. I'm sure the answer is much shorter.
 
It's difficult to say what is wrong in your code, not enough information.
I have added new branch "oil_fragment" with your code slightly changed into Navigation Drawer project.

Please find project demo here https://github.com/v777779/aad_20180429
Direct link to branch https://github.com/v777779/aad_20180429/tree/oil_fragment

Download, open, checkout oil_branch, run. Android Studio 3.1.
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.
 
Your code of BrakesFragment works fine. I used it in demo project actually without changes.
 
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.
 
Back
Top Bottom