2013-04-16 42 views
1

的梅索德初始化空指針異常我剛開始一個新的Android項目,與Android註釋庫。 Android AnnotationAndroid的詮釋 - 對生成的

我有兩個活動:StartScreen誰包含一個按鈕「信息」誰啓動一個seconde活動InfoActivity。兩者都註釋。

第一個工作非常好,但是當我點擊「信息」按鈕,我已經得到了NullPointerException上生成的類的public static class IntentBuilder_:「InfoActivity_」

這裏是錯誤跟蹤:

04-16 09:32:29.329: E/AndroidRuntime(561): FATAL EXCEPTION: main 
java.lang.NullPointerException 
at android.content.ComponentName.<init>(ComponentName.java:75) 
at android.content.Intent.<init>(Intent.java:3122) 
at com.cinapp.dombox.tablet.activity.InfoActivity_$IntentBuilder_.<init>(InfoActivity_.java:63) 
at com.cinapp.dombox.tablet.activity.InfoActivity_.intent(InfoActivity_.java:53) 
at com.cinapp.dombox.tablet.activity.StartScreen.buttonInformation(StartScreen.java:24) 
at com.cinapp.dombox.tablet.activity.StartScreen_$1.onClick(StartScreen_.java:44) 
at android.view.View.performClick(View.java:3511) 
at android.view.View$PerformClick.run(View.java:14105) 
at android.os.Handler.handleCallback(Handler.java:605) 
at android.os.Handler.dispatchMessage(Handler.java:92) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4424) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
at dalvik.system.NativeStart.main(Native Method) 

這裏是InfoActivity或多或少空:

@EActivity(R.layout.activity_info) 
public class InfoActivity extends Activity { 

} 

這裏是InfoActivity_

public final class InfoActivity_ 
extends InfoActivity 
{ 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    init_(savedInstanceState); 
    super.onCreate(savedInstanceState); 
    setContentView(layout.activity_info); 
} 

private void init_(Bundle savedInstanceState) { 
} 

private void afterSetContentView_() { 
} 

@Override 
public void setContentView(int layoutResID) { 
    super.setContentView(layoutResID); 
    afterSetContentView_(); 
} 

@Override 
public void setContentView(View view, LayoutParams params) { 
    super.setContentView(view, params); 
    afterSetContentView_(); 
} 

@Override 
public void setContentView(View view) { 
    super.setContentView(view); 
    afterSetContentView_(); 
} 

public static InfoActivity_.IntentBuilder_ intent(Context context) { 
    return new InfoActivity_.IntentBuilder_(context); 
} 

public static class IntentBuilder_ { 

    private Context context_; 
    private final Intent intent_; 

    public IntentBuilder_(Context context) { 
     context_ = context; 
     intent_ = new Intent(context, InfoActivity_.class); 
    } 

    public Intent get() { 
     return intent_; 
    } 

    public InfoActivity_.IntentBuilder_ flags(int flags) { 
     intent_.setFlags(flags); 
     return this; 
    } 

    public void start() { 
     context_.startActivity(intent_); 
    } 

    public void startForResult(int requestCode) { 
     if (context_ instanceof Activity) { 
      ((Activity) context_).startActivityForResult(intent_, requestCode); 
     } else { 
      context_.startActivity(intent_); 
     } 
    } 

} 

} 

最後這裏是`的Android Manifest``

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.exemple.application" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="15" 
    android:targetSdkVersion="15" /> 

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".activity.StartScreen_" 
     android:label="@string/title_activity_start_screen" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name=".activity.InfoActivity_" 
     android:label="@string/title_activity_info" > 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="com.exemple.application.activity.StartScreen_" /> 
    </activity> 
</application> 

</manifest> 

它假設是很容易的,但我想錯過的東西

+0

一點題外話,但如果沒有這個'_'風格的名字從何而來?我看到越來越多的人使用這種令人迷惑的名字風格... – WarrenFaith

+0

'_'在最後的類名,用來指代生成的類。它是Android Annotation庫使用的一個約定,請參閱:http://androidannotations.org/ –

+0

啊,好的,謝謝你... – WarrenFaith

回答

1

你如何開始你是第二活動?

根據堆棧跟蹤,似乎在意圖的情況下是空的,所以我覺得當你創建你需要傳遞一個上下文給它一個新的活動,你做你的電話是這樣InfoActivity_.intent(null).start()

,所以在StartScreenActivity你應該做這樣的事情:InfoActivity_.intent(this).start()

+0

Whao,我覺得很愚蠢。我lanching這樣的活動:'\t \t InfoActivity_.intent(上下文)。開始();'** **但我從來沒有初始化'context' –

+0

什麼是最好的「Android說文解字注」的方式來初始化一個上下文參數?我已經這樣做了:'\t @Override \t protected void onCreate(Bundle savedInstanceState){ \t \t context = this; \t \t super.onCreate(savedInstanceState); \t}' –

+0

如果你在一個活動只使用'this'。活動是上下文。如果你在Bean中,你可以使用[@RootContext註釋](https://github.com/excilys/androidannotations/wiki/Enhance-custom-classes) – DayS