从自定义视图返回值
我创建了一个类 ShowWeight
扩展 LinearLayout
,它有两个自定义视图作为内部类。我通过 main.xml 中的 XML 标记来使用此类:
<org.test.ShowWeight android:id="@+id/sw"..................../>
ShowWeight 类中有两个公共变量,需要在使用 main.xml
作为其视图。
我该怎么做? 我在主要活动中尝试了这个:
ShowWeight sw=(ShowWeight)this.findViewById(R.id.sw);
Log.d("test",sw.getContentDescription().toString());
在 showWeight 类中尝试了这个:
this.setContentDescription(/*required value */);
这导致了 NullPointerException。
希望得到一些建议(数据库、静态变量,不是选项)
更新:
不幸的是,我不允许发布任何代码,如果我看起来含糊不清,我深表歉意,但我确信 ShowWeight
类没有改变任何可能导致问题的内容。
我通过 XML 标记添加到 main.xml 视图中的 ShowWeight 类显示良好且功能正常。
当我在主活动中使用 ShowWeight sw=(ShowWeight)this.findViewById(R.id.sw);
然后 Toast 或打印 ShowWeight 我得到 'null' 。另外,setContentDescription(),getContentDescription()
不应引发错误,因为我已在 ShowWeight 的 XML 标记中给出了默认的 contentDescription。
I have made a class ShowWeight
extending LinearLayout
, which has two custom Views as Inner classes. I am using this class by means of an XML tag in the main.xml :
<org.test.ShowWeight android:id="@+id/sw"..................../>
There are two public variables in the ShowWeight class, whose changing values need to be captured in the main activity, which uses main.xml
as its view.
How do I do this?
I tried this in the main activity :
ShowWeight sw=(ShowWeight)this.findViewById(R.id.sw);
Log.d("test",sw.getContentDescription().toString());
and this in the showWeight class:
this.setContentDescription(/*required value */);
This resulted in a NullPointerException
.
Would appreciate some suggestions (Database, static variables, not an option)
Update:
Unfortunately, I am not permitted to post any of the code, I apologize if I seem vague, nevertheless I'm sure the ShowWeight
class hasn't altered anything that might be causing the problem.
The ShowWeight class, which I have added to the main.xml view by means of an XML tag appears fine and functions properly.
When I use ShowWeight sw=(ShowWeight)this.findViewById(R.id.sw);
in the Main Activity and then Toast or print ShowWeight I am getting 'null' . Also the setContentDescription(),getContentDescription()
shouldn't throw errors because I've given a default contentDescription in the XML tag for ShowWeight.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发布您的 ShowWeight 课程将为我们提供更多帮助。
假设你有这样的课程。
在你的 MainActivity.java 中
Posting your ShowWeight class will help us more.
Assuming that you have class like this.
and in you MainActivity.java