1

我刚刚使用 Android 注释库开始了一个新的 Android 项目。 安卓注解

我有两个 Activity:StartScreen,其中包含一个按钮“信息”,用于启动第二个 Activity InfoActivity。两者都有注释。

第一个工作得很好,但是当我点击“信息”按钮时,我在生成的类上有一个NullPointerExceptionpublic 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>

这应该很容易,但我想错过了一些东西

4

1 回答 1

1

你如何开始你的第二个活动?

根据堆栈跟踪,似乎 Intent 中的上下文为空,所以我认为您是这样拨打电话的InfoActivity_.intent(null).start()

当您创建一个新活动时,您需要将上下文传递给它,因此在 StartScreenActivity 中您应该执行以下操作:InfoActivity_.intent(this).start()

于 2013-04-16T08:34:48.437 回答