我使用Volley从phpmysql数据库中获取数据。现在,每当我单击按钮显示对话框时,我的应用程序都会崩溃并显示以下错误:
有什么问题?
这是我的代码:
public class SetTask extends Fragment {
ListView lv_task_chooser;
ArrayList<SetTaskModel> setTaskModelArrayList;
private SetTaskAdapter setTaskAdapter;
ArrayAdapter<String> scAdapter;
String url, category;
RequestQueue queue_fetch, queue_fetch1;
ConnectivityManager connMgr;
NetworkInfo networkInfo;
Context ctx;
int cat_id;
LinearLayout taskLayout;
private String[] array;
private int[] sub_cat_id;
JSONArray jsonArray, jsonArray1;
public SetTask() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_set_task, container, false);
ctx = getContext();
lv_task_chooser = (ListView)rootView.findViewById(R.id.lv_task_chooser);
taskLayout = (LinearLayout)rootView.findViewById(R.id.taskLayout);
queue_fetch = Volley.newRequestQueue(ctx);
queue_fetch1 = Volley.newRequestQueue(ctx);
setTaskModelArrayList = new ArrayList<>();
fetchDB();
Log.i("pointafterfetch", "reached");
Log.i("numberofarrayentries", String.valueOf(setTaskModelArrayList.size()));
for (int i = 0; i < setTaskModelArrayList.size(); i++){
Log.i("arrayentriesfetch", setTaskModelArrayList.get(i).toString());
}
//setTaskAdapter = new SetTaskAdapter(setTaskModelArrayList, ctx);
//lv_task_chooser.setAdapter(setTaskAdapter);
/*lv_task_chooser.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
populateSpinner(position);
}
});*/
return rootView;
}
private void fetchDB(){
url = "http://nmstech.in/coach/categorys.php";
connMgr = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
networkInfo = connMgr.getActiveNetworkInfo();
if (!(networkInfo != null && networkInfo.isConnected())) {
final AlertDialog.Builder builder1 = new AlertDialog.Builder(ctx);
builder1.setTitle(getString(R.string.network_no_t));
builder1.setMessage(getString(R.string.network_no_m));
builder1.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder1.create().show();
}
else{
StringRequest strreq_fetch = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String Response) {
try {
jsonArray = new JSONArray(Response);
for(int i = 0; i < jsonArray.length(); i++){
setTaskModelArrayList.add(new SetTaskModel(jsonArray.getJSONObject(i)
.getString("cat_name")));
Log.i("arraylistentries", setTaskModelArrayList.get(i).getCategory());
Log.i("noentries", String.valueOf(setTaskModelArrayList.size()));
setTaskAdapter = new SetTaskAdapter(setTaskModelArrayList, ctx);
lv_task_chooser.setAdapter(setTaskAdapter);
setTaskAdapter.notifyDataSetChanged();
lv_task_chooser.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
populateSpinner(position);
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError e) {
e.printStackTrace();
}
});
Log.i("pointnotesfetch", "reached");
queue_fetch.add(strreq_fetch);
}
}
private void populateSpinner(final int position){
url = "http://nmstech.in/coach/categorys.php";
connMgr = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
networkInfo = connMgr.getActiveNetworkInfo();
if (!(networkInfo != null && networkInfo.isConnected())) {
final AlertDialog.Builder builder1 = new AlertDialog.Builder(ctx);
builder1.setTitle(getString(R.string.network_no_t));
builder1.setMessage(getString(R.string.network_no_m));
builder1.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder1.create().show();
}
else {
Log.i("spinpop", "reached");
StringRequest strreq_fetch1 = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String Response) {
try {
cat_id = (int) lv_task_chooser.getItemIdAtPosition(position);
jsonArray1 = new JSONArray(Response);
final AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setTitle("Subtopics");
connMgr = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
networkInfo = connMgr.getActiveNetworkInfo();
category = jsonArray1.getJSONObject(cat_id).getString("cat_name");
int k = 0;
try {
for (int i = 0; i < jsonArray1.getJSONObject(position).getJSONArray("sub_cat").length(); i++){
array = new String[jsonArray1.getJSONObject(position).getJSONArray("sub_cat").length()];
sub_cat_id = new int[jsonArray1.getJSONObject(position).getJSONArray("sub_cat").length()];
array[i] = jsonArray1.getJSONObject(position).getJSONArray("sub_cat").getJSONObject(i).getString("sub_cat_name");
sub_cat_id[k++] = jsonArray1.getJSONObject(position).getJSONArray("sub_cat").getJSONObject(i).getInt("sub_cat_id");
Log.i("sub_cat_id", String.valueOf(sub_cat_id[k-1]));
Log.i("sub_cat", array[i]);
}
scAdapter = new ArrayAdapter<String>(ctx, android.R.layout.simple_spinner_dropdown_item, array);
}
catch (Exception e){
e.printStackTrace();
}
builder.setItems(array, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Bundle cat_args = new Bundle();
cat_args.putInt("sub_cat_id", sub_cat_id[which]);
cat_args.putString("sub_cat", array[which]);
Fragment fragment = new Exercise();
FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
fragment.setArguments(cat_args);
fragmentTransaction.replace(R.id.mainFrame, fragment);
fragmentTransaction.commit();
}
});
builder.show();
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError e) {
e.printStackTrace();
}
});
Log.i("pointnotesfetch", "reached");
queue_fetch1.add(strreq_fetch1);
}
}
}
编辑: 添加完整的堆栈跟踪
06-29 10:38:25.499 9183-9183 / nmss.example.com.coach E / AndroidRuntime: 致命异议:主要 处理:nmss.example.com.coach,PID:9183 java.lang.NullPointerException:尝试调用虚方法 &#39; java.lang.String java.lang.Object.toString()&#39;在null对象上 参考 在 android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:409) 在android.widget.ArrayAdapter.getView(ArrayAdapter.java:371) 在android.widget.AbsListView.obtainView(AbsListView.java:2367) 在android.widget.ListView.measureHeightOfChildren(ListView.java:1326) 在android.widget.ListView.onMeasure(ListView.java:1233) 在android.view.View.measure(View.java:19756) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6124) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:185) 在android.view.View.measure(View.java:19756) 在 android.support.v7.widget.AlertDialogLayout.tryOnMeasure(AlertDialogLayout.java:135) 在 android.support.v7.widget.AlertDialogLayout.onMeasure(AlertDialogLayout.java:63) 在android.view.View.measure(View.java:19756) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6124) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:185) 在 android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139) 在android.view.View.measure(View.java:19756) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6124) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:185) 在android.view.View.measure(View.java:19756) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6124) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:185) 在android.view.View.measure(View.java:19756) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6124) 在 android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464) 在android.widget.LinearLayout.measureVertical(LinearLayout.java:758) 在android.widget.LinearLayout.onMeasure(LinearLayout.java:640) 在android.view.View.measure(View.java:19756) 在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6124) 在android.widget.FrameLayout.onMeasure(FrameLayout.java:185) 在com.android.internal.policy.DecorView.onMeasure(DecorView.java:687) 在android.view.View.measure(View.java:19756) 在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2283) 在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1343) 在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1619) 在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1258) 在 android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6348) 在 android.view.Choreographer $ CallbackRecord.run(Choreographer.java:871) 在android.view.Choreographer.doCallbacks(Choreographer.java:683) 在android.view.Choreographer.doFrame(Choreographer.java:619) 在 android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:857) 在android.os.Handler.handleCallback(Handler.java:751) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:6123) at java.lang.reflect.Method.invoke(Native Method) 在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:867) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
编辑2:
为SetTaskAdapter类添加代码
public class SetTaskAdapter extends ArrayAdapter<SetTaskModel>{
private ArrayList<SetTaskModel> data;
Context mContext;
private static class ViewHolder{
Button btn_category;
}
public SetTaskAdapter(ArrayList<SetTaskModel> data, Context mContext){
super(mContext, R.layout.lv_task_chooser, data);
this.data = data;
this.mContext = mContext;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
SetTaskModel setTaskModel = getItem(position);
ViewHolder viewHolder;
final View result;
if(convertView == null){
viewHolder = new ViewHolder();
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
convertView = layoutInflater.inflate(R.layout.lv_task_chooser, parent, false);
viewHolder.btn_category = (Button)convertView.findViewById(R.id.btn_category);
result = convertView;
convertView.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder)convertView.getTag();
result = convertView;
}
viewHolder.btn_category.setText(setTaskModel.getCategory());
return convertView;
}
}
答案 0 :(得分:0)
尝试此代码,初始化for循环中的字符串数组和int数组。
示例:
array = new String[jsonArray1.getJSONObject(position).getJSONArray("sub_cat").length()];
sub_cat_id = new int[jsonArray1.getJSONObject(position).getJSONArray("sub_cat").length()];
try {
for (int i = 0; i < jsonArray1.getJSONObject(position).getJSONArray("sub_cat").length(); i++){
array[i] = jsonArray1.getJSONObject(position).getJSONArray("sub_cat").getJSONObject(i).getString("sub_cat_name");
sub_cat_id[k++] = jsonArray1.getJSONObject(position).getJSONArray("sub_cat").getJSONObject(i).getInt("sub_cat_id");
Log.i("sub_cat_id", String.valueOf(sub_cat_id[k-1]));
Log.i("sub_cat", array[i]);
}
scAdapter = new ArrayAdapter<String>(ctx, android.R.layout.simple_spinner_dropdown_item, array);
}
catch (Exception e){
e.printStackTrace();
}