2011-10-08 77 views
0

我在android 2.2上使用了這段代碼,但是在運行這段代碼時,我得到的錯誤是「The application expliintent(process .com.nakool)停止了意外,請再次嘗試..強制關閉。應用程序expliintent(process.com.nakool)已經意外停止,請再試一次..強制關閉

當我評論語句「b.setOnClickListener(this);」。代碼處理成功..不知道爲什麼我得到錯誤在這個line..pls指導我

enter code here

public void onCreate(Bundle savedInstanceState) { 


    Button b=(Button)findViewById(R.id.button1); 
    b.setOnClickListener(this); 

    i=new Intent(this,activitynew.class); 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
} 
public void onClick(View v) 
{}} 

回答

0

看着你的代碼,它看起來像你正在試圖找到ViewById,然後通過調用setContentView(R.layout.main)來設置此活動的內容。這意味着findViewById返回null,並且您的應用程序由於nullPointerException(b爲null ...)而變爲colured。

確保您在嘗試任何findViewById之前調用setContentView。

+0

非常感謝您糾正我。現在它成功運行 – Nakool

相關問題