ListView Not Working after Fragment Transaction detach and attach?
I am replacing a fragment at runtime to show an other fragment which contains a listview which shows data fetched from a remote server at run time. When an item from the list is selected! a function delete_surah() which is in the main activity is called from the fragment through an interface known as DarsCommunicator. The function that is called deletes a single row from the database and then refreshes the fragment by detaching it and then reattaching it again with the activity but on reattaching the fragment the application crashes and log cat shows an error . Code is following ..
Main Activity :
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();
}
Fragment :
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() function:
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 works fine first time and shows correct data but after performing a transaction from a delete_surah() function, the application crashes and logcat show following error :
Error Log:
Process: com.example.mashood.muftiattaullahmultanicom, PID: 13762 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394) at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362) at android.widget.AbsListView.obtainView(AbsListView.java:2402) at android.widget.ListView.makeAndAddView(ListView.java:1891) at android.widget.ListView.fillDown(ListView.java:713) at android.widget.ListView.fillFromTop(ListView.java:779) at android.widget.ListView.layoutChildren(ListView.java:1699) at android.widget.AbsListView.onLayout(AbsListView.java:2206) at android.view.View.layout(View.java:16075) at android.view.ViewGroup.layout(ViewGroup.java:5300) at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077) at android.view.View.layout(View.java:16075) at android.view.ViewGroup.layout(ViewGroup.java:5300) at android.widget.FrameLayo ut.layoutChildren(FrameLayout.java:579) at android.widget.FrameLayout.onLayout(FrameLayout.java:514) at android.view.View.layout(View.java:16075) at android.view.ViewGroup.layout(ViewGroup.java:5300) at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077) at android.view.View.layout(View.java:16075) at android.view.ViewGroup.layout(ViewGroup.java:5300) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) at android.widget.FrameLayout.onLayout(FrameLayout.java:514) at android.view.View.layout(View.java:16075) at android.view.ViewGroup.layout(ViewGroup.java:5300) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557) at android.widget.LinearLayout.onLayout(LinearLayout.java:1466) at android.view.View.layout(View.java:16075) at android.view.ViewGroup.layout(ViewGroup.java:5300) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) at android.widget.FrameLayout.onLayou t(FrameLayout.java:514) at android.view.View.layout(View.java:16075) at android.view.ViewGroup.layout(ViewGroup.java:5300) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557) at android.widget.LinearLayout.onLayout(LinearLayout.java:1466) at android.view.View.layout(View.java:16075) at android.view.ViewGroup.layout(ViewGroup.java:5300) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) at android.widget.FrameLayout.onLayout(FrameLayout.java:514) at android.view.View.layout(View.java:16075) at android.view.ViewGroup.layout(ViewGroup.java:5300) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2119) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1873) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1073) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5988) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:76 7) at android.view.Choreographer.doCallbacks(Choreographer.java:580) at android.view.Choreographer.doFrame(Choreographer.java:550) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5910) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)`
You are getting a NullPointerException
because your array is null
. I suggest you initialize the arrays when you declare them when to make sure they are not 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];
链接地址: http://www.djcxy.com/p/93154.html
上一篇: 为微调器设置默认选择