q9722088
q9722088
采纳率54.5%
2017-12-29 10:58 阅读 4.5k

Android Studio报错 java.lang.IllegalStateException

5

package com.example.q9722.biancheng1;

import android.media.Image;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
ViewPager mviewPager;
mFragmentAdapter madapter;
FragmentOne mone;
FragmentTwo mtwo;
FragmentThree mthree;
List mFragmentList;
ImageView[] mimageViews;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
initViews();
madapter = new mFragmentAdapter(getSupportFragmentManager(),mFragmentList);
mviewPager.setOffscreenPageLimit(3);
mviewPager.setAdapter(madapter);
mviewPager.setCurrentItem(0);
mimageViews[1].setImageResource(R.drawable.blue_circle);
mviewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            for(int i=0;i<3;i++) {
                mimageViews[i].setImageResource(R.drawable.write_circle);
            }
            mimageViews[position].setImageResource(R.drawable.blue_circle);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });
}

private void initViews() {
    mviewPager = (ViewPager) findViewById(R.id.viewpager);
    mFragmentList = new ArrayList<Fragment>();
    mone = new FragmentOne();
    mtwo = new FragmentTwo();
    mthree = new FragmentThree();
    mFragmentList.add(mone);
    mFragmentList.add(mtwo);
    mFragmentList.add(mthree);
    mimageViews = new ImageView[3];
    mimageViews[0] = (ImageView) findViewById(R.id.iv1);
    mimageViews[1] = (ImageView) findViewById(R.id.iv2);
    mimageViews[2] = (ImageView) findViewById(R.id.iv3);
    mimageViews[0].setOnClickListener(this);
    mimageViews[1].setOnClickListener(this);
    mimageViews[2].setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.iv1:
            mviewPager.setCurrentItem(0,true);
            break;
        case R.id.iv2:
            mviewPager.setCurrentItem(1,true);
            break;
        case R.id.iv3:
            mviewPager.setCurrentItem(2,true);
            break;
    }
}

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

10条回答 默认 最新

  • 已采纳
    sixiaobing sixiaobing 2017-12-29 12:21

    引用v4,v7只需其中一个即可,无需重复添加,会报错

    点赞 评论 复制链接分享
  • q9722088 q9722088 2017-12-29 10:59

    E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.q9722.biancheng1, PID: 10821
    java.lang.IllegalStateException: No tab known for tag null
    at android.support.v4.app.FragmentTabHost.doTabChanged(FragmentTabHost.java:336)
    at android.support.v4.app.FragmentTabHost.onAttachedToWindow(FragmentTabHost.java:282)
    at android.view.View.dispatchAttachedToWindow(View.java:15417)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2955)
    at android.view.ViewGroup.addViewInner(ViewGroup.java:4495)
    at android.view.ViewGroup.addViewInLayout(ViewGroup.java:4432)
    at android.view.ViewGroup.addViewInLayout(ViewGroup.java:4410)
    at android.support.v4.view.ViewPager.addView(ViewPager.java:1475)
    at android.view.ViewGroup.addView(ViewGroup.java:4237)
    at android.view.ViewGroup.addView(ViewGroup.java:4210)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1124)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
    at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1638)
    at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:679)
    at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:1240)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:1088)
    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1614)
    at android.view.View.measure(View.java:19759)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6122)
    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:19759)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6122)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
    at android.view.View.measure(View.java:19759)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6122)
    at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:389)
    at android.view.View.measure(View.java:19759)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6122)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    at android.view.View.measure(View.java:19759)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6122)
    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:19759)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6122)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
    at com.android.internal.policy.DecorView.onMeasure(DecorView.java:690)
    at android.view.View.measure(View.java:19759)
    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2317)
    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1404)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1653)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1292)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6368)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:873)
    at android.view.Choreographer.doCallbacks(Choreographer.java:685)
    at android.view.Choreographer.doFrame(Choreographer.java:621)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:859)
    at android.os.Handler.handleCallback(Handler.java:754)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:163)
    E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6321)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)
    Application terminated.

    点赞 评论 复制链接分享
  • caozhy 从今以后生命中的每一秒都属于我爱的人 2017-12-29 11:10
  • sinat_39220407 sinat_39220407 2017-12-29 12:58

    引用v4,v7只需其中一个,无需重复添加,会报错

    点赞 评论 复制链接分享
  • dcxy0 Q544471255 2017-12-29 14:12

    http://ask.csdn.net/questions/349183

    要不用v4,要不用v7,不要混用

    import android.support.v4.app.Fragment;
    import android.support.v4.view.ViewPager;
    import android.support.v7.app.AppCompatActivity;

    点赞 评论 复制链接分享
  • weixin_40076295 weixin_40076295 2017-12-29 14:14

    可以加我的qq,我给你讲讲

    点赞 评论 复制链接分享
  • qq_41415844 qq_41415844 2017-12-29 23:16

    要么用v4,要么用v7,不要混用,引用v4,v7只需其中一个,无需重复添加,会报错

    点赞 评论 复制链接分享
  • ywyjn ywyjn 2017-12-30 02:12

    v4,v7两者只需使用一个,不要混用。建议使用v7,适应于任何版本的开发,保证了兼容性。

    点赞 评论 复制链接分享
  • lwh912 lwh923 2017-12-30 05:23

    fragmengAdapter里面有没报错

    点赞 评论 复制链接分享
  • MS_blogger MS_blogger 2017-12-30 08:02

    尝试一下将API的版本调低,一般来说API 19是兼容性最广的。

    点赞 评论 复制链接分享

相关推荐