Jimbo
Jimbo

Reputation: 1

Cannot add a TextView programmatically

I am trying to add a textview and keep getting a nullPointer when I try to add it to the layout.

My layout:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/app_header" android:layout_width="match_parent"
    android:layout_height="@dimen/headerSize" android:background="@color/iPhoneBackground">

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/grey"
        android:layout_alignParentBottom="true" />

</RelativeLayout>

My class:

public class AppHeader extends RelativeLayout {
    Context mContext;
    screenName sName;
    RelativeLayout parent;


    public AppHeader(Context context, screenName sn) {
        super(context);
        mContext = context;
        sName = sn;
        parent = (RelativeLayout) findViewById(R.id.app_header);
        init();
    }

    public AppHeader(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        mContext = context;
        init();
    }
    public AppHeader(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    public void init(){
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        TextView title = new TextView(mContext);
        switch (sName) {
            case FAVORITER:
                title.setText(mContext.getString(R.string.favourite));
                break;
            case SOKRESA:
                title.setText(mContext.getString(R.string.search));
                break;
            case REALTID:
                // tod
                break;
            case STORNINGAR:
                // tod
                break;
            case MER:
                // tod
                break;
        }

        params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
        params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
        title.setLayoutParams(params);
        parent.addView(title);
    }

}

Logcat gives me a nullPointer on the last line "parent.addView(title);"

Does anyone know what I am doing wrong?

Here is logcat:

java.lang.RuntimeException: Unable to start activity ComponentInfo{mtr.se.reskoll/mtr.se.reskoll.SokResa}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5041)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at mtr.se.reskoll.AppHeader.init(AppHeader.java:63)
            at mtr.se.reskoll.AppHeader.<init>(AppHeader.java:24)
            at mtr.se.reskoll.SokResa.setHeader(SokResa.java:50)
            at mtr.se.reskoll.SokResa.onCreate(SokResa.java:37)
            at android.app.Activity.performCreate(Activity.java:5104)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5041)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)

Upvotes: 0

Views: 104

Answers (3)

A B
A B

Reputation: 338

public AppHeader(Context context) {
        super(context);
        mContext = context;

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        parent  = (MobileAndrRelativeLayout) inflater.inflate(R.layout.yourlayout, this);

        init();
    }

    public AppHeader(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        mContext = context;

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        parent  = (MobileAndrRelativeLayout) inflater.inflate(R.layout.yourlayout, this);

        init();
    }
    public AppHeader(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);    
        parent  = (MobileAndrRelativeLayout) inflater.inflate(R.layout.yourlayout, this);

        init();
    }

EDIT: For the text to appear, it depends on which constructor you are using, as you are only setting the text in the first constructor.

I would advise a different approach: (I also modified the first constructor)

public void SetTitle(sName)
{
      switch (sName) {
            case FAVORITER:
                title.setText(mContext.getString(R.string.favourite));
                break;
            case SOKRESA:
                title.setText(mContext.getString(R.string.search));
                break;
            case REALTID:
                // tod
                break;
            case STORNINGAR:
                // tod
                break;
            case MER:
                // tod
                break;
        }
}

public void init(){
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        TextView title = new TextView(mContext);

        // we will add this line for test, to see in the problem is that the text is not displayed or the textview itself is not visible
        title.setBackgroundColor(Color.YELLOW); 

        params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
        params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
        title.setLayoutParams(params);
        parent.addView(title);
    }

So after you create your custom view:

AppHeader header= new AppHeader (context);
header.SetTitle("Test");

Also, just to make sure the switch is working correctly (aka u are sending a screen name that is actually containted in the switch options), try a simple test method instead of the above one:

public void SetTitle(sName)
{
  title.setText("Test Title");
}

Upvotes: 0

Manos
Manos

Reputation: 1501

Try this:

Activity activity = (Activity) mContext;
parent = (RelativeLayout) activity.findViewById(R.id.app_header);

Upvotes: 0

Bubu
Bubu

Reputation: 1543

Your "parent" variable is initialized only in your constructor public AppHeader(Context context, screenName sn). If you use other constructor, parent will be null.

The line parent = (RelativeLayout) findViewById(R.id.app_header); must be moved in your init() method.

Upvotes: 1

Related Questions