Answer the question
In order to leave comments, you need to log in
Why does the application crash when I return to the first two tabs in the Tabbed Activity?
I created a tabbed activity in the standard way built into android studio. Each tab contains 2 nested fragments - in the first fragment there are filters, depending on the choice of which the required second fragment is nested. Tabs normally pass in order from first to last (4 pieces). Between the first two tabs it slides perfectly, and between 3 and 4 it also switches normally. But if you return to the 1st or 2nd tab from the 3rd or 4th tab, then the application crashes, and it always swears at the fragment belonging to the first tab, even if you slide from the 3rd to the 2nd tab. What's wrong?
Switch between tabs in activity
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);
}
}
}
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: Shutting down VM
09-21 18:07:56.920 1729-1729/im.upgrade.upgradeim W/dalvikvm:threadid=1: thread exiting with uncaught exception (group=0xb2cd8908)
09-21 18:07:56.920 1729-1729/im.upgrade.upgradeim E/AndroidRuntime: FATAL EXCEPTION: main
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(Native Method)
Answer the question
In order to leave comments, you need to log in
At the time the thread ends, the fragment may not be attached to the activity, and then getActivity() will return null and everything will fall. Wrap the code like this
if(isAdded()){
ArrayAdapter adapter;
adapter = new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_activated_1, allGoalsList);
Put a breakpoint there and in debug mode see where you have null there.
I suspect that getActivity() returns null. If so, then keep the context in the fragment.
As one of the options in the adapter constructor, add the R.id of the sheet item.
For example, I crashed when
but it turned out that the adapter still needs a sheet item that needs to be defined
adapter = new ArrayAdapter<>(MainActivity.this, R.layout.list_item,
R.id.textView, tags);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question