为什么不将ImageView实例化?

时间:2013-06-19 12:38:07

标签: android xml nullpointerexception imageview android-viewpager

我是Android开发的菜鸟,我在ViewPager中实例化ImageView时遇到了麻烦。我的ViewPager有7页,所有页面都有ImageViews。但是,只有第一页上的ImageView实例化,并且在调用时不返回null。我反复检查以确定每个viewpager页面都引用了正确的xml,并确定每个相应的xml都有适当的ImageView。非常感谢任何帮助。

我的代码

使用viewpager页面。

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>

1 个答案:

答案 0 :(得分:0)

我没有任何确凿的证据,但我的想法是为什么只能找到ViewPager中第一个片段中的观点是因为其他人不“存在”。当然,它们以XML格式存在,但是我不知道在添加Fragments后所有布局都会被夸大和影响,这样如果UI不可见(或根本不可见),您可以直接更新它们。

我也认为您不能使用findViewById来覆盖Fragment托管的ViewPager内的观看次数。 findViewById仅在提供的内容视图中查看 - 即该活动的布局 - 或您提供给它的特定布局 - 例如如果你夸大特定布局,例如列表项的布局,然后在其上调用findViewById以查找内部视图,如TextView。您应该无法在某个findViewById的视图上调用Fragment并进行更改。

至于解决问题。您的视图应该在片段本身内初始化。如果您想根据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。最简单的方法是在片段中声明一个接口,在host活动中实现该接口,并在片段onAttach方法中初始化接口。这样,您可以在片段及其主机活动之间进行回调。

使用这种方法,您几乎可以从包含视图分页器

的活动中控制片段中的所有内容