Note that there are some explanatory texts on larger screens.

plurals
  1. PONull pointer exception when creating another fragments
    primarykey
    data
    text
    <p>Im using viewPager with action bar tabs in my app and every fragment has own fragment xml file for UI but I have two problems. First problem is if I create 3 fragments then when app is laoded first fragment is Ok I swipe to second - second is Ok then I swipe to third and return tu first and first fragment is demaged - has default values(all data are losed but I dont know why) Second biggest problem is when I create third fragment and I want initialize items inside him I get null pointer because(I think third fragment wasnt created or maybe is not good). Null pointer exception on <code>PlaceholderFragment3()</code></p> <p>Here is my code></p> <pre><code>public class SectionsPagerAdapter extends FragmentPagerAdapter { public SectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { switch (position) { case 0: return new PlaceholderFragment().newInstance(0); case 1: return new PlaceholderFragment2().newInstance(0); case 2: return new PlaceholderFragment3().newInstance(0); } return null; } @Override public int getCount() { return 3; } @Override public CharSequence getPageTitle(int position) { switch (position) { case 0: return getString(R.string.title_section1); case 1: return getString(R.string.title_section2); case 2: return getNameOfDayOfWeek(DAY_AFTER_TOMORROW); } return null; } } public static class PlaceholderFragment extends Fragment { private static final String ARG_SECTION_NUMBER = "section_number"; public static PlaceholderFragment newInstance(int sectionNumber) { PlaceholderFragment fragment = new PlaceholderFragment(); Bundle args = new Bundle(); args.putInt(ARG_SECTION_NUMBER, sectionNumber); fragment.setArguments(args); return fragment; } public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); MainActivity.iMainActivity.dateToday=(TextView) rootView.findViewById(R.id.tvDateOfDay); return rootView; } } </code></pre> <p>ANOTHER FRAGMENTS ARE CREATED LIKE PlaceholderFragment only name is changed to PlaceholderFragment2 and 3</p> <pre><code>public static MainActivity iMainActivity; // instance of MainActivity public class JSONWeatherTaskDay3 extends AsyncTask&lt;String, Void, Weather&gt; { @Override protected Weather doInBackground(String... params) { Weather weatherDay3 = new Weather(); String data = ((new WeatherHttpClient()).getWeatherData(params[0])); try { weatherDay3 = JSONWeatherParser.getWeather(data,2); } catch (JSONException e) { e.printStackTrace(); } return weatherDay3; } @Override protected void onPostExecute(Weather weather) { MainActivity.iMainActivity.cityNameDay3.setText(weather.location.getCity()); } .... </code></pre> <p>}</p> <p>JSONWeatherTaskDay3 is Task for third fragment where I get exception, fragment 1 and 2 have another TASK classes</p> <p>Exception></p> <pre><code>java.lang.NullPointerException at com.pfittech.andweather.MainActivity$JSONWeatherTaskDay3.onPostExecute(MainActivity.java:636) at com.pfittech.andweather.MainActivity$JSONWeatherTaskDay3.onPostExecute(MainActivity.java:618) at android.os.AsyncTask.finish(AsyncTask.java:602) at android.os.AsyncTask.access$600(AsyncTask.java:156) at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4448) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590) at dalvik.system.NativeStart.main(Native Method) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload