片段事务分离并附加后ListView不工作?

时间:2017-08-16 14:46:05

标签: android listview android-volley

我在运行时替换片段以显示包含listview的其他片段,该列表视图显示在运行时从远程服务器获取的数据。当列表中的项目被选中时!主要活动中的函数delete_surah()通过称为DarsCommunicator的接口从片段调用。调用的函数从数据库中删除单个行,然后通过分离它来刷新片段,然后使用活动重新连接它,但在重新附加片段时应用程序崩溃并且log cat显示错误。代码如下......

主要活动:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.content_dars);
    ContentDarsSurahFragmentTwo fragmentSurahtwo=new 
    ContentDarsSurahFragmentTwo();
    transaction.replace(R.id.frame,fragmentSurahtwo,"fragmentsurahtwo");
    transaction.commit();
    }

片段:

 package com.example.mashood.muftiattaullahmultanicom;

 import android.app.Fragment;
 import android.os.Bundle;
 import android.support.annotation.Nullable;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.AdapterView;
 import android.widget.ArrayAdapter;
 import android.widget.ListAdapter;
 import android.widget.ListView;
 import android.widget.TextView;
 import android.widget.Toast;

 import com.android.volley.Request;
 import com.android.volley.Response;
 import com.android.volley.VolleyError;
 import com.android.volley.toolbox.StringRequest;

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

 import java.util.HashMap;
 import java.util.Map;

/**
 * Created by Mashood on 8/16/2017.
 */

public class ContentDarsSurahFragmentTwo extends Fragment {
View view;
TextView text;
ListView list;
String surah_no;
String surah_name;
public static String url = 
           "http://www.muftiattaullahmultani.com/android/get_all_surah.php";
String[] dars_topic;
String[] dars_id;
String[] dars_link;
String[] dars_date;
String[] data;
int count = 0;
int i = 0;
DarsCommunicator communicator;

  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 
   container, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.audio_fragment_three, container, 
   false);

      return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    text=(TextView)view.findViewById(R.id.textView);
    text.setText("Dars e Quran List");
    communicator=(DarsCommunicator)getActivity();
    StringRequest stringRequest=new StringRequest(Request.Method.POST, url, 
     new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

            JSONObject object2=null;
            JSONArray object = null;
            try {
                object = new JSONArray(response);
                text.setText("here");
                dars_topic = new String[object.length()];
                dars_id = new String[object.length()];
                dars_link = new String[object.length()];
                dars_date = new String[object.length()];

                while (count < object.length()) {
                    object2 = object.getJSONObject(count);
                    dars_id[count] = object2.getString("surah_no");
                    dars_topic[count] = object2.getString("surah_name");
                    text.setText("here2");
                    count++;
                }

                list = (ListView) view.findViewById(R.id.list);
                ListAdapter adapter = new ArrayAdapter<String>
           (getActivity(), android.R.layout.simple_list_item_1, dars_topic);
                list.setAdapter(adapter);
                list.setOnItemClickListener(new 
                AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View 
       view, int position, long id) {

                        surah_no=dars_id[position];
                        surah_name=dars_topic[position];
                         communicator.delete_surah(surah_no,surah_name);
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }}, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getActivity(),"Error",Toast.LENGTH_LONG).show();
        }
    });
    MySingleton.getInstance(getActivity()).addToRequestQueue(stringRequest);
    }

  }

delete_surah()函数:

    public void delete_surah(final String surah_no, final String 
        surah_name)
    {

    url="http://www.muftiattaullahmultani.com/android/delete_surah.php";
    AlertDialog.Builder builder=new AlertDialog.Builder(this);
    builder.setTitle("Delete "+surah_name+" ?");
    builder.setMessage("Are you sure you want to delete "+surah_name+" ?");
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

            StringRequest stringRequest=new 
     StringRequest(Request.Method.POST, url, new Response.Listener<String>() 
    {

                @Override
                public void onResponse(String response) {
                    Toast.makeText(getApplicationContext(),surah_name+" 
    Deleted Succesfully",Toast.LENGTH_LONG).show();
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {


  Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_LONG).show();
                }
            }){
                protected Map<String,String> getParams()
                {
                    Map<String,String>param=new HashMap<String, String>();
                    param.put("surah_no",surah_no);
                    return param;
                }

            };       

   MySingleton.getInstance(getApplicationContext()).
 addToRequestQueue(stringRequest);
            ContentDarsSurahFragmentTwo fragmentsurahtwo=

(ContentDarsSurahFragmentTwo)getFragmentManager().
findFragmentByTag("fragmentsurahtwo");
            FragmentTransaction 
 transaction=getFragmentManager().beginTransaction();
       transaction.detach(fragmentsurahtwo);
            transaction.attach(fragmentsurahtwo);
            transaction.commit();
        }
    });
    builder.setNegativeButton("Cancel",null);
    builder.show();
   }

ContentDarsSurahFragmentTwo第一次正常工作并显示正确的数据但是在从delete_surah()函数执行事务后,应用程序崩溃并且logcat显示以下错误:

错误日志:

  

流程:com.example.mashood.muftiattaullahmultanicom,PID:13762                                                                                                 java.lang.NullPointerException:尝试调用虚方法   &#39; java.lang.String java.lang.Object.toString()&#39;在null对象上   参考                                                                                                     在   android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)                                                                                                     在android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)                                                                                                     在android.widget.AbsListView.obtainView(AbsListView.java:2402)                                                                                                     在android.widget.ListView.makeAndAddView(ListView.java:1891)                                                                                                     在android.widget.ListView.fillDown(ListView.java:713)                                                                                                     在android.widget.ListView.fillFromTop(ListView.java:779)                                                                                                     在android.widget.ListView.layoutChildren(ListView.java:1699)                                                                                                     在android.widget.AbsListView.onLayout(AbsListView.java:2206)                                                                                                     在android.view.View.layout(View.java:16075)                                                                                                     在android.view.ViewGroup.layout(ViewGroup.java:5300)                                                                                                     在android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077)                                                                                                     在android.view.View.layout(View.java:16075)                                                                                                     在android.view.ViewGroup.layout(ViewGroup.java:5300)                                                                                                     在android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)                                                                                                     在android.widget.FrameLayout.onLayout(FrameLayout.java:514)                                                                                                     在android.view.View.layout(View.java:16075)                                                                                                     在android.view.ViewGroup.layout(ViewGroup.java:5300)                                                                                                     在android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077)                                                                                                     在android.view.View.layout(View.java:16075)                                                                                                     在android.view.ViewGroup.layout(ViewGroup.java:5300)                                                                                                     在android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)                                                                                                     在android.widget.FrameLayout.onLayout(FrameLayout.java:514)                                                                                                     在android.view.View.layout(View.java:16075)                                                                                                     在android.view.ViewGroup.layout(ViewGroup.java:5300)                                                                                                     在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)                                                                                                     在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)                                                                                                     在android.widget.LinearLayout.onLayout(LinearLayout.java:1466)                                                                                                     在android.view.View.layout(View.java:16075)                                                                                                     在android.view.ViewGroup.layout(ViewGroup.java:5300)                                                                                                     在android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)                                                                                                     在android.widget.FrameLayout.onLayout(FrameLayout.java:514)                                                                                                     在android.view.View.layout(View.java:16075)                                                                                                     在android.view.ViewGroup.layout(ViewGroup.java:5300)                                                                                                     在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)                                                                                                     在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)                                                                                                     在android.widget.LinearLayout.onLayout(LinearLayout.java:1466)                                                                                                     在android.view.View.layout(View.java:16075)                                                                                                     在android.view.ViewGroup.layout(ViewGroup.java:5300)                                                                                                     在android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)                                                                                                     在android.widget.FrameLayout.onLayout(FrameLayout.java:514)                                                                                                     在android.view.View.layout(View.java:16075)                                                                                                     在android.view.ViewGroup.layout(ViewGroup.java:5300)                                                                                                     在android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2119)                                                                                                     在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1873)                                                                                                     在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1073)                                                                                                     在   android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:5988)                                                                                                     在   android.view.Choreographer $ CallbackRecord.run(Choreographer.java:767)                                                                                                     在android.view.Choreographer.doCallbacks(Choreographer.java:580)                                                                                                     在android.view.Choreographer.doFrame(Choreographer.java:550)                                                                                                     在   android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:753)                                                                                                     在android.os.Handler.handleCallback(Handler.java:739)                                                                                                     在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                                     在android.os.Looper.loop(Looper.java:135)                                                                                                     在android.app.ActivityThread.main(ActivityThread.java:5910)                                                                                                     at java.lang.reflect.Method.invoke(Native Method)                                                                                                     在java.lang.reflect.Method.invoke(Method.java:372)                                                                                                     在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1405)                                                                                                     在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)`

1 个答案:

答案 0 :(得分:0)

您收到NullPointerException,因为您的数组是null。我建议你在声明它们的时候初始化数组,以确保它们不是null:

String[] dars_topic = new String[0];
String[] dars_id = new String[0];
String[] dars_link = new String[0];
String[] dars_date = new String[0];
String[] data = new String[0];