由于添加了viewpager android

时间:2018-07-19 16:47:41

标签: android stack-overflow

我是Android编程的新手,正在创建一个使用NavigationView通过intents打开活动(使用TabLayout)的应用程序。该应用程序运行良好,直到我添加到ViewPager中,然后由于stackoverflow错误而导致应用程序崩溃。该代码是为TouristAttractions类提供的

   package com.example.android.tourguide;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;

public class TouristAttractions extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private NavigationView mNavigationView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu_item);

    mNavigationView = (NavigationView) findViewById(R.id.navigation_view);
    /*
       Finding the drawerLayout so that when the user clicks on the menu item of the navigation drawer it should close as we invoke the method closeDrawers()
     */
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

            if (menuItem.getItemId() == R.id.home_menu) {
                /*
                Opening the home class that is the MainActivity when the Tourist Home menu button is clicked.
                 */
                Intent intentToOpenHomeClass = new Intent(TouristAttractions.this, MainActivity.class);
                startActivity(intentToOpenHomeClass);
            } else if (menuItem.getItemId() == R.id.entertainment_menu) {
                Intent intentToOpenEntertainmentClass = new Intent(TouristAttractions.this, Entertainment.class);
                startActivity(intentToOpenEntertainmentClass);
            } else if (menuItem.getItemId() == R.id.gardens_menu) {
                Intent intentToOpenGardenClass = new Intent(TouristAttractions.this, Garden.class);
                startActivity(intentToOpenGardenClass);
            }
            else
            {
                mDrawerLayout.closeDrawers();
            }
     return true;
        }
    });
//Till here it works fine after adding the following code the app crashes.
    TabLayout tabLayout =(TabLayout)findViewById(R.id.tab_layout_for_menu_items);
    ViewPager viewPager=(ViewPager)findViewById(R.id.viewpager_for_menu_items);
    AdapterForFragmentOfTouristAttraction adapterForFragmentOfTouristAttraction=new AdapterForFragmentOfTouristAttraction(this,getSupportFragmentManager());
    viewPager.setAdapter(adapterForFragmentOfTouristAttraction);
    tabLayout.setupWithViewPager(viewPager);
}

}

,下面给出AdapterForFragmentOfTouristAttraction的代码

package com.example.android.tourguide;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class AdapterForFragmentOfTouristAttraction extends FragmentPagerAdapter {
/*
Context variable to get the string that is used in displaying the page title.
 */
private Context mContext;

public AdapterForFragmentOfTouristAttraction(Context context,FragmentManager fm) {
    super(fm);
    mContext = context;
}

@Override
public Fragment getItem(int position) {
    Fragment saveFragment = new Fragment();
    switch (position) {
        case 0:
            saveFragment = new MusemsFragmentForTouristAttractions();
            break;
        case 1:
            saveFragment = new FortFragmentForTouristAttractions();
            break;

    }
    return saveFragment;
}

@Nullable
@Override
public CharSequence getPageTitle(int position) {
    String saveReturnString = null;
    switch (position) {
        case 0:
            saveReturnString = mContext.getString(R.string.museums_under_tourist_attractions);
            break;
        case 1:
            saveReturnString = mContext.getString(R.string.forts_under_tourist_attractions);
            break;
    }
    return saveReturnString;
}

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

这是堆栈跟踪

java.lang.StackOverflowError: stack size 8MB
    at android.view.View.invalidateInternal(View.java:13653)
    at android.view.View.invalidate(View.java:13615)
    at android.view.View.invalidateDrawable(View.java:17833)
    at android.graphics.drawable.Drawable.invalidateSelf(Drawable.java:435)
    at android.graphics.drawable.RippleDrawable.invalidateSelf(RippleDrawable.java:704)
    at android.graphics.drawable.RippleDrawable.cancelExitingRipples(RippleDrawable.java:239)
    at android.graphics.drawable.RippleDrawable.jumpToCurrentState(RippleDrawable.java:223)
    at android.widget.AbsListView.jumpDrawablesToCurrentState(AbsListView.java:2941)
    at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:6463)
    at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:6463)
    at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:6463)
    at android.view.ViewGroup.jumpDr
[TRUNCATED 3158644 CHARS]
oCurrentState(ViewGroup.java:6463)
    at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:6463)
    at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:6463)
    at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:6463)
    at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:6463)
    at android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:6463)
    at android.view.View.onAttachedToWindow(View.java:15053)
    at android.view.ViewGroup.onAttachedToWindow(ViewGroup.java:4351)
    at android.support.v4.view.ViewPager.onAttachedToWindow(ViewPager.java:1544)
    at android.view.View.dispatchAttachedToWindow(View.java:15535)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2918)
    at android.view.ViewGroup.addViewInner(ViewGroup.java:4458)
    at android.view.ViewGroup.addViewInLayout(ViewGroup.java:4395)
    at android.view.ViewGroup.addViewInLayout(ViewGroup.java:4373)
    at android.support.v4.view.ViewPager.addView(ViewPager.java:1483)
    at android.view.ViewGroup.addView(ViewGroup.java:4200)
    at android.view.ViewGroup.addView(ViewGroup.java:4173)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1466)
    at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
    at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
    at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2623)
    at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2410)
    at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2365)
    at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2242)
    at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:654)
    at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:146)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:1244)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:1092)
    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1622)
    at android.view.View.measure(View.java:19886)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6085)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    at android.view.View.measure(View.java:19886)
    at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1121)
    at android.view.View.measure(View.java:19886)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6085)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
    at android.view.View.measure(View.java:19886)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6085)
    at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:401)
    at android.view.View.measure(View.java:19886)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6085)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    at android.view.View.measure(View.java:19886)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6085)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
    at android.view.View.measure(View.java:19886)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6085)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    at com.android.internal.policy.DecorView.onMeasure(DecorView.java:693)
    at android.view.View.measure(View.java:19886)
    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2316)
    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1407)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1660)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1295)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6400)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:876)
    at android.view.Choreographer.doCallbacks(Choreographer.java:688)
    at android.view.Choreographer.doFrame(Choreographer.java:623)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:862)
    at android.os.Handler.handleCallback(Handler.java:754)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:163)
    at android.app.ActivityThread.main(ActivityThread.java:6239)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)

由于android.view.ViewGroup.jumpDrawablesToCurrentState(ViewGroup.java:6463)连续重复多次,因此我从堆栈跟踪中删除了其中的一部分以减少堆栈跟踪的长度。

这是两个片段的片段

package com.example.android.tourguide;


import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * A simple {@link android.support.v4.app.Fragment} subclass.
 */
public class MusemsFragmentForTouristAttractions extends android.support.v4.app.Fragment {


    public MusemsFragmentForTouristAttractions() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.list_for_fragment,container);
        return view;
    }

}

0 个答案:

没有答案