Amrmsmb
Amrmsmb

Reputation: 11406

NPE when declare a layout from a customized view

I have a class which extends View, and inside that class, at a specific point, I want to display text with a small icon beside it.

To achieve this, I created an XML layout as shown below, and inside the class that extends View I define the layout so its contents appear on the screen, but the App crashes with a NullPointerException.

Please let me know how to solve it.

Update_1:

I have initialized the LayoutInflater in onsizeChanged() as follows but when I run the App, nothing appears.

Initialization in onSizechanged:

void  onsizechanged() {                                 
Layoutinflater mlayoutinf = LayoutInflater.from(this.mcontext;  
View mview = mLayoutInf.inflate(R.id.confMSG_Yes,null);
this.tv = (textview) mView.findViewById(R.id.confMSG_Yes) 
}

 ontouchevent(){                                                       
 ....
 ....
 showconfirmation()

 }
 showconfirmation() {                                              

 this.tv.settext("some text");

 }

xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<RelativeLayout 
    android:id="@+id/rL01_ConfMSG"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView 
        android:id="@+id/confMSG_Header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/confMSG_Header"/>

</RelativeLayout>

<LinearLayout 
    android:id="@+id/rL02_ConfMSG"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <TextView 
        android:id="@+id/confMSG_Yes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:drawableRight="@drawable/accept"/>
</LinearLayout>

Java File

private void showConfirmation() {
    // TODO Auto-generated method stubfin 

    TextView tv = (TextView) findViewById(R.id.confMSG_Yes);
    tv.setText("some text");
}

Upvotes: 0

Views: 51

Answers (2)

JstnPwll
JstnPwll

Reputation: 8695

From where are you calling showConfirmation()? It's possible that you're calling it before the layout has been inflated.

When you initialize your view, inflate it from your XML and return a reference to the view:

View view = inflate(getContext(), R.layout.layout_id, null);

Then get a reference to your TextView:

TextView tv = (TextView) view.findViewById(R.id.confMSG_Yes);

Upvotes: 0

gMale
gMale

Reputation: 17895

Sounds like your view isn't inflating this layout. this.findViewById essentially searches through the layout that was inflated for this view.

You may need to add a line like the following to your constructor for the view (note: inflate is a static method inside the View class):

inflate( context, R.layout.view_name_of_your_layout_file, this );

Upvotes: 1

Related Questions