Mairead Mairead - 1 year ago 37
Java Question

Determine why my android app crashes upon opening

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);
}
}

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download