我通过android studio中内置的标准方法创建了标签式活动。每个选项卡包含两个子片段 - 第一个片段具有微调器,具体取决于嵌套片段的选择。标签通常从第一个到最后一个滑动(4个标签)。在第一和第二突片之间完全滑动,在3和4之间也是正常的开关。但是,如果我尝试从3或4滑回第1或第2个选项卡,应用程序崩溃,总是责怪第一个选项卡中的嵌套片段,即使幻灯片是从第3个到第2个选项卡。怎么了?
在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 / AndroidRuntime: 关闭VM 09-21 18:07:56.920 1729-1729 / im.upgrade.upgradeim W / dalvikvm:threadid = 1:线程退出未捕获的异常 (group = 0xb2cd8908)09-21 18:07:56.920 1729-1729 / im.upgrade.upgradeim E / AndroidRuntime:FATAL EXCEPTION:主09-21 18:07:56.920 1729-1729 / im.upgrade.upgradeim E / AndroidRuntime: java.lang.NullPointerException 09-21 18:07:56.920 1729-1729 / im.upgrade.upgradeim E / AndroidRuntime:at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)09-21 18:07:56.920 1729-1729 / im.upgrade.upgradeim E / AndroidRuntime:at android.widget.ArrayAdapter。(ArrayAdapter.java:153)
09-21 18:07:56.920 1729-1729 / im.upgrade.upgradeim E / AndroidRuntime:at fragments.GoalsListFragment $ GetAllGoals.onPostExecute(GoalsListFragment.java:122) 09-21 18:07:56.920 1729-1729 / im.upgrade.upgradeim E / AndroidRuntime:at fragments.GoalsListFragment $ GetAllGoals.onPostExecute(GoalsListFragment.java:63)
09-21 18:07:56.920 1729-1729 / im.upgrade.upgradeim E / AndroidRuntime:at android.os.AsyncTask.finish(AsyncTask.java:631)09-21 18:07:56.920 1729-1729 / im.upgrade.upgradeim E / AndroidRuntime:at android.os.AsyncTask.access $ 600(AsyncTask.java:177)09-21 18:07:56.920 1729-1729 / im.upgrade.upgradeim E / AndroidRuntime:at android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:644) 09-21 18:07:56.920 1729-1729 / im.upgrade.upgradeim E / AndroidRuntime:at android.os.Handler.dispatchMessage(Handler.java:99)09-21 18:07:56.920 1729-1729 / im.upgrade.upgradeim E / AndroidRuntime:at android.os.Looper.loop(Looper.java:137)09-21 18:07:56.920 1729-1729 / im.upgrade.upgradeim E / AndroidRuntime:at android.app.ActivityThread.main(ActivityThread.java:5041)09-21 18:07:56.920 1729-1729 / im.upgrade.upgradeim E / AndroidRuntime:at java.lang.reflect.Method.invokeNative(Native Method)09-21 18:07:56.920 1729-1729 / im.upgrade.upgradeim E / AndroidRuntime:at java.lang.reflect.Method.invoke(Method.java:511)09-21 18:07:56.920 1729-1729 / im.upgrade.upgradeim E / AndroidRuntime:at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793) 09-21 18:07:56.920 1729-1729 / im.upgrade.upgradeim E / AndroidRuntime:at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)09-21 18:07:56.920 1729-1729 / im.upgrade.upgradeim E / AndroidRuntime:at dalvik.system.NativeStart.main(原生方法)
答案 0 :(得分:1)
尝试登录以查看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);
}
}