Java 如何修复此片段中停止的应用程序

Java 如何修复此片段中停止的应用程序,java,android,xml,Java,Android,Xml,我试图使水平滑块在谷歌播放应用程序相同,但在片段 下面我的所有文件都包含代码,我从其他网站获得了这些代码,但这些代码对我不起作用-这些代码在android studio中不会出现任何错误,也不会运行,但是当打开这个片段时,应用程序停止工作-如何解决这个问题 谢谢 main Fragment.java public class SearchFragment extends Fragment { private static final String TAG = "SearchFragment";

我试图使水平滑块在谷歌播放应用程序相同,但在片段 下面我的所有文件都包含代码,我从其他网站获得了这些代码,但这些代码对我不起作用-这些代码在android studio中不会出现任何错误,也不会运行,但是当打开这个片段时,应用程序停止工作-如何解决这个问题 谢谢

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();

            }


        }



    }



});
}
}
公共类SearchFragment扩展了片段{
私有静态最终字符串TAG=“SearchFragment”;
私人回收站查看我的回收站查看;
专用工具栏;
私有最终静态字符串测试\u URL=
"http://monaasoft.com/indianfm/api/test.json";
列出所有样本数据;
@可空
@凌驾
创建视图上的公共视图(LAYOUTINGER充气机,@Nullable ViewGroup
容器,@Nullable Bundle savedInstanceState){
视图=充气机。充气(R.layout.fragment\u搜索,容器,
假);
如果(工具栏!=null){
工具栏.setTitle(“G PlayStore”);
}
获取类别(测试URL);
返回视图;
}
私有void fetch_类别(字符串url){
allSampleData=new ArrayList();
toolbar=(toolbar)toolbar.findviewbyd(R.id.toolbar);
AsyncHttpClient=新的AsyncHttpClient();
get(url,新的JsonHttpResponseHandler(){
成功时公共无效(int状态代码,PreferenceActivity.Header[]
标题(JSONObject响应){
if(statusCode==200&&response!=null){
Log.i(“response-”,response.toString());
试一试{
JSONArray dataarry=response.getJSONArray(“数据”);
对于(int i=0;i
代码中没有任何错误,但应用程序已停止工作
我对java还是很陌生,所以可能我犯了太多的错误

但是99/100次当这个问题被问到时,它是一个 NullPointerException

第一个是您的
工具栏
,它是:

private void fetch_category(String url) {

    allSampleData = new ArrayList<Data>();
    toolbar = (Toolbar) toolbar.findViewById(R.id.toolbar);  //<- here
    ...
private void fetch\u类别(字符串url){
allSampleData=new ArrayList();

toolbar=(toolbar)toolbar.findViewById(R.id.toolbar);//您需要包含日志猫。但是99/100次当这样的问题被问到时,它是一个错误。