2012-10-09 74 views
0

我的Nullpointer預期讓我難堪。我在FindViewByID獲得空指針異常。這裏是我的一段代碼Android:空指針問題

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

非常簡單的代碼,我認爲,但空指針異常是令人沮喪的我從昨晚起。任何幫助表示讚賞。

+1

嘗試清理並構建項目。 –

+0

感謝您的所有意見。它在我將Im​​ageView移動到OnCreate方法後工作。 – Jasma

回答

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; 

      } 

    } 
0

問題由此行引起。

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

移動「畫像」變量類成員和的onCreate()的setContentView後intialize它(),這將解決您的問題。

或更改這樣的代碼

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; 

    } 

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

     } 

} 
} 
0

把這個

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

在onCreate方法..

1

像其他人指出的那樣,獲得在onCreate方法ImageView

你得到一個空指針異常的原因是你不能在initialize之外的對象在java中的任何函數。初始化必須在一個函數中進行,在這種情況下,在onCreate()。 在functionconstructors之外聲明的任何非靜態都不會被初始化。它就在那裏。要開始使用它,請在函數中初始化它。