片段事务分离和附加后ListView不工作?
Posted
技术标签:
【中文标题】片段事务分离和附加后ListView不工作?【英文标题】:ListView Not Working after Fragment Transaction detach and attach? 【发布时间】:2018-01-24 18:47:19 【问题描述】:我正在运行时替换一个片段以显示另一个片段,其中包含一个列表视图,该列表视图显示在运行时从远程服务器获取的数据。选择列表中的项目时!主要活动中的函数 delete_surah() 通过称为 DarsCommunicator 的接口从片段中调用。被调用的函数从数据库中删除单行,然后通过将其分离来刷新片段,然后将其重新附加到活动中,但在重新附加片段时,应用程序崩溃并且日志 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:尝试调用虚拟方法 空对象上的“java.lang.String java.lang.Object.toString()” 参考 在 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) 在 java.lang.reflect.Method.invoke(本机方法) 在 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】:你得到一个NullPointerException
,因为你的数组是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];
【讨论】:
以上是关于片段事务分离和附加后ListView不工作?的主要内容,如果未能解决你的问题,请参考以下文章