Android 为什么不是';是否正在实例化ImageView?
我是android开发的新手,在ViewPager中实例化ImageView时遇到困难。我的ViewPager有7个页面,所有页面都有ImageView。但是,只有第一页上的ImageView会实例化,并且在调用时不会返回null。我反复检查,以确保每个viewpager页面都引用了正确的xml,并且确保每个对应的xml都具有适当的ImageView。非常感谢您的帮助 我的代码 工作视图页Android 为什么不是';是否正在实例化ImageView?,android,xml,nullpointerexception,imageview,android-viewpager,Android,Xml,Nullpointerexception,Imageview,Android Viewpager,我是android开发的新手,在ViewPager中实例化ImageView时遇到困难。我的ViewPager有7个页面,所有页面都有ImageView。但是,只有第一页上的ImageView会实例化,并且在调用时不会返回null。我反复检查,以确保每个viewpager页面都引用了正确的xml,并且确保每个对应的xml都具有适当的ImageView。非常感谢您的帮助 我的代码 工作视图页 case 0: resId = (R.layout.reference_l
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;
案例0:
resId=(R.layout.reference_layout);
视图=充气机。充气(剩余,空);
herb_ListView=(IndexableListView)view.findViewById(R.id.herbList);
herbsListresource=res.getStringArray(R.array.HerbsList);
herbsItems=newarraylist();
Collections.addAll(herbsItems,herbslestresource);
集合。排序(herbsItems);
ArrayAdapter Herbers\u adapter=新的CustomListAdapter(ReferenceActivity.this,R.layout.list\u item\u Herbers);
对于(int i=0;i
对此我没有任何确凿的证据,但我的想法是,为什么只能在
ViewPager
的第一个片段中找到视图,是因为其他片段并不“存在”当然,它们存在于XML中,但我不知道一旦添加了片段
,所有布局都会膨胀和匹配,以便在UI不可见(或根本不可见)时可以直接更新它们
我也不认为您可以使用findViewById
在ViewPager
findViewById
承载的Fragment
中查看视图,只查看提供的内容视图,即该活动的布局,或您提供给它的特定布局,例如,如果您对特定布局进行充气,例如一个列表项,然后在该列表项上调用findViewById
以查找内部视图,如TextView
。对于某些Fragment
,您不能只在视图上调用findViewById
,然后对其进行更改
至于解决问题。您的视图应该在片段本身中初始化。如果您试图根据片段活动中发生的某些情况更新片段中的视图,那么我将尝试此方法
(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);
}
(三)如果您想知道更新是否完成,或者从片段返回到片段活动,只需使用接口
。最简单的方法是在片段中声明接口,在宿主活动中实现该接口,并在片段onAttach方法中初始化接口。这样您就可以调用回到片段和它的宿主活动之间
使用这种方法,您几乎可以从包含视图寻呼机的活动中控制片段中的所有内容
public updateTextView(String idk){
TextView message = (TextView)getActivity().findViewById(R.id.message);
message.setText(idk);
}