我是 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>