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方法中初始化接口。通过这种方式,您可以在片段与其宿主活动之间进行回调。

使用这种方法,您几乎可以控制包含视图传呼机

相关问题