我通过android studio中内置的标准方法创建了选项卡式活动。每个选项卡包含两个子片段-第一个片段具有微调器,具体取决于将哪个嵌套片段放在右边。这些选项卡通常从第一个滑动到最后一个(四个选项卡)。在第一个和第二个选项卡之间可以完美滑动,在3和4之间也可以正常切换。但是,如果我尝试将3或4滑回到第一个或第二个选项卡,则应用程序崩溃,总是将责任归咎于第一个选项卡中的嵌套片段,即使幻灯片是从第3到第二个选项卡也是如此。怎么了?
在TabActivity中的选项卡之间切换
switch(position){
case 0:
Fragment addNewFragment = new AddNew_fragment();
Bundle args = new Bundle();
args.putString("connectURL", connectURL);
args.putString("access_token", access_token);
addNewFragment.setArguments(args);
return addNewFragment;
case 1:
Fragment toDoListFragment = new ToDoListFragment();
Bundle args1 = new Bundle();
args1.putString("connectURL", connectURL);
args1.putString("access_token", access_token);
toDoListFragment.setArguments(args1);
return toDoListFragment;
case 2:
Fragment allNewsFragment = new AllNewsFragment();
Bundle args2 = new Bundle();
args2.putString("connectURL", connectURL);
args2.putString("access_token", access_token);
allNewsFragment.setArguments(args2);
return allNewsFragment;
case 3:
Fragment myPageFragment = new MyPageFragment();
Bundle args3 = new Bundle();
args3.putString("connectURL", connectURL);
args3.putString("access_token", access_token);
myPageFragment.setArguments(args3);
return myPageFragment;
default:
return null;
}
第一个选项卡上的嵌套片段
public class GoalsListFragment extends ListFragment {
int goalID;
int goalExist;
JSONObject jsonResponseReports = null;
JSONObject jsonResponse;
JSONArray items;
String goalDescriptionText;
StringBuilder feedInput;
String connectURL;
ArrayList<String> allGoalsList;
ArrayAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
connectURL = getArguments().getString("connectURL");
View goalFragment = inflater.inflate(R.layout.fragment_goals_list, null);
allGoalsList = new ArrayList<String>();
new GetAllGoals().execute();
return goalFragment;
}
private class GetAllGoals extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
URL url = new URL(connectURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true); // Triggers POST.
connection.setDoInput(true);
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
feedInput = new StringBuilder();
String inputBufferSymbol = "";
while ((inputBufferSymbol = br.readLine()) != null) {
feedInput.append(inputBufferSymbol);
}
br.close();
jsonResponseReports = new JSONObject(feedInput.toString());
jsonResponse = jsonResponseReports.getJSONObject("response");
if (jsonResponse.getString("count").equals("0")) {
goalDescriptionText = "В этой сфере жизни вы не поставили ни одной цели";
goalExist = 0;
} else {
goalExist = 1;
items = jsonResponse.getJSONArray("items");
int count=0;
while (count<Integer.parseInt(jsonResponse.getString("count")))
{
allGoalsList.add(items.getJSONObject(count).getString("title"));
count++;
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_activated_1, allGoalsList);
setListAdapter(adapter);
}
}
}
始终将责任归咎于onPostExecute中的适配器
adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_activated_1, allGoalsList);
09-21 18:07:56.920 1729-1729 / im.upgrade.upgradeim D / Android运行时:
关闭VM 09-21 18:07:56.920 1729-1729 / im.upgrade.upgradeim
W / dalvikvm:线程id = 1:线程退出,未捕获的异常
(group = 0xb2cd8908)09-21 18:07:56.920 1729-1729 / im.upgrade.upgradeim
E / Android运行时:致命异常:主要09-21 18:07:56.920
1729-1729 / im.upgrade.upgradeim E / Android运行时:
java.lang.NullPointerException 09-21 18:07:56.920
1729-1729 / im.upgrade.upgradeim E / Android运行时:at
android.widget.ArrayAdapter.init(ArrayAdapter.java:310)09-21
18:07:56.920 1729-1729 / im.upgrade.upgradeim E / Android运行时:在
android.widget.ArrayAdapter。(ArrayAdapter.java:153)
09-21 18:07:56.920 1729-1729 / im.upgrade.upgradeim E / AndroidRuntime:at fragment.GoalsListFragment $ GetAllGoals.onPostExecute(GoalsListFragment.java:122)
09/21 18:07:56.920 1729-1729 / im.upgrade.upgradeim E / AndroidRuntime:at fragment.GoalsListFragment $ GetAllGoals.onPostExecute(GoalsListFragment.java:63)
09-21 18:07:56.920 1729-1729 / im.upgrade.upgradeim E / Android运行时:at
android.os.AsyncTask.finish(AsyncTask.java:631)09-21 18:07:56.920
1729-1729 / im.upgrade.upgradeim E / Android运行时:at
android.os.AsyncTask.access $ 600(AsyncTask.java:177)09-21 18:07:56.920
1729-1729 / im.upgrade.upgradeim E / Android运行时:at
android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:644)
09-21 18:07:56.920 1729-1729 / im.upgrade.upgradeim E / Android运行时:at
android.os.Handler.dispatchMessage(Handler.java:99)09-21 18:07:56.920
1729-1729 / im.upgrade.upgradeim E / Android运行时:at
android.os.Looper.loop(Looper.java:137)09-21 18:07:56.920
1729-1729 / im.upgrade.upgradeim E / Android运行时:at
android.app.ActivityThread.main(ActivityThread.java:5041)09-21
18:07:56.920 1729-1729 / im.upgrade.upgradeim E / Android运行时:在
java.lang.reflect.Method.invokeNative(本机方法)09-21
18:07:56.920 1729-1729 / im.upgrade.upgradeim E / Android运行时:在
java.lang.reflect.Method.invoke(Method.java:511)09-21 18:07:56.920
1729-1729 / im.upgrade.upgradeim E / Android运行时:at
com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
09-21 18:07:56.920 1729-1729 / im.upgrade.upgradeim E / Android运行时:at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)09-21
18:07:56.920 1729-1729 / im.upgrade.upgradeim E / Android运行时:在
dalvik.system.NativeStart.main(本机方法)
最佳答案
尝试记录以查看getActivity()是否为null或allGoalList
我的猜测是碎片正在处理,其中之一为空。
您应该每次在应用程序中检查非null尝试此操作
@Override
protected void onPostExecute(Void aVoid)
{
if(null != getActivity() )
{
if(null != allGoalsList){ // log to find error}
adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_activated_1, allGoalsList);
setListAdapter(adapter);
}
}