我已将此设置为允许我从在线文件更改TextView的可见性,该文件变为字符串并且除了添加允许我更改可见性的代码部分之外,所有这些都可以正常工作一个TextView。我不明白为什么这不起作用。这是一个logcat:
07-21 19:00:46.451 21334-21334/com.example.app E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.app.LazyAdapter.getView(LazyAdapter.java:68)
at android.widget.AbsListView.obtainView(AbsListView.java:2035)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
at android.widget.ListView.onMeasure(ListView.java:1155)
at android.view.View.measure(View.java:12723)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
at android.view.View.measure(View.java:12723)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at android.view.View.measure(View.java:12723)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1447)
at android.view.View.measure(View.java:12723)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at android.view.View.measure(View.java:12723)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
at android.view.View.measure(View.java:12723)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)
at android.view.View.measure(View.java:12723)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4575)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
at dalvik.system.NativeStart.main(Native Method)
以下是我的适配器的代码,其中包含错误:
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row, null);
TextView title = (TextView)vi.findViewById(R.id.title); // title
TextView date1 = (TextView)vi.findViewById(R.id.date1); // Release Date 1 (Usually US)
TextView date2 = (TextView)vi.findViewById(R.id.date2); // Release Date 2 (Usually Europe)
TextView date3 = (TextView)vi.findViewById(R.id.date3); // Release Date 3 (Usually Australia)
TextView platforms = (TextView)vi.findViewById(R.id.platforms); // duration
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);
//This allows the Visibility of certain TextViews to be set via a string set by the online XML
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("visible", View.VISIBLE);
map.put("gone", View.GONE);
map.put("invisible", View.INVISIBLE);
// Setting all values in listview
title.setText(song.get(CustomizedListView.KEY_TITLE));
date1.setText(song.get(CustomizedListView.KEY_DATE1));
date2.setText(song.get(CustomizedListView.KEY_DATE2));
date3.setText(song.get(CustomizedListView.KEY_DATE3));
date2.setVisibility(map.get(song.get(CustomizedListView.KEY_DATE2VIS))); //This sets visibility via the KEY_DATE2VIS String using one of the terms in the Hashmap above
date3.setVisibility(map.get(song.get(CustomizedListView.KEY_DATE3VIS)));
platforms.setText(song.get(CustomizedListView.KEY_PLATFORMS));
imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image);
return vi;
}
}
LazyAdapter.class的第68行是:
date2.setVisibility(map.get(song.get(CustomizedListView.KEY_DATE2VIS)));
如果有人可以帮我解决问题,我会非常高兴。谢谢阅读。如果您需要任何其他文件,请不要犹豫。
FIX:我弄乱了字符串名称,所以这是一个非常愚蠢的错误。
答案 0 :(得分:0)
在第68行,map
不会为空,因此我认为从song
(在索引data
处)后position
可能为空。< / p>
只需添加一些日志代码即可调试此问题:
HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);
Log.i("test", "index is: " + position);
Log.i("test", "song: " + song);
然后转向logcat,你可能会发现问题。
如果没有,请在此处注明输出。
<强> UPDATE1 强>
地图和歌曲不为空。此值可能为null:
map.get(song.get(CustomizedListView.KEY_DATE2VIS));
<强> UPDATE2 强>
如果为null,则可以指定默认值:
int visibility = map.get(song.get(CustomizedListView.KEY_DATE2VIS));
if (null == visibility)
visibility = View.VISIBLE;
您可以添加一些日志记录以找出问题所在:
Log.i("test", "song :" + song);
Log.i("test", song.get(CustomizedListView.KEY_DATE2VIS));