自定义网格视图

时间:2015-02-13 17:45:35

标签: java android eclipse gridview

我有一个自定义网格视图适配器,获得2个参数。第一个为项目名称,第二个为img url。并使用Picasso库加载图像。但是当我运行它并发送参数时它不起作用。 谢谢你的帮助 ; 记录:

    02-13 17:40:14.964: E/AndroidRuntime(1342): FATAL EXCEPTION: main
02-13 17:40:14.964: E/AndroidRuntime(1342): Process: com.plusnet.tashrifat, PID: 1342
02-13 17:40:14.964: E/AndroidRuntime(1342): java.lang.NullPointerException: uriString
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.net.Uri$StringUri.<init>(Uri.java:467)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.net.Uri$StringUri.<init>(Uri.java:457)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.net.Uri.parse(Uri.java:429)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at plusnet.tashrifat.adapter.CustomGrid.getView(CustomGrid.java:55)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.widget.AbsListView.obtainView(AbsListView.java:2255)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.widget.GridView.makeAndAddView(GridView.java:1345)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.widget.GridView.makeRow(GridView.java:345)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.widget.GridView.fillDown(GridView.java:287)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.widget.GridView.fillFromTop(GridView.java:421)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.widget.GridView.layoutChildren(GridView.java:1233)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.widget.AbsListView.onLayout(AbsListView.java:2087)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.view.View.layout(View.java:14817)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.view.View.layout(View.java:14817)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.view.View.layout(View.java:14817)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:678)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.view.View.layout(View.java:14817)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.view.View.layout(View.java:14817)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:374)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.view.View.layout(View.java:14817)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.view.View.layout(View.java:14817)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1983)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1740)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.view.Choreographer.doFrame(Choreographer.java:544)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.os.Handler.handleCallback(Handler.java:733)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.os.Handler.dispatchMessage(Handler.java:95)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.os.Looper.loop(Looper.java:136)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at android.app.ActivityThread.main(ActivityThread.java:5001)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at java.lang.reflect.Method.invokeNative(Native Method)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at java.lang.reflect.Method.invoke(Method.java:515)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
02-13 17:40:14.964: E/AndroidRuntime(1342):     at dalvik.system.NativeStart.main(Native Method)

自定义适配器类:

public class CustomGrid extends BaseAdapter {
private Context mContext;
private final String[] name;
private final String[] Imageid;

public CustomGrid(Context c, String[] web, String[] Imageid) {
    mContext = c;
    this.Imageid = Imageid;
    this.name = web;
}

public int getCount() {
    // TODO Auto-generated method stub
    return name.length;
}

public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View grid;
    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        grid = new View(mContext);
        grid = inflater.inflate(R.layout.grid_single, null);
        TextView textView = (TextView) grid.findViewById(R.id.grid_text);
        ImageView imageView = (ImageView) grid
                .findViewById(R.id.grid_image);
        textView.setText(name[position]);
        Picasso.with(mContext).load(Uri.parse(Imageid[position]))
                .error(R.drawable.ic_launcher).into(imageView);
        Log.d("picasso", Imageid[position]);
    } else {
        grid = (View) convertView;
    }
    return grid;
}

}

2 个答案:

答案 0 :(得分:1)

你的跟踪说&#34; java.lang.NullPointerException:uriString&#34;,所以检查你是否正在使用Log或甚至Toast正确地提供数据。

Appart表示,你的getView实现是错误的,因为你没有正确地更新视图:你必须每次都设置值,而不仅仅是在你正在创建视图的时候。

这是您的固定代码:

public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View grid;
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        grid = new View(mContext);
        grid = inflater.inflate(R.layout.grid_single, null);
    } else {
        grid = (View) convertView;
    }

    // Refresh view
    TextView textView = (TextView) grid.findViewById(R.id.grid_text);
    ImageView imageView = (ImageView) grid.findViewById(R.id.grid_image);
    textView.setText(name[position]);
    Picasso.with(mContext).load(Uri.parse(Imageid[position])).error(R.drawable.ic_launcher).into(imageView);
    Log.d("picasso", Imageid[position]);

    return grid;
}

---编辑---------------------

检查打印的值:

Log.d("picasso", Imageid[position]);

如果不为null,则检查URI解析是否正常工作:

try {
    Uri uri = Uri.parse(Imageid[position]);
    Log.d("picasso", uri.toString());
} catch (Exception ex) {
    ex.printStackTrace();
}

还要考虑在列表适配器中实现视图持有者模式,以提高性能并获得更平滑的响应。

答案 1 :(得分:1)

Uri.parse(Imageid [position])抛出空指针异常,检查Imageid [position]处的数据,确保它在尝试解析之前不为null。