Android的NullPointerException异常的自定义视图访问 [英] Android NullPointerException in custom view access

查看:438
本文介绍了Android的NullPointerException异常的自定义视图访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义视图名称 StepView 。这里是code为StepView。

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);
    }


}

这是code为stepview.xml。

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

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>

现在我使用的XML的主要布局。

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>

我已经cheched它是否正确显示。但是,当我添加以下code这是给NullPointerException异常。

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);

我已签了 stepMenu 是给空。

头朝下在我的code中的问题?

Whatis the problem in my code?

推荐答案

我解决的概率...修改code在StepView类的构造函数

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

public StepView(Context context, AttributeSet attrs) {
  super(context, attrs);
  ....................
}

这篇关于Android的NullPointerException异常的自定义视图访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆