-4

Edit: Before writing this question, I did not know why my app was crashing, and I didn't know how to fix it. The reason I am keeping this up is for beginners to learn what a logcat is (had I known that it existed, I probably wouldn't have written this question).

So, If your android app doesn't produce any errors when compiled, but crashes upon opening or after clicking something, check the logcat! At the bottom of the window, there is a task bar that has a few options: Run, TODO, Android Monitor, Terminal, and Messages. Click 'Android Monitor' and when that opens, choose the tab 'logcat' at the top.

My Original question:

My app was working correctly, but then stopped randomly. Why is this?

home

 public class Home extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);


    mPager = (ViewPager) findViewById(R.id.pager);
    mPagerAdapter = new ScreenSlidePager.ScreenSlidePagerAdapter(getSupportFragmentManager());
    mPager.setAdapter(mPagerAdapter);
}

private static final int NUM_PAGES = 5;

private ViewPager mPager;

private PagerAdapter mPagerAdapter;

@Override
public void onBackPressed() {
    if (mPager.getCurrentItem() == 0) {
        super.onBackPressed();
    }
    else {
        mPager.setCurrentItem(mPager.getCurrentItem() - 1);
    }
}
public class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
    public ScreenSlidePagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return new ScreenSlidePageFragment();
    }

    @Override
    public int getCount() {
        return NUM_PAGES;
    }
}


public void ffwePage(View view) {
    Intent intent = new Intent(Home.this, FamilyFocusedWorkEnvironment.class);
    startActivity(intent);
}

public void esePage(View view) {
    Intent intent = new Intent(Home.this, EvaluateSoftwareEffectiveness.class);
    startActivity(intent);
}

public void impPage(View view) {
    Intent intent = new Intent(Home.this, ImprovingMissionPerformance.class);
    startActivity(intent);
}

public void dcoPage(View view) {
    Intent intent = new Intent(Home.this, DefensiveCyberspaceOperations.class);
    startActivity(intent);
}

public void stayHome(View view) {
    Intent intent = new Intent(Home.this, Home.class);
    startActivity(intent);
}
}
Mairead
  • 275
  • 1
  • 2
  • 10
  • I know what an NPE is, I just don't know where in my code is causing it to occur, after looking through it. – Mairead Jun 21 '17 at 18:56
  • 1
    Read the stack trace. `at com.opsconsulting.mcummins.opswebsitetry4.Home.onCreate(Home.java:28)` – litelite Jun 21 '17 at 18:57
  • "I don't know where in my code it is occuring .... com.opsconsulting.mcummins.opswebsitetry4.Home.onCreate(Home.java:28)" – Dave S Jun 21 '17 at 19:08
  • The key skill here is to read the entire stack trace until you find a line which references something you wrote. If you do this, you will find `at com.opsconsulting.mcummins.opswebsitetry4.Home.onCreate(Home.java:28)` as mentioned above. `(Home‌​.java:28)` means that the error occurs at line 28 in `Home.java`. In Android Studio, just click on this and it will open that file directly to the offending line. See https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this for more debugging tips. – Code-Apprentice Jun 21 '17 at 19:10

1 Answers1

1

first of all change this line of code:

mPagerAdapter = new ScreenSlidePager.ScreenSlidePagerAdapter(getSupportFragmentManager());

to this:

mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());

it seems you copied your adapter from another page

change place you are defining your pager:

private ViewPager mPager;

above the onCreate

Meikiem
  • 1,876
  • 2
  • 11
  • 19