0

我是android開發的小菜鳥,在實例化ViewPager中的ImageView時遇到了問題。我的ViewPager有7頁,他們都有ImageViews。但是,只有第一頁上的ImageView實例化,調用時不會返回null。我反覆檢查以確保每個viewpager頁面都引用了正確的xml,並確保每個對應的xml都具有適當的ImageView。任何幫助是極大的讚賞。爲什麼ImageView沒有被實例化?

我的代碼

Working viewpager page。

case 0: 
       resId = (R.layout.reference_layout);     
       view = inflater.inflate(resId, null); 

       herb_ListView = (IndexableListView)view.findViewById(R.id.herbList);       
       herbsListresource = res.getStringArray(R.array.herblist); 

       herbsItems = new ArrayList<String>(); 
       Collections.addAll(herbsItems, herbsListresource);     
       Collections.sort(herbsItems); 

       ArrayAdapter<String> herbs_adapter = new CustomListAdapter(ReferenceActivity.this, R.layout.list_item_herbs); 
       for (int i=0; i<herbsItems.size();i++) 
        herbs_adapter.add(herbsItems.get(i)); 
       herb_ListView.setAdapter(herbs_adapter); 
       herb_ListView.setFastScrollEnabled(true); 
       herb_ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
        public void onItemClick(AdapterView<?> parent, final View view, int position, long id) { 

         View toolbar = view.findViewById(R.id.toolbar);            

         String herb_info = herb_ListView.getItemAtPosition(position).toString() + "_info"; 
         String new_herb_info = herb_info.replaceAll("\\s+", "").replace("'", "").replace(".", ""); 
         Log.e("herb_info result", new_herb_info); 
         String herb_pic = herb_ListView.getItemAtPosition(position).toString().toLowerCase() + "_picture"; 
         String new_herb_pic = herb_pic.replaceAll("\\s+", "").replace("'", "").replace(".", "");             
         Log.e("herb_pic result", new_herb_pic); 

         try{ 
          ((ImageView)view.findViewById(R.id.herb_image)).setImageResource((ReferenceActivity.this.getResources().getIdentifier(new_herb_pic, "drawable", ReferenceActivity.this.getApplicationInfo().packageName))); 
          ((TextView)view.findViewById(R.id.herb_description)).setText((ReferenceActivity.this.getResources().getIdentifier(new_herb_info, "string", ReferenceActivity.this.getApplicationInfo().packageName))); 
         }catch(Exception e){ 
          Log.e("Herb Info Error", "Error loading Herb Info >> " + e.getMessage() + " //" + e.toString()); 
         } 

         //Setting Arrows 
         if(((ImageView)view.findViewById(R.id.chevron)).getTag()=="down"||((ImageView)view.findViewById(R.id.chevron)).getTag()==null){        
          ((ImageView)view.findViewById(R.id.chevron)).setImageResource(R.drawable.chevron_white); 
          ((ImageView)view.findViewById(R.id.chevron)).setTag("up"); 
         }else{ 
          ((ImageView)view.findViewById(R.id.chevron)).setImageResource(R.drawable.chevron_white_down); 
          ((ImageView)view.findViewById(R.id.chevron)).setTag("down"); 
         } 

         // Creating the expand animation for the item 
         ExpandAnimation expandAni = new ExpandAnimation(toolbar, 500); 

         // Start the animation on the toolbar 
         toolbar.startAnimation(expandAni); 
        } 
       }); 


       ((ViewPager) collection).addView(view, 0); 
       return view; 

非工作viewpager頁面。

case 4: 
       resId = (R.layout.reference_layout);  
       view = inflater.inflate(resId, null); 

       crystals_ListView = (IndexableListView)view.findViewById(R.id.herbList);       
       crystalsListresource = res.getStringArray(R.array.crystallist); 

       crystalsItems = new ArrayList<String>(); 
       Collections.addAll(crystalsItems, crystalsListresource);     
       Collections.sort(crystalsItems); 

       ArrayAdapter<String> crystals_adapter = new CustomListAdapter(ReferenceActivity.this, R.layout.list_item_crystals); 
       for (int i=0; i<crystalsItems.size();i++) 
        crystals_adapter.add(crystalsItems.get(i)); 
       crystals_ListView.setAdapter(crystals_adapter); 
       crystals_ListView.setFastScrollEnabled(true); 
       crystals_ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
        public void onItemClick(AdapterView<?> parent, final View view, int position, long id) { 

         View toolbar = view.findViewById(R.id.crystal_toolbar);            

         String crystal_sign = crystals_ListView.getItemAtPosition(position).toString() + "_definition"; 
         String new_crystal_sign = crystal_sign.replaceAll("\\s+", "").replace("'", "").replace(".", "").replace("(", "").replace(")", ""); 
         Log.e("crystal_sign result", new_crystal_sign); 

         String crystal_mode = crystals_ListView.getItemAtPosition(position).toString() + "_optimumday"; 
         String new_crystal_mode = crystal_mode.replaceAll("\\s+", "").replace("'", "").replace(".", "").replace("(", "").replace(")", ""); 
         Log.e("crystal_mode_result", crystal_mode); 

         String crystal_usage = crystals_ListView.getItemAtPosition(position).toString() + "_usage"; 
         String new_crystal_usage = crystal_usage.replaceAll("\\s+", "").replace("'", "").replace(".", "").replace("(", "").replace(")", ""); 
         Log.e("crystal_usage_result", new_crystal_usage); 

         try{ 
          TextView crystalSign = (TextView)view.findViewById(R.id.crystal_sign); 
          TextView crystalMode = (TextView)view.findViewById(R.id.crystal_mode); 
          TextView crystalUsage = (TextView)view.findViewById(R.id.crystal_usage); 
          crystalSign.setText("Sign: " +(ReferenceActivity.this.getResources().getIdentifier(new_crystal_sign, "string", ReferenceActivity.this.getApplicationInfo().packageName))); 
          crystalMode.setText("Mode: " +(ReferenceActivity.this.getResources().getIdentifier(new_crystal_mode, "string", ReferenceActivity.this.getApplicationInfo().packageName))); 
          crystalUsage.setText("Usage: " +(ReferenceActivity.this.getResources().getIdentifier(new_crystal_usage, "string", ReferenceActivity.this.getApplicationInfo().packageName))); 

         }catch(Exception e){ 
          Log.e("Crystal Info Error", "Error loading Crystal Info >> " + e.getMessage() + " //" + e.toString()); 
         } 

         //Setting Arrows 
         ImageView chevron = (ImageView)view.findViewById(R.id.crystal_chevron); 
         if(chevron.getTag()=="down"||chevron.getTag()==null){ //NULLPOINTER THROWN HERE.       
          chevron.setImageResource(R.drawable.chevron_white); 
          chevron.setTag("up"); 
         }else{ 
          chevron.setImageResource(R.drawable.chevron_white_down); 
          chevron.setTag("down"); 
         } 


         // Creating the expand animation for the item 
         ExpandAnimation expandAni = new ExpandAnimation(toolbar, 500); 

         // Start the animation on the toolbar 
         toolbar.startAnimation(expandAni); 
        } 
       }); 

       ((ViewPager) collection).addView(view, 0); 
       return view; 

非工作viewpager XML。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<LinearLayout 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="horizontal" > 

<TextView 
    android:id="@+id/crystal_title" 
    android:layout_width="275dp" 
    android:layout_height="wrap_content" 
    android:focusable="false" 
    android:focusableInTouchMode="false" 
    android:padding="20dip" 
    android:textSize="20dp" /> 

<ImageView 
    android:id="@+id/crystal_chevron" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginRight="30dp" 
    android:focusable="false" 
    android:focusableInTouchMode="false" 
    android:src="@drawable/chevron_white_down" 
    android:layout_gravity="right" 
    android:padding="20dip"/> 

</LinearLayout> 

<!-- *********************** --> 
<!-- *** TOOLBAR LAYOUT **** --> 
<!-- *********************** --> 

<LinearLayout 
    android:id="@+id/crystal_toolbar" 
    android:layout_width="fill_parent" 
    android:layout_height="150dip" 
    android:layout_marginBottom="-50dip" 
    android:orientation="vertical" 
    android:visibility="gone" > 


    <TextView 
     android:id="@+id/crystal_sign" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:focusable="false" 
     android:focusableInTouchMode="false" 
     android:padding="2dip" 
     android:text="No information available" /> 

    <TextView 
     android:id="@+id/crystal_mode" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:focusable="false" 
     android:focusableInTouchMode="false" 
     android:padding="2dip" 
     android:text="No information available" /> 

    <TextView 
     android:id="@+id/crystal_usage" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:focusable="false" 
     android:focusableInTouchMode="false" 
     android:padding="2dip" 
     android:text="No information available" /> 
</LinearLayout> 

回答

0

我沒有這方面的任何硬證據,但我爲什麼在ViewPager第一片段中唯一的觀點可以發現的想法是因爲別人不「存在」。當然,它們存在於XML中,但我不知道一旦添加了Fragments,所有佈局都會膨脹並且變爲可用,以便在UI不可見(或根本不可見)時直接更新它們。

我也不認爲您可以使用findViewById在由ViewPager承載的Fragment範圍內查看視圖。 findViewById僅在所提供的內容視圖內看到 - 即該活動的佈局 - 或者您提供給它的特定佈局 - 例如如果你膨脹一個特定的佈局,比如一個列表項,然後調用findViewById來查找裏面的視圖,如TextView。您不應該只能在某個視圖上撥打findViewById,然後對其進行更改。

至於解決問題。你的觀點應該在片段本身內進行初始化。如果您是根據發生在什麼特林更新片段中的視圖您FragmentActivity那麼我會嘗試這個

(1)保持片段的引用要添加到您的ViewPager

Private whateverFragment f1 

//initialize onCreate 
f1= new WhateverFragment(); 

(2)在該片段中聲明一個公共方法,您可以使用該方法更新所需的視圖。請注意,該視圖是在片段內而不是主機活動中初始化的。

public updateTextView(String idk){ 
    TextView message = (TextView)getActivity().findViewById(R.id.message); 
    message.setText(idk); 
} 

(3)如果你想知道更新完成後,或交流者優先考慮從片段與片段活動回來,只需使用一個interface。最簡單的方法是在片段中聲明一個接口,在主機活動中實現該接口,並在片段的onAttach方法中初始化接口。通過這種方式,您可以在片段與其宿主活動之間進行回調。

使用這種方法,您幾乎可以控制包含視圖傳呼機

相關問題