0

我已将其设置为允许我从在线文件中更改 TextView 的可见性,该文件会变成字符串,除了添加允许我更改 TextView 可见性的代码部分之外,所有这些都可以正常工作. 我不明白为什么这不起作用。这是一个日志猫:

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)));

如果有人可以帮助我解决我的问题,我会很高兴。谢谢阅读。如果您需要任何其他文件,请不要犹豫。

修复:我弄乱了字符串名称,所以这是一个非常愚蠢的错误。

4

1 回答 1

0

在第 68 行,map不会为空,因此我认为在您获得 from (在 index 处)song后可能为空。dataposition

只需添加一些日志代码来调试此问题:

HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);

Log.i("test", "index is: " + position);
Log.i("test", "song: " + song);

然后转到logcat,你可能会发现问题。

如果没有,请在此处使用输出进行评论。

更新1

地图和歌曲不为空。此值可能为空:

map.get(song.get(CustomizedListView.KEY_DATE2VIS));

更新2

如果它为空,您可以分配一个默认值:

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));
于 2013-07-21T18:20:13.790 回答