how to fix the app is stopped in this Fragment

时间:2019-01-09 22:17:20

标签: java android xml

i trying to make horizontal slider same in google play app but in Fragment below all my file include the code i have this code from other site but its not working with me - the code not make any error in android studio also its run but when open this fragment the app stop working - how to solve that plz thanks

main Fragment.java

public class SearchFragment extends Fragment  {
private static final String TAG = "SearchFragment";
private RecyclerView my_recycler_view;
private Toolbar toolbar;

private final static String TEST_URL = 
 "http://monaasoft.com/indianfm/api/test.json";
List<Data> allSampleData;



@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 
container, @Nullable Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_search, container, 
false);
    if (toolbar != null) {

        toolbar.setTitle("G PlayStore");

    }


    fetch_category(TEST_URL);


    return view;
}

private void fetch_category(String url) {

allSampleData = new ArrayList<Data>();
toolbar = (Toolbar) toolbar.findViewById(R.id.toolbar);

AsyncHttpClient client = new AsyncHttpClient();
client.get(url, new JsonHttpResponseHandler() {

    public void onSuccess(int statusCode, PreferenceActivity.Header[] 
headers, JSONObject response) {


        if (statusCode == 200 && response != null) {
            Log.i("response-", response.toString());


            try {
                JSONArray dataArary = response.getJSONArray("data");


                for (int i = 0; i < dataArary.length(); i++) {
                    JSONObject sectionObj = (JSONObject) dataArary.get(i);

                    String title = sectionObj.getString("title");


                    List<Section> sections = new ArrayList<Section>();


                    JSONArray sectionsArray = 
  sectionObj.getJSONArray("section");

                    for (int j = 0; j < sectionsArray.length(); j++) {

                        JSONObject obj = (JSONObject) sectionsArray.get(j);


                        Section section = new Section();

                        section.setName(obj.getString("name"));
                        section.setImage(obj.getString("image"));


                        sections.add(section);
                    }


                    Data data = new Data();

                    data.setTitle(title);
                    data.setSection(sections);


                    allSampleData.add(data);


                }


            } catch (JSONException e) {
                e.printStackTrace();

            }

            if (allSampleData != null) {

                my_recycler_view = (RecyclerView) 
  my_recycler_view.findViewById(R.id.my_recycler_view);

                my_recycler_view.setHasFixedSize(true);

                RecyclerViewDataAdapter adapter = new 
RecyclerViewDataAdapter(getContext(), allSampleData);

                my_recycler_view.setLayoutManager(new 
LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));

                my_recycler_view.setAdapter(adapter);


            } else {
                Toast.makeText(getContext(), "Connection Error", 
Toast.LENGTH_SHORT).show();

            }


        }



    }



});
}
}

not get any error in code but the app is stop working
im still very new in java so may be i do too much mistake

1 个答案:

答案 0 :(得分:0)

But 99/100 times when this question is asked, it's a NullPointerException

Yes.

The first one is your toolbar, here it is:

private void fetch_category(String url) {

    allSampleData = new ArrayList<Data>();
    toolbar = (Toolbar) toolbar.findViewById(R.id.toolbar);  //<- here
    ...

Getting toolbar from toolbar?

You probably meant view.findViewById(R.id.toolbar), but from your current setup, you would need to first come up with a way to pass that view to it.

Good luck, and next time, please provide the stacktrace from your logcat, if you can't debug it yourself.