2013-04-14 111 views
0

對不起,我的英語,但我是意大利人,我不說話很好。 我寫了這個簡單的代碼:NullPointerException與setOnClickListener(安卓)

package com.cris96.rssreader; 


import android.os.Bundle; 
import android.app.Activity; 
import android.view.View; 
import android.widget.Button; 

public class RssReader extends Activity { 
private Button button1; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    button1 = (Button)findViewById(R.id.b1); 
    button1.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      System.out.println("test"); 

     } 
     }); 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.rss_reader); 
    } 


} 

是文件rss_reader.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 





    <Button 
     android:id="@+id/b1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Button" /> 

</LinearLayout> 

而且它是錯誤的:

04-14 18:50:14.305: E/AndroidRuntime(443): FATAL EXCEPTION: main 
04-14 18:50:14.305: E/AndroidRuntime(443): java.lang.RuntimeException: Unable to start    activity ComponentInfo{com.cris96.rssreader/com.cris96.rssreader.RssReader}: java.lang.NullPointerException 
04-14 18:50:14.305: E/AndroidRuntime(443): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
04-14 18:50:14.305: E/AndroidRuntime(443): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
04-14 18:50:14.305: E/AndroidRuntime(443): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
04-14 18:50:14.305: E/AndroidRuntime(443): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
04-14 18:50:14.305: E/AndroidRuntime(443): at android.os.Handler.dispatchMessage(Handler.java:99) 
04-14 18:50:14.305: E/AndroidRuntime(443): at android.os.Looper.loop(Looper.java:123) 
04-14 18:50:14.305: E/AndroidRuntime(443): at android.app.ActivityThread.main(ActivityThread.java:3683) 
04-14 18:50:14.305: E/AndroidRuntime(443): at java.lang.reflect.Method.invokeNative(Native Method) 
04-14 18:50:14.305: E/AndroidRuntime(443): at java.lang.reflect.Method.invoke(Method.java:507) 
04-14 18:50:14.305: E/AndroidRuntime(443): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
04-14 18:50:14.305: E/AndroidRuntime(443): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
04-14 18:50:14.305: E/AndroidRuntime(443): at dalvik.system.NativeStart.main(Native Method) 
04-14 18:50:14.305: E/AndroidRuntime(443): Caused by: java.lang.NullPointerException 
04-14 18:50:14.305: E/AndroidRuntime(443): at com.cris96.rssreader.RssReader.onCreate(RssReader.java:14) 
04-14 18:50:14.305: E/AndroidRuntime(443): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
04-14 18:50:14.305: E/AndroidRuntime(443): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
04-14 18:50:14.305: E/AndroidRuntime(443): ... 11 more 

我得到一個錯誤,當我使用「 setOnClickListener「方法,我不明白爲什麼。我錯在哪裏? 謝謝。

回答

10

在註冊您的OnClickListeners之前,您必須致電setContentView(R.layout.rss_reader);

package com.cris96.rssreader; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.View; 
import android.widget.Button; 

public class RssReader extends Activity { 
    private Button button1; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.rss_reader); 

     button1 = (Button) findViewById(R.id.b1); 
     button1.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       System.out.println("test"); 

      } 
     }); 

    } 

} 
+1

+1爲更快:) –

+0

哈哈謝謝;)。 – Ahmad

3

你需要做的是這樣:

public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.rss_reader); 
button1 = (Button)findViewById(R.id.b1); 
button1.setOnClickListener(new View.OnClickListener() { 

    public void onClick(View v) { 
     System.out.println("test"); 
    } 
    }); 
} 

您不能設置找到視圖之前設置佈局作爲活動的內容。 這就是findViewById返回null的原因。

相關問題