2014-01-22 4 views
0

ViewPager를 작동 시키려고하지만 코드의 한 섹션에서 문제가 발생합니다. 문제가되는 코드 섹션은 바로 아래에 게시됩니다. 내 생각에, 내 TextViews는 반복적으로 위치가 배열의 크기 인 getCount() 메서드에 설정된 값으로 반복되고 있으므로 업데이트되고 있습니다.ViewPager 및 TextView 업데이트. 치명적인 예외입니까?

어쨌든,이 모든 것의 아래에 지금까지 가지고있는대로 내 코드가 꽉 찼습니다. 여기

  // Capture position and set to the TextViews 
      tvNumber.setText(arrayAtomicNum.get(position)); 
      tvSymbol.setText(arraySymbol.get(position)); 
      tvName.setText(arrayName.get(position)); 
      tvGroup.setText(arrayGroup.get(position)); 
      tvPeriod.setText(arrayPeriod.get(position)); 
      tvBlock.setText(arrayBlock.get(position)); 
      tvFamily.setText(arrayFamily.get(position)); 
      tvColor.setText(arrayColor.get(position)); 
      tvPhase.setText(arrayPhase.get(position)); 
      tvNeutrons.setText(arrayNeutrons.get(position)); 
      tvProtons.setText(arrayProtons.get(position)); 
      tvElectrons.setText(arrayElectrons.get(position)); 

ViewPagerAdapter.java

에서

문제있는 코드는 내가

public class LearnActivity extends Activity { 
    public static String TAG = LearnActivity.class.getSimpleName(); 

    ViewPager viewPager; 
    PagerAdapter adapter; 

    private int atomicNum = 0; 

    private int temp1; 
    private int temp2; 

    List<Integer> arrayAtomicNum = new ArrayList<Integer>(); 
    List<String> arrayName = new ArrayList<String>(); 
    List<String> arraySymbol = new ArrayList<String>(); 
    List<String> arrayFamily = new ArrayList<String>(); 
    List<String> arrayPhase = new ArrayList<String>(); 
    List<String> arrayColor = new ArrayList<String>(); 
    List<Integer> arrayGroup = new ArrayList<Integer>(); 
    List<Integer> arrayPeriod = new ArrayList<Integer>(); 
    List<String> arrayBlock = new ArrayList<String>(); 
    List<Integer> arrayProtons = new ArrayList<Integer>(); 
    List<Integer> arrayNeutrons = new ArrayList<Integer>(); 
    List<Integer> arrayElectrons = new ArrayList<Integer>(); 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.activity_learn); 
     Log.d(TAG, "Learn layout loaded successfully"); 

     while (atomicNum < 3) { 
     atomicNum++; 

     if (atomicNum == 1) { 
      ElementStructure.setName("Hydrogen"); 
      ElementStructure.setSymbol("H"); 
      ElementStructure.setFamily("Non metals"); 
      ElementStructure.setPhase("Gas"); 
      ElementStructure.setColor("Colorless"); 
      ElementStructure.setAtomicNum(atomicNum); 
      ElementStructure.setGroup(1); 
      ElementStructure.setPeriod(1); 
      ElementStructure.setBlock("s"); 
      ElementStructure.setProton(atomicNum); 
     } else if (atomicNum == 2) { 
      ElementStructure.setName("Helium"); 
      ElementStructure.setSymbol("He"); 
      ElementStructure.setFamily("Noble gases"); 
      ElementStructure.setPhase("Gas"); 
      ElementStructure.setColor("Colorless"); 
      ElementStructure.setAtomicNum(atomicNum); 
      ElementStructure.setGroup(18); 
      ElementStructure.setPeriod(1); 
      ElementStructure.setBlock("s"); 
      ElementStructure.setProton(atomicNum); 
     } else if (atomicNum == 3) { 
      ElementStructure.setName("Lithium"); 
      ElementStructure.setSymbol("Li"); 
      ElementStructure.setFamily("Alkali metals"); 
      ElementStructure.setPhase("Solid"); 
      ElementStructure.setColor("Silvery white"); 
      ElementStructure.setAtomicNum(atomicNum); 
      ElementStructure.setGroup(1); 
      ElementStructure.setPeriod(2); 
      ElementStructure.setBlock("s"); 
     } 

     // Atoms must have equal number(s) of protons and electrons 
     ElementStructure.setElectron(ElementStructure.getProton()); 

     /* Legend: 
     * temp1-atomic weight rounded to nearest whole integer 
     * temp2-number of neutrons */ 
     //temp1 = (int)Math.round(ElementStructure.getWeight()); 
     temp1 = 100; //just a test value 
     temp2 = temp1 - ElementStructure.getProton(); 
     ElementStructure.setNeutron(temp2); 

     // Populate arrays 
     arrayAtomicNum.add(ElementStructure.getAtomicNum()); 
     arrayName.add(ElementStructure.getName()); 
     arraySymbol.add(ElementStructure.getSymbol()); 
     arrayFamily.add(ElementStructure.getFamily()); 
     arrayPhase.add(ElementStructure.getPhase()); 
     arrayColor.add(ElementStructure.getColor()); 
     arrayGroup.add(ElementStructure.getGroup()); 
     arrayPeriod.add(ElementStructure.getPeriod()); 
     arrayBlock.add(ElementStructure.getBlock()); 
     arrayProtons.add(ElementStructure.getProton()); 
     arrayNeutrons.add(ElementStructure.getNeutron()); 
     arrayElectrons.add(ElementStructure.getElectron()); 

     } //end while loop 


     viewPager = (ViewPager)findViewById(R.id.pager); 
     viewPager.setOffscreenPageLimit(3); 
     adapter = new ViewPagerAdapter(LearnActivity.this, arrayAtomicNum, arrayName, 
       arraySymbol, arrayFamily, arrayPhase, arrayColor, arrayGroup, arrayPeriod, 
       arrayBlock, arrayProtons, arrayNeutrons, arrayElectrons); 
     viewPager.setAdapter(adapter); 


    } 

} 
LearnActivity.java라는 다른 활동에 내 arrayLists을 채우는, ViewPagerAdapter.java

public class ViewPagerAdapter extends PagerAdapter { 
    public static String TAG = ViewPagerAdapter.class.getSimpleName(); 

    Context context; 
    LayoutInflater inflater; 


    List<Integer> arrayAtomicNum = new ArrayList<Integer>(); 
    List<String> arrayName = new ArrayList<String>(); 
    List<String> arraySymbol = new ArrayList<String>(); 
    List<String> arrayFamily = new ArrayList<String>(); 
    List<String> arrayPhase = new ArrayList<String>(); 
    List<String> arrayColor = new ArrayList<String>(); 
    List<Integer> arrayGroup = new ArrayList<Integer>(); 
    List<Integer> arrayPeriod = new ArrayList<Integer>(); 
    List<String> arrayBlock = new ArrayList<String>(); 
    List<Integer> arrayProtons = new ArrayList<Integer>(); 
    List<Integer> arrayNeutrons = new ArrayList<Integer>(); 
    List<Integer> arrayElectrons = new ArrayList<Integer>();  


    public ViewPagerAdapter(Context context, List<Integer> arrayAtomicNum, 
      List<String> arrayName, List<String> arraySymbol, List<String> arrayFamily, 
      List<String> arrayPhase, List<String> arrayColor, List<Integer> arrayGroup, 
      List<Integer> arrayPeriod, List<String> arrayBlock, List<Integer> arrayProtons, 
      List<Integer> arrayNeutrons, List<Integer> arrayElectrons) { 
     this.context = context; 
     this.arrayAtomicNum = arrayAtomicNum; 
     this.arrayName = arrayName; 
     this.arraySymbol = arraySymbol; 
     this.arrayFamily = arrayFamily; 
     this.arrayPhase = arrayPhase; 
     this.arrayColor = arrayColor; 
     this.arrayGroup = arrayGroup; 
     this.arrayPeriod = arrayPeriod; 
     this.arrayBlock = arrayBlock; 
     this.arrayProtons = arrayProtons; 
     this.arrayNeutrons = arrayNeutrons; 
     this.arrayElectrons = arrayElectrons; 

    } 


    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     //return 0; 
     return arrayAtomicNum.size(); 
    } 

    @Override 
    public boolean isViewFromObject(View view, Object object) { 
     // TODO Auto-generated method stub 
     //return false; 
     return view == ((RelativeLayout) object); 

    } 

    @Override 
     public Object instantiateItem(ViewGroup container, int position) { 

      // Declare Variables 
      TextView tvNumber; // container for atomic number 
      TextView tvSymbol; // container for symbol 
      TextView tvName; // container for name 
      TextView tvGroup; // container for group 
      TextView tvPeriod; // container for period 
      TextView tvBlock; // container for block 
      TextView tvFamily; // container for family 
      TextView tvColor; // container for color 
      TextView tvPhase; // container for phase 
      TextView tvNeutrons; // container for neutrons 
      TextView tvProtons; // container for protons 
      TextView tvElectrons; // container for electrons 

      inflater = (LayoutInflater) context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View itemView = inflater.inflate(R.layout.frame_learn, container, 
        false); 

      // Locate the TextViews in viewpager_item.xml 
      // atomic number textView 
      tvNumber = (TextView)itemView.findViewById(R.id.metanumber); 

      // symbol textView 
      tvSymbol = (TextView)itemView.findViewById(R.id.metasymbol); 

      // name textView 
      tvName = (TextView)itemView.findViewById(R.id.metaname); 

      // group textView 
      tvGroup = (TextView)itemView.findViewById(R.id.metagroup); 

      // period textView 
      tvPeriod = (TextView)itemView.findViewById(R.id.metaperiod); 

      // block textView 
      tvBlock = (TextView)itemView.findViewById(R.id.metablock); 

      // family textView 
      tvFamily = (TextView)itemView.findViewById(R.id.metafamily); 

      // color textView 
      tvColor = (TextView)itemView.findViewById(R.id.metacolor); 

      // phase textView 
      tvPhase = (TextView)itemView.findViewById(R.id.metaphase); 

      // neutrons textView 
      tvNeutrons = (TextView)itemView.findViewById(R.id.metaneutrons); 

      // protons textView 
      tvProtons = (TextView)itemView.findViewById(R.id.metaprotons); 

      // electrons textView 
      tvElectrons = (TextView)itemView.findViewById(R.id.metaelectrons); 

      //comment out the setText and logs will print out full arrayLists 
      Log.d(TAG, "arrayAtomicNum.get(position): " + arrayAtomicNum.get(position)); 
      Log.d(TAG, "arraySymbol.get(position): " + arraySymbol.get(position)); 
      Log.d(TAG, "arrayName.get(position): " + arrayName.get(position)); 

      // Capture position and set to the TextViews 
      tvNumber.setText(arrayAtomicNum.get(position)); 
      tvSymbol.setText(arraySymbol.get(position)); 
      tvName.setText(arrayName.get(position)); 
      tvGroup.setText(arrayGroup.get(position)); 
      tvPeriod.setText(arrayPeriod.get(position)); 
      tvBlock.setText(arrayBlock.get(position)); 
      tvFamily.setText(arrayFamily.get(position)); 
      tvColor.setText(arrayColor.get(position)); 
      tvPhase.setText(arrayPhase.get(position)); 
      tvNeutrons.setText(arrayNeutrons.get(position)); 
      tvProtons.setText(arrayProtons.get(position)); 
      tvElectrons.setText(arrayElectrons.get(position)); 

      // Add viewpager_item.xml to ViewPager 
      ((ViewPager) container).addView(itemView); 

      return itemView; 
     } 

     @Override 
     public void destroyItem(ViewGroup container, int position, Object object) { 
      // Remove viewpager_item.xml from ViewPager 
      ((ViewPager) container).removeView((RelativeLayout) object); 

     } 
} 

전체 클래스

모든 arrayLists에 올바른 값이 있습니다. 내가 원하는 모든 데이터를 적절하게 연관시켰다. 더 많은 것이 있지만, 나는이 지위를 제한하려고 노력하고있다. 두 경우 모두 아래 오류가 발생하는 이유는 무엇입니까? 나는 setText를 시도 할 때 그것이 일어난다는 것을 안다, 그러나 왜? 당신은 텍스트 뷰에 텍스트로 정수를 설정하려면 그냥 int 값의 텍스트 뷰를 설정합니다 경우

답변

2

해당 int 값을 가지는 자원을 액세서하려고합니다

textView.setText(String.valueOf(intValue)); 

를 사용해야합니다.

+0

예! 당신 Eddy 남자, 그게 내가이 오류를 얻는 이유입니다. 모든 것이 이제 완벽하게 작동합니다! 이 팁을 확실히 기억해 두겠습니다. 이제 알겠습니다. – portfoliobuilder

+0

오신 것을 환영합니다. .. – Eddy