2013-08-05 58 views
1

我從書中複製了以下代碼。當我運行該應用程序時,它無法啓動。android onClickListener(新手)

違規行是aboutButton.setOnClickListener(this);。如果我把它敲出來,應用程序的作品。

任何線索?

謝謝。

G.

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    View aboutButton = findViewById(R.id.main_about_button); 
    aboutButton.setOnClickListener(this); 
    setContentView(R.layout.activity_main); 
} 


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:gravity="center" 
    tools:context=".MainMenu" 
    > 

    <TextView 
     android:id="@+id/welcome_title" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:textStyle="bold" 
     android:textSize="25sp" 
     android:text="@string/welcome_title" 
     /> 

    <Button 
     android:id="@+id/search_button" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/search_button" 
     /> 

    <Button 
     android:id="@+id/new_button" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/new_button" 
     /> 

    <Button 
     android:id="@+id/help_button" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/help_button" 
     /> 

    <Button 
     android:id="@+id/exit_button" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/exit_button" 
     /> 

    <Button 
     android:id="@+id/main_about_button" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/exit_button" 
     android:layout_alignLeft="@+id/exit_button" 
     android:text="@string/main_about_button" 
     android:clickable="true" 
     /> 

</LinearLayout> 

回答

2

aboutButtonnull。只有在充值layout之後,才能初始化它。將其更改爲

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); // Switch these two lines 
    View aboutButton = findViewById(R.id.main_about_button); 
    aboutButton.setOnClickListener(this); 

} 

Viewslayout中存在,這意味着他們將返回null如果你試圖誇大你的layout,使用layout inflater或之前初始化它們通過調用setContentView(),試圖在這將給你一個NPE調用它們上的函數或設置一個listener

0

我建議您將aboutButton聲明爲Button。試試這個:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Button aboutButton = (Button) findViewById(R.id.main_about_button); 
    aboutButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      // Do something 
     } 
    }); 

} 

我希望它有幫助!

+0

我確實嘗試用Button更改聲明。這並沒有解決它。謝謝。 –

1

不得不把聽衆陳述AFTER seContentView命令。修復它。

我的猜測是,setContentView從佈局導入所有變量& ID。而findViewById只是返回一個空指針或其他東西。

如果有人可以證實我的猜測,我會很感激。

+0

你甚至看過我的回答嗎?它在發佈此消息之前一小時確認了你的猜測。 – codeMagic

+0

對不起。我猜測後我只得到你的答案。 –

+0

這很好。我很高興你明白了。你仍然可以接受或自己接受,以便將來可以幫助別人 – codeMagic