2012-02-01 52 views
0

我剛開始學習Android,並在第一個示例中出現以下錯誤,請幫助我。E/AndroidRuntime(543):java.lang.RuntimeException

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

     final Button buttonNesnesi=(Button) findViewById(id.button1); 

     buttonNesnesi.setOnClickListener(
      new OnClickListener() 
      { 

      public void onClick(View v) 
      { 
       Toast.makeText(SecondActivity.this, "Hello!!!",1000).show(); 
      } 
     } 
     ); 
    } 
} 

例外:

E/AndroidRuntime(543):了java.lang.RuntimeException:無法啓動 活性 ComponentInfo {net.developersland/net.developersland.SecondActivity}: 的java。 lang.NullPointerException

,這是我的main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/hello" /> 




    <Button 
     android:id="@+id/button1" 
     android:layout_width="231dp" 
     android:layout_height="wrap_content" 
     android:text="@string/hello" /> 

</LinearLayout> 

回答

2

這是應該的,

final Button buttonNesnesi=(Button) findViewById(R.id.button1); 

不是

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

由於這是工作! 但是,當我犯這個錯誤時,編譯器爲什麼不給出任何警告或錯誤? – 2012-02-01 12:05:37

+0

我想如果你清理那個時間,那麼它顯示錯誤。 – user370305 2012-02-01 12:07:42

1

你缺少[R .id.button1

final Button buttonNesnesi=(Button) findViewById(R.id.button1); 
相關問題