如何修复该应用程序在此片段中停止



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

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

}

}

});
}
}

代码中没有任何错误,但应用程序已停止工作
我在java中仍然很新,所以可能是我犯了太多错误

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

是。

第一个是你的toolbar,在这里:

private void fetch_category(String url) {
allSampleData = new ArrayList<Data>();
toolbar = (Toolbar) toolbar.findViewById(R.id.toolbar);  //<- here
...

toolbar获取toolbar

你可能是指view.findViewById(R.id.toolbar),但从你目前的设置来看,你需要首先想出一种方法来将view传递给它

祝你好运,下次,如果你不能自己调试,请从你的logcat中提供stacktrace

最新更新