开发者

Returning values from custom view

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开发者_JAVA技巧:

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.


Posting your ShowWeight class will help us more. Assuming that you have class like this.

public class ShowWeight extends LinearLayout {

    private Object myObject; 
    public ShowWeight(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.header, this);
    }
    public Object getMyObject()
    {
        return myObject;
    }    
}

and in you MainActivity.java

ShowWeight sw=(ShowWeight)this.findViewById(R.id.sw);
sw.getMyObject();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜