0

我对 Nullpointer 的期望让我很难过。我在 FindViewByID 处收到空指针异常。这是我的一段代码

public class numberActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.number);

}    

ImageView portrait = (ImageView) findViewById(R.id.numberImage);
public void numberClick(View v) {   
    switch(v.getId()){
            case R.drawable.numberone:
                portrait.setImageResource(R.drawable.numbertwo);
                break;

            case R.drawable.numbertwo:
                portrait.setImageResource(R.drawable.numberthree);
                break;

        }

}

}

我的 number.xml 在这里

<ImageView
    android:id="@+id/numberImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/numberone" 
    android:onClick="numberClick" />

我认为非常简单的代码,但自昨晚以来,空指针异常让我感到沮丧。任何帮助表示赞赏。

4

5 回答 5

1

就像其他人指出的那样,ImageView在 onCreate 方法中获取。

您收到空指针异常的原因是您不能initialize在 java 中的任何函数之外创建对象。必须在函数中进行初始化,在这种情况下是 on onCreate()function在or之外声明的任何非静态的东西constructors都不会被初始化。它就在那里。要开始使用它,请在函数中对其进行初始化。

于 2012-10-09T07:31:03.857 回答
0

问题是由这条线引起的。

 ImageView portrait = (ImageView) findViewById(R.id.numberImage);

将“肖像”变量移动到类成员并在 setContentView() 之后在 onCreate() 中对其进行初始化,这将解决您的问题。

或像这样更改您的代码

public class numberActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.number);

} 



  public void numberClick(View v) {   
     switch(v.getId()){
        case R.drawable.numberone:
            ((ImageView)v).setImageResource(R.drawable.numbertwo);
            break;

        case R.drawable.numbertwo:
            ((ImageView)v).setImageResource(R.drawable.numberthree);
            break;

    }

  }
 }
于 2012-10-09T07:21:36.110 回答
0

试试这个

 public class numberActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.number);


ImageView portrait = (ImageView) findViewById(R.id.numberImage);
}    

public void numberClick(View v) {   
    switch(v.getId()){
            case R.drawable.numberone:
                portrait.setImageResource(R.drawable.numbertwo);
                break;

            case R.drawable.numbertwo:
                portrait.setImageResource(R.drawable.numberthree);
                break;

        }

}
}
于 2012-10-09T07:21:58.277 回答
0

把这个

 ImageView portrait = (ImageView) findViewById(R.id.numberImage);

在 onCreate 方法中..

于 2012-10-09T07:22:18.000 回答
0
ImageView portrait = (ImageView) findViewById(R.id.numberImage);

此行必须在 onCreate 方法中。像这样

  public class numberActivity extends Activity {

    ImageView portrait;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.number);
//This element will be found once your contentview is set
        portrait = (ImageView) findViewById(R.id.numberImage);

    }    


    public void numberClick(View v) {   
        switch(v.getId()){
                case R.drawable.numberone:
                    portrait.setImageResource(R.drawable.numbertwo);
                    break;

                case R.drawable.numbertwo:
                    portrait.setImageResource(R.drawable.numberthree);
                    break;

            }

    }
于 2012-10-09T07:23:32.037 回答