2012-04-24 83 views
0

我是新來的android和我現在只開始。對於我當我試圖實現點擊應用程序關閉的按鈕監聽器。可以幫助任何人。強制關閉android中的錯誤

以下是代碼:

public class Sampleprojectsubbu1Activity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main);   
     final Button b1=(Button)findViewById(android.R.id.button1); 
     b1.setOnClickListener(new View.OnClickListener() 
     {  
     @Override 
     public void onClick(View v) 
     {   

      // TODO Auto-generated method stub 

     } 
    }); 
    } 
} 
+1

你能發佈logcat錯誤? – 2012-04-24 05:49:15

+0

更改R.id.button1而不是android.R.id.button1。 – 2012-04-24 05:50:10

+0

接受任何人對你滿意的答案。 – Praveenkumar 2012-04-24 06:51:39

回答

1

可能是在您遇到的錯誤這條線 -

final Button b1=(Button)findViewById(android.R.id.button1); 

在上述行android.R.id.button1將通過將與Android罐子來默認按鈕提供。

而不是在那裏,你必須使用按鈕ID。你在main.xml文件中使用了什麼。就像如果您在使用的buttonconfirm手段ID在你main.xml一個按鈕,你應該使用如下 -

final Button b1=(Button)findViewById(R.id.buttonconfirm); 

希望這有助於你。並且,請在您的問題中發佈您的main.xml

1

請更改此行。

final Button b1=(Button)findViewById(android.R.id.button1); 

final Button b1=(Button)findViewById(R.id.button1); 
+0

由於all.Now其工作.. – subburaj 2012-04-24 05:54:40

+0

您好我有一個問題,敬酒不apperingpublic無效的onClick(視圖v) \t \t {\t \t \t \t的EditText的text1 =(EditText上)findViewById(R.id.editText1 ); \t \t \t \t \t \t \t \t // TODO自動生成方法存根 \t \t \t如果(text1.getText()。等於( 「subbu」)) \t \t \t { \t \t \t \t Toast.makeText (Sampleprojectsubbu1Activity.this,「Hello」+ text1.getText(),Toast.LENGTH_LONG).show(); \t \t \t} \t \t \t \t } \t \t }); – subburaj 2012-04-24 06:12:00

+0

你必須通過像addTextChangedListener這樣的事件來處理這個事件。在這個實現'onTextChanged'方法。 – GAMA 2012-04-24 06:14:56

2

您正在使用的ID 「android.R.id.button1」,這是Android的一部分,你應該使用類似R.id.your_button_id