Navigation drawer app has stopped with fragments and JSON parsing

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 582 Views - Last Post: 05 August 2017 - 11:58 AM

#1 suhailpappu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 02-July 17

Navigation drawer app has stopped with fragments and JSON parsing

Posted 05 August 2017 - 10:55 AM

Hey guys i am stuck with my app which involves a navigation drawer and items, when clicked one of the item it opens recycler view with json parsing but my app always shutting down whats the problem?My app doesnt open at all and if u guys require any xml file i am ready to provide it please help me with this situation and thank you...

My Main Activity.java

package com.pappu5.navigation;

import android.app.DownloadManager;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;



public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);



        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        //add this line to display menu1 when the activity is loaded
        displaySelectedScreen(R.id.nav_gallery);
    }



    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.

        displaySelectedScreen(item.getItemId());

        return true;
    }

    private void displaySelectedScreen(int itemId) {

        //creating fragment object
        Fragment fragment = null;

        //initializing the fragment object which is selected
        switch (itemId) {
            case R.id.nav_gallery:
                fragment = new GalleryFrag();
                break;
            /*case R.id.nav_menu2:
                fragment = new Menu2();
                break;
            case R.id.nav_menu3:
                fragment = new Menu3();
                break;*/
        }

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

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



My Gallery Fragment

package com.pappu5.navigation;

import android.app.ProgressDialog;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.annotation.Nullable;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link GalleryFrag.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link GalleryFrag#newInstance} factory method to
 * create an instance of this fragment.
 */
public class GalleryFrag 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";

    private RecyclerView rv;
    private RecyclerView.Adapter adapter;
    private List<ListItem> lt;
    private String url = "https://newsapi.org/v1/articles?source=techcrunch&sortBy=top&apiKey=8b66f69591fc49229753135fe44488a5";
    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    public GalleryFrag() {
        // 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 GalleryFrag.
     */
    // TODO: Rename and change types and number of parameters
    public static GalleryFrag newInstance(String param1, String param2) {
        GalleryFrag fragment = new GalleryFrag();
        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) {
        rv = (RecyclerView)getView().findViewById(R.id.recyclerView);
        rv.setHasFixedSize(true);
        rv.setLayoutManager(new LinearLayoutManager(getActivity()));
        lt =  new ArrayList<>();
        loadRecyclerData();
        View view =  inflater.inflate(R.layout.fragment_gallery, container, false);
        return view;
    }

    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //you can set the title for your toolbar here for different fragments different titles
        getActivity().setTitle("Menu 1");
    }

    private void loadRecyclerData() {
        final ProgressDialog pd = new ProgressDialog(getActivity());
        pd.setMessage("Fetching the data..");
        pd.show();

        StringRequest sr = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        pd.dismiss();
                        try {
                            JSONObject json = new JSONObject(response);
                            JSONArray jarray = new JSONArray("articles");
                            for (int i =0 ; i<jarray.length() ; i++){
                                JSONObject jo = jarray.getJSONObject(i);
                                ListItem lm = new ListItem(
                                        jo.getString("author"),
                                        jo.getString("title"),
                                        jo.getString("description"),
                                        jo.getString("url"),
                                        jo.getString("urlToImage"),
                                        jo.getString("publishedAt")
                                );
                                lt.add(lm);
                            }
                            adapter = new MyAdapter(lt,getActivity().getApplicationContext());
                            rv.setAdapter(adapter);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }
                , new Response.ErrorListener() {
            @Override
            public void onerrorResponse(VolleyError error) {

            }
        });
        RequestQueue rq = Volley.newRequestQueue(getActivity().getApplicationContext());
        rq.add(sr);
    }

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



Is This A Good Question/Topic? 0
  • +

Replies To: Navigation drawer app has stopped with fragments and JSON parsing

#2 ndc85430  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 565
  • Posts: 2,322
  • Joined: 13-June 14

Re: Navigation drawer app has stopped with fragments and JSON parsing

Posted 05 August 2017 - 11:00 AM

There's a lot of code there and I think you'd be hard pressed to find someone who'll debug it for you. What have you done to debug the problem? If the app is crashing, is an exception being thrown? If so, what does the stack trace tell you?
Was This Post Helpful? 0
  • +
  • -

#3 wseng92  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 280
  • Joined: 23-September 15

Re: Navigation drawer app has stopped with fragments and JSON parsing

Posted 05 August 2017 - 11:04 AM

Please post the logcat errors
Was This Post Helpful? 0
  • +
  • -

#4 suhailpappu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 02-July 17

Re: Navigation drawer app has stopped with fragments and JSON parsing

Posted 05 August 2017 - 11:05 AM

View Postndc85430, on 05 August 2017 - 11:00 AM, said:

There's a lot of code there and I think you'd be hard pressed to find someone who'll debug it for you. What have you done to debug the problem? If the app is crashing, is an exception being thrown? If so, what does the stack trace tell you?

Well its like
08-05 23:34:40.309 23621-23621/com.pappu5.navigation I/art: Late-enabling -Xcheck:jni
08-05 23:34:40.368 23621-23621/com.pappu5.navigation W/System: ClassLoader referenced unknown path: /data/app/com.pappu5.navigation-2/lib/arm
08-05 23:34:40.516 23621-23621/com.pappu5.navigation D/AndroidRuntime: Shutting down VM
08-05 23:34:40.517 23621-23621/com.pappu5.navigation E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: com.pappu5.navigation, PID: 23621
                                                                       java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pappu5.navigation/com.pappu5.navigation.MainActivity}: java.lang.RuntimeException: [email protected] must implement OnFragmentInteractionListener
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724)
                                                                           at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                           at android.os.Looper.loop(Looper.java:154)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:6123)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
                                                                        Caused by: java.lang.RuntimeException: [email protected] must implement OnFragmentInteractionListener
                                                                           at com.pappu5.navigation.GalleryFrag.onAttach(GalleryFrag.java:157)
                                                                           at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1019)
                                                                           at android.support.v4.app.BackStackRecord.setLastIn(BackStackRecord.java:779)
                                                                           at android.support.v4.app.BackStackRecord.calculateFragments(BackStackRecord.java:819)
                                                                           at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:660)
                                                                           at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
                                                                           at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:339)
                                                                           at android.support.v4.app.FragmentActivity.onstart(FragmentActivity.java:601)
                                                                           at android.app.Instrumentation.callActivityonstart(Instrumentation.java:1270)
                                                                           at android.app.Activity.performStart(Activity.java:6689)
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2622)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724) 
                                                                           at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473) 
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                           at android.os.Looper.loop(Looper.java:154) 
                                                                           at android.app.ActivityThread.main(ActivityThread.java:6123) 
                                                                           at java.lang.reflect.Method.invoke(Native Method) 
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757) 


Was This Post Helpful? 0
  • +
  • -

#5 ndc85430  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 565
  • Posts: 2,322
  • Joined: 13-June 14

Re: Navigation drawer app has stopped with fragments and JSON parsing

Posted 05 August 2017 - 11:07 AM

Line 6 of the stack trace seems particularly pertinent. Have you tried addressing that issue?
Was This Post Helpful? 0
  • +
  • -

#6 suhailpappu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 02-July 17

Re: Navigation drawer app has stopped with fragments and JSON parsing

Posted 05 August 2017 - 11:14 AM

View Postndc85430, on 05 August 2017 - 11:07 AM, said:

Line 6 of the stack trace seems particularly pertinent. Have you tried addressing that issue?

well actually i am a noob in android programming and i dont know how to address those errors and if u please guide me it would be a great help
Was This Post Helpful? 0
  • +
  • -

#7 ndc85430  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 565
  • Posts: 2,322
  • Joined: 13-June 14

Re: Navigation drawer app has stopped with fragments and JSON parsing

Posted 05 August 2017 - 11:16 AM

This is basic Java. When we say one class implements something, what does that mean? You've already used this in your code, so you should know what it means.
Was This Post Helpful? 0
  • +
  • -

#8 suhailpappu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 02-July 17

Re: Navigation drawer app has stopped with fragments and JSON parsing

Posted 05 August 2017 - 11:25 AM

View Postndc85430, on 05 August 2017 - 11:16 AM, said:

This is basic Java. When we say one class implements something, what does that mean? You've already used this in your code, so you should know what it means.

Yea i just did that too i.e i implemented the onFragmentInteractionListener but the problem still persists...please help
Was This Post Helpful? 0
  • +
  • -

#9 ndc85430  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 565
  • Posts: 2,322
  • Joined: 13-June 14

Re: Navigation drawer app has stopped with fragments and JSON parsing

Posted 05 August 2017 - 11:26 AM

Show the stack trace after implementing the interface.
Was This Post Helpful? 0
  • +
  • -

#10 suhailpappu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 02-July 17

Re: Navigation drawer app has stopped with fragments and JSON parsing

Posted 05 August 2017 - 11:29 AM

View Postndc85430, on 05 August 2017 - 11:26 AM, said:

Show the stack trace after implementing the interface.

Here it is
08-05 23:58:45.031 29308-29308/? I/art: Late-enabling -Xcheck:jni
08-05 23:58:45.065 29308-29308/? W/System: ClassLoader referenced unknown path: /data/app/com.pappu5.navigation-2/lib/arm
08-05 23:58:45.172 29308-29308/? D/AndroidRuntime: Shutting down VM
08-05 23:58:45.173 29308-29308/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.pappu5.navigation, PID: 29308
                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pappu5.navigation/com.pappu5.navigation.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724)
                                                       at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                       at android.os.Looper.loop(Looper.java:154)
                                                       at android.app.ActivityThread.main(ActivityThread.java:6123)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
                                                    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
                                                       at com.pappu5.navigation.GalleryFrag.onCreateView(GalleryFrag.java:87)
                                                       at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
                                                       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
                                                       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
                                                       at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
                                                       at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
                                                       at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:339)
                                                       at android.support.v4.app.FragmentActivity.onstart(FragmentActivity.java:601)
                                                       at android.app.Instrumentation.callActivityonstart(Instrumentation.java:1270)
                                                       at android.app.Activity.performStart(Activity.java:6689)
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2622)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724) 
                                                       at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473) 
                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                       at android.os.Looper.loop(Looper.java:154) 
                                                       at android.app.ActivityThread.main(ActivityThread.java:6123) 
                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757) 


Was This Post Helpful? 0
  • +
  • -

#11 ndc85430  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 565
  • Posts: 2,322
  • Joined: 13-June 14

Re: Navigation drawer app has stopped with fragments and JSON parsing

Posted 05 August 2017 - 11:30 AM

So again, what is it telling you this time?
Was This Post Helpful? 0
  • +
  • -

#12 suhailpappu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 02-July 17

Re: Navigation drawer app has stopped with fragments and JSON parsing

Posted 05 August 2017 - 11:33 AM

View Postndc85430, on 05 August 2017 - 11:30 AM, said:

So again, what is it telling you this time?

i didnt understand that .. i am sorry and please will u just try to stop asking me back and if i knew how to solve it by just seeing the errors why would i be on this site? please i request u to solve my problem
Was This Post Helpful? 0
  • +
  • -

#13 ndc85430  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 565
  • Posts: 2,322
  • Joined: 13-June 14

Re: Navigation drawer app has stopped with fragments and JSON parsing

Posted 05 August 2017 - 11:38 AM

You're saying you've learnt Java and haven't encountered a NullPointerException? That seems quite strange. Maybe read the documentation for the exception to see what the cause could be.
Was This Post Helpful? 0
  • +
  • -

#14 suhailpappu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 02-July 17

Re: Navigation drawer app has stopped with fragments and JSON parsing

Posted 05 August 2017 - 11:47 AM

View Postndc85430, on 05 August 2017 - 11:38 AM, said:

You're saying you've learnt Java and haven't encountered a NullPointerException? That seems quite strange. Maybe read the documentation for the exception to see what the cause could be.

well i am sorry again i dont seem to find the situation in the code where the error is occuring
Was This Post Helpful? 0
  • +
  • -

#15 ndc85430  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 565
  • Posts: 2,322
  • Joined: 13-June 14

Re: Navigation drawer app has stopped with fragments and JSON parsing

Posted 05 August 2017 - 11:49 AM

After reading the documentation for NullPointerException, what seems likely and what seems unlikely? Remember, too, that you also have the message from the exception that's printed out in the stack trace.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2