0

我是 android 开发的菜鸟,在 ViewPager 中实例化 ImageView 时遇到问题。我的 ViewPager 有 7 个页面,它们都有 ImageViews。但是,只有第一页上的 ImageView 实例化并且在调用时不返回 null。我反复检查以确保每个 viewpager 页面都引用了正确的 xml,并确定每个相应的 xml 都有适当的 ImageView。任何帮助是极大的赞赏。

我的代码

工作浏览器页面。

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;

非工作浏览器页面。

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;

非工作查看器 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>

4

1 回答 1

0

我对此没有任何确凿的证据,但我对为什么只能ViewPager找到第一个片段中的视图的想法是因为其他不“存在”。当然,它们存在于 XML 中,但我不知道一旦Fragments添加了所有布局,所有布局都会被膨胀和调整,这样如果 UI 不可见(或根本不可见),您可以直接更新它们。

我也不认为您可以findViewById使用. 仅在提供的内容视图中查看 - 即该活动的布局 - 或您提供给它的特定布局 - 例如,如果您膨胀特定布局,如列表项的布局,然后调用该布局以在其中查找视图,像一个。您不应该只调用某些视图并更改它。FragmentViewPagerfindViewByIdfindViewByIdTextViewfindViewByIdFragment

至于解决问题。您的视图应该在片段本身内初始化。如果您想根据您发生的事情来更新片段中的视图,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 方法中初始化该接口。这样你就可以在片段和它的宿主活动之间回调。

使用这种方法,您几乎可以从包含视图寻呼机的活动中控制片段中的所有内容

于 2013-06-19T13:12:53.080 回答