q9722088
2017-12-29 10:58
采纳率: 54.5%
浏览 4.6k

Android Studio报错 java.lang.IllegalStateException

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条回答 默认 最新

相关推荐 更多相似问题