Android的:如何使用视图的setTag,getTag和findViewWithTag方法? [英] Android: How to use View's setTag, getTag and findViewWithTag methods?

查看:488
本文介绍了Android的:如何使用视图的setTag,getTag和findViewWithTag方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用了code我的Andr​​oid应用程序的一部分了一些麻烦。我不断收到一个 NullPointerException异常试图设置的ImageView 对象的背景的时候。

这里的code:

 公开查看getView(INT的立场,观点来看,一个ViewGroup父){
    ImageView的ImageView的;
    如果(查看== NULL){
        ImageView的=新ImageView的(mContext);
    } 其他 {
        ImageView的=(ImageView的)观点;
    }
    imageView.setTag(位置);
    返回ImageView的;
}

私人OnItemClickListener itemClickListener =新OnItemClickListener(){
    @覆盖
    公共无效onItemClick(适配器视图<>母公司,视图V,INT位置,长的id){

        ImageView的ImageView的;
        //变量I,在这里,是从一个for循环。
        ImageView的=(ImageView的)v.findViewWithTag(我);
        //我得到一个NullPointerException异常的下一行,Log.d
        Log.d(视图1,imageView.toString());
        //如果我摆脱了Log.d线以上,
        //所述的NullPointerException发生到下一行
        imageView.setBackgroundColor(Color.BLUE);
        ImageView的=(ImageView的)v.findViewWithTag(位置);

        Log.d(查看2,imageView.toString());
        imageView.setBackgroundColor(Color.BLUE);

    };
}
 

我怀疑的问题与我的code是因为什么参数,我通过了 setTag()方法,什么参数,我通过了<$的C $ C> findViewWithTag()方法。如果有人能告诉我如何设置标签的例子,并找到标记的意见,我将不胜AP preciate吧。

感谢您的时间。

编辑:这是错误日志。我不知道在哪里把它,所以我把它放在这里。

  05-04 21:47:24.314:ERROR / AndroidRuntime(335):致命异常:主要
05-04 21:47:24.314:ERROR / AndroidRuntime(335):显示java.lang.NullPointerException
05-04 21:47:24.314:ERROR / AndroidRuntime(335):在com.jacksmartie.PhotoMem.MainActivity $ 1.onItemClick(MainActivity.java:79)
05-04 21:47:24.314:ERROR / AndroidRuntime(335):在android.widget.AdapterView.performItemClick(AdapterView.java:284)
05-04 21:47:24.314:ERROR / AndroidRuntime(335):在android.widget.AbsListView $ PerformClick.run(AbsListView.java:1696)
05-04 21:47:24.314:ERROR / AndroidRuntime(335):在android.os.Handler.handleCallback(Handler.java:587)
05-04 21:47:24.314:ERROR / AndroidRuntime(335):在android.os.Handler.dispatchMessage(Handler.java:92)
05-04 21:47:24.314:ERROR / AndroidRuntime(335):在android.os.Looper.loop(Looper.java:123)
05-04 21:47:24.314:ERROR / AndroidRuntime(335):在android.app.ActivityThread.main(ActivityThread.java:4627)
05-04 21:47:24.314:ERROR / AndroidRuntime(335):在java.lang.reflect.Method.invokeNative(本机方法)
05-04 21:47:24.314:ERROR / AndroidRuntime(335):在java.lang.reflect.Method.invoke(Method.java:521)
05-04 21:47:24.314:ERROR / AndroidRuntime(335):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
05-04 21:47:24.314:ERROR / AndroidRuntime(335):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-04 21:47:24.314:ERROR / AndroidRuntime(335):在dalvik.system.NativeStart.main(本机方法)`
 

解决方案

这行是你的罪魁祸首的一部分:

  com.jacksmartie.PhotoMem.MainActivity $ 1.onItemClick(MainActivity.java:79)
 

将断点位置:

  Log.d(视图1,imageView.toString());
 

再看看你的的ImageView 引用对象[ImageView的]是的,我很期待,这是空,因为你不将它链接正确。

如果为null,这意味着你的链接到参考的观点是不正确的。如果是这样的话,那么你需要将它正确地分配如下所示:

 按钮B = findViewById(R.id.Button01);
 

不过,因为你使用的是什么似乎是一个的ListView ,那拉略有不同。这意味着,你拉视图的方式是错误的,做了一些研究,应该找一些事来帮助清除了!

I'm having some trouble with a section of code for my android app. I keep getting a NullPointerException when trying to set the background of an ImageView object.

Here's the code:

public View getView(int position, View view, ViewGroup parent) {
    ImageView imageView;
    if (view == null) {
        imageView = new ImageView(mContext);
    } else {
        imageView = (ImageView) view;
    }
    imageView.setTag(position);
    return imageView;
}

private OnItemClickListener itemClickListener = new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

        ImageView imageView;
        //Variable i, here, is from a for loop.
        imageView = (ImageView)v.findViewWithTag(i);
        //I get a NullPointerException at the next line, "Log.d"
        Log.d("View 1", imageView.toString());
        //If I get rid of the "Log.d" line above, 
        //the NullPointerException occurs on the next line
        imageView.setBackgroundColor(Color.BLUE);
        imageView = (ImageView)v.findViewWithTag(position);

        Log.d("View 2", imageView.toString());
        imageView.setBackgroundColor(Color.BLUE);

    };
}

I suspect the problem with my code is because of what parameter I'm passing the setTag() method and what parameter I'm passing the findViewWithTag() method. If someone could show me an example of how to set tags and find views with tags, I would greatly appreciate it.

Thank you for your time.

Edit: Here's the error log. I'm not sure where to put it, so I will put it here.

05-04 21:47:24.314: ERROR/AndroidRuntime(335): FATAL EXCEPTION: main
05-04 21:47:24.314: ERROR/AndroidRuntime(335): java.lang.NullPointerException
05-04 21:47:24.314: ERROR/AndroidRuntime(335):     at com.jacksmartie.PhotoMem.MainActivity$1.onItemClick(MainActivity.java:79)
05-04 21:47:24.314: ERROR/AndroidRuntime(335):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
05-04 21:47:24.314: ERROR/AndroidRuntime(335):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
05-04 21:47:24.314: ERROR/AndroidRuntime(335):     at android.os.Handler.handleCallback(Handler.java:587)
05-04 21:47:24.314: ERROR/AndroidRuntime(335):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-04 21:47:24.314: ERROR/AndroidRuntime(335):     at android.os.Looper.loop(Looper.java:123)
05-04 21:47:24.314: ERROR/AndroidRuntime(335):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-04 21:47:24.314: ERROR/AndroidRuntime(335):     at java.lang.reflect.Method.invokeNative(Native Method)
05-04 21:47:24.314: ERROR/AndroidRuntime(335):     at java.lang.reflect.Method.invoke(Method.java:521)
05-04 21:47:24.314: ERROR/AndroidRuntime(335):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-04 21:47:24.314: ERROR/AndroidRuntime(335):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-04 21:47:24.314: ERROR/AndroidRuntime(335):     at dalvik.system.NativeStart.main(Native Method)`

解决方案

This line is part of your culprit:

com.jacksmartie.PhotoMem.MainActivity$1.onItemClick(MainActivity.java:79)

Put a breakpoint here:

Log.d("View 1", imageView.toString());

And look what your imageview reference object [imageView] is, I'm expecting that it is null because you aren't linking it properly.

If it is null, this means that your link to the reference view is not right. If that's the case, then you need to assign it properly like so:

Button b = findViewById(R.id.Button01);

However; since you're using what seems to be a ListView, the pulling of that is slightly different. This means that the way you're pulling the view is wrong, do some research, should find something to help clear that up!

这篇关于Android的:如何使用视图的setTag,getTag和findViewWithTag方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆