分割后的ListView不工作事务分离并附加?
我在运行时替换一个片段以显示其他片段,其中包含一个列表视图,该列表显示在运行时从远程服务器获取的数据。 当选择列表中的项目时! 通过名为DarsCommunicator的接口从片段中调用主活动中的函数delete_surah()。 被调用的函数从数据库中删除单个行,然后通过分离刷新碎片,然后再次与该活动重新附加碎片,但在重新附加碎片时应用程序崩溃并记录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 show following error:
错误日志:
进程:com.example.mashood.muftiattaullahmultanicom,PID:13762 java.lang.NullPointerException:尝试在android.widget的空对象引用上调用虚方法'java.lang.String java.lang.Object.toString()'。在android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)android.widget.AbsListView.obtainView(AbsListView.java:2402)android.widget.ListView.makeAndAddView(ListView)上的ArrayAdapter.createViewFromResource(ArrayAdapter.java:394) .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)在android.widget的android.view.ViewGroup.layout(ViewGroup.java:5300)上的android.view.View.layout(View.java:16075)处的android.widget.AbsListView.onLayout(AbsListView.java:2206)。在android.widget.FrameLayo上android.view.ViewGroup.layout(ViewGroup.java:5300)android.view.View.layout(View.java:16075)处的RelativeLayout.onLayout(RelativeLayout.java:1077) 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)在android.view.ViewGroup.layout(ViewGroup.java:5300)的android.view.View.layout(View.java:16075)android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077)在android.view.android.view.View.layout(View.java:16075)处的android.widget.FrameLayout.onLayout(FrameLayout.java:514)处的android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)。在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)在android.widget.LinearLayout.onLayout(LinearLayout)上的ViewGroup.layout(ViewGroup.java:5300) .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)在android.widget.FrameLayout.onLayou 在android.view.View.layout(View.java:16075)android.view.ViewGroup.layout(ViewGroup.java:5300)在android.widget.LinearLayout.setChildFrame(LinearLayout.java)的t(FrameLayout.java:514) :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。在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java)处的android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2119)处的android.view.ViewGroup.layout(ViewGroup.java:5300)处的布局(View.java:16075) :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)在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:753)at android.view.Choreographer.doFrame(Choreographer.java:550)android.view.Choreographer.doCallbacks(Choreographer.java:580)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)`
你正在得到一个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];
链接地址: http://www.djcxy.com/p/93153.html
上一篇: ListView Not Working after Fragment Transaction detach and attach?
下一篇: Android DiagonalLayout java.lang.IllegalArgumentException: path must be convex