1

I have created a custom view name StepView . Here is the code for StepView.

public class StepView extends LinearLayout {

    private Context cont;
    private LinearLayout stepHolder;

    public StepView(Context context, AttributeSet attrs) {
        super(context);
        // TODO Auto-generated constructor stub
        LayoutInflater.from(context).inflate(R.layout.stepview, this, true);
        stepHolder = (LinearLayout) findViewById(R.id.stepHolder);

        cont = context;
    }

    public void addStep(String title, int drawableId, View.OnClickListener stepAction){

        View step = LayoutInflater.from(cont).inflate(R.layout.step, this, true);
        TextView txtText = (TextView) step.findViewById(R.id.txtText);
        ImageView imgStep = (ImageView) step.findViewById(R.id.imgStep);

        txtText.setText(title);
        imgStep.setImageResource(drawableId);

        stepHolder.addView(step);
    }


}

It is the code for stepview.xml.

<?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" >
    <LinearLayout
        android:id="@+id/stepHolder"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

    </LinearLayout>

</LinearLayout>

step.xml

<?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" >

    <ImageView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imgStep"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/txtText">

    </TextView>

</LinearLayout>

Now I used the XML in main layout.

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" >
 ..................
     </LinearLayout>

    <com.orthokeys.view.StepView
        android:id="@+id/stepMenu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </com.orthokeys.view.StepView>

</merge>

I have cheched that it is appearing properly. But when I add the following code it is giving NullPointerException.

StepView stepMenu = (StepView) findViewById(R.id.stepMenu);
stepMenu.addStep("Picture", R.drawable.step01, null);

I have checked that stepMenu is giving null.

Whatis the problem in my code?

7
  • What is your step.xml layout? I want say from what you've given that it can't find "txtText" TextView and/or "imgStep" ImageView because it's not inflating properly. Commented Oct 16, 2012 at 13:37
  • I see nothing obviously wrong; can you try to clean and rebuild your project (or throw away your generated R class)? Commented Oct 16, 2012 at 13:48
  • "I have checked that stepMenu is giving null." Does that mean it is null, or you checked it and it's not null? Commented Oct 16, 2012 at 13:59
  • if(stepMenu==null) Log.d("stepMenu", "stepMenu is null"); Commented Oct 16, 2012 at 14:01
  • I am getting that it is null. Commented Oct 16, 2012 at 14:01

2 Answers 2

3

I solved the prob...change the code in constructor of StepView Class

public StepView(Context context, AttributeSet attrs) {
  super(context, attrs);
  ....................
}
Sign up to request clarification or add additional context in comments.

Comments

0

You are not showing the full code block where you get the error, but it looks like you may have forgotten to use setContentView(R.layout.main_layout); to set the layout in use.

2 Comments

No, I have called the setContentView(R.layout.main_layout); properly, not only that, I can access other Views in the layout and work with them.
Add the stack trace to see what methods were called when the null point occured.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.