2013-12-17 84 views
0

我在我的應用程序中使用AndroidAnnotations,並且當我試圖把額外的意圖,並啓動一個活動,其中我使用AndroidAnnotations,我總是得到一個運行時錯誤。當我停止使用AndroidAnnotations時,一切正常。運行時錯誤與AndroidAnnotations

下面是一個代碼,開始我的活動:

public class TimeAndDateShower extends Activity { 
//some code 

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.time_and_date_shower); 
     //some code 
     setButtonListener(); 

    } 
public void setButtonListener() 
    { 
     Button button = (Button) findViewById(R.id.button1); 
     button.setOnClickListener(new OnClickListener() 
     { 
      public void onClick(View v) 
      { 
       Intent intent = new Intent(TimeAndDateShower.this, DateChooser_.class); 
       intent.putExtra("SSID", network); 
       startActivity(intent); 
       TimeAndDateShower.this.finish(); 
      } 
     }); 
    } 
} 

這裏是DateChooser.java的樣子:

@EActivity(R.layout.date_chooser) 
public class DateChooser extends Activity { 

    public String network; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     //setContentView(R.layout.date_chooser); 

     setNetworkName(); 
     //some code 
    } 

    public void setNetworkName() 
    { 
     TextView textView = (TextView)findViewById(R.id.textView4); 
     network = this.getIntent().getStringExtra("SSID");//using an extra 
     textView.setText(network); 
    } 

} 

在AndroidManifest.xml我宣佈DateChooser活動是這樣的:

name="com.componentix.imwizard.DateChooser_" android:screenOrientation="landscape"> </activity> 

這裏是我的運行時錯誤的日誌:

12-17 11:51:32.267: E/AndroidRuntime(3234): FATAL EXCEPTION: main 
12-17 11:51:32.267: E/AndroidRuntime(3234): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.componentix.imwizard/com.componentix.imwizard.DateChooser_}: java.lang.NullPointerException 
12-17 11:51:32.267: E/AndroidRuntime(3234):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 
12-17 11:51:32.267: E/AndroidRuntime(3234):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
12-17 11:51:32.267: E/AndroidRuntime(3234):  at android.app.ActivityThread.access$600(ActivityThread.java:130) 
12-17 11:51:32.267: E/AndroidRuntime(3234):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
12-17 11:51:32.267: E/AndroidRuntime(3234):  at android.os.Handler.dispatchMessage(Handler.java:99) 
12-17 11:51:32.267: E/AndroidRuntime(3234):  at android.os.Looper.loop(Looper.java:137) 
12-17 11:51:32.267: E/AndroidRuntime(3234):  at android.app.ActivityThread.main(ActivityThread.java:4745) 
12-17 11:51:32.267: E/AndroidRuntime(3234):  at java.lang.reflect.Method.invokeNative(Native Method) 
12-17 11:51:32.267: E/AndroidRuntime(3234):  at java.lang.reflect.Method.invoke(Method.java:511) 
12-17 11:51:32.267: E/AndroidRuntime(3234):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
12-17 11:51:32.267: E/AndroidRuntime(3234):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
12-17 11:51:32.267: E/AndroidRuntime(3234):  at dalvik.system.NativeStart.main(Native Method) 
12-17 11:51:32.267: E/AndroidRuntime(3234): Caused by: java.lang.NullPointerException 
12-17 11:51:32.267: E/AndroidRuntime(3234):  at com.componentix.imwizard.DateChooser.setNetworkName(DateChooser.java:30) 
12-17 11:51:32.267: E/AndroidRuntime(3234):  at com.componentix.imwizard.DateChooser.onCreate(DateChooser.java:22) 
12-17 11:51:32.267: E/AndroidRuntime(3234):  at com.componentix.imwizard.DateChooser_.onCreate(DateChooser_.java:24) 
12-17 11:51:32.267: E/AndroidRuntime(3234):  at android.app.Activity.performCreate(Activity.java:5008) 
12-17 11:51:32.267: E/AndroidRuntime(3234):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
12-17 11:51:32.267: E/AndroidRuntime(3234):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 
12-17 11:51:32.267: E/AndroidRuntime(3234):  ... 11 more 
+2

取消註釋 '的setContentView(R.layout.date_chooser);' – Raghunandan

+1

您可以根據自己的日誌中的NPE,所以激活你的調試器(在第30行添加一個斷點到'setNoteworkName()')並檢查哪個對象不可用。你可以很容易地解決這個問題(或提供更多的細節,真的有什麼事情發生) – Veger

+0

@Raghunandan爲什麼我需要這樣做?我認爲我已經使用@EActivity設置了內容視圖(R.layout.date_chooser) –

回答

4

我想你的NPE是在textView上,這是非常正常的:佈局注入是在原始的oncreate()方法後生成的類中完成的。此外,你應該讓AA給你注入額外的視角。只需更新類是這樣的:

@EActivity(R.layout.date_chooser) 
public class DateChooser extends Activity { 

    @Extra("SSID") 
    String network; 

    @ViewById(R.id.textView4) 
    TextView textView; 

    @AfterViews 
    void init() { 
     textView.setText(network); 
     //some code 
    } 

} 

你真的應該仔細看看at the wiki