如何在Android的原生扩展中启动活动并在onCreate函数上设置内容视图?

时间:2012-09-18 11:11:29

标签: android flex air air-native-extension

我正在编写一个扩展程序,显示按钮单击时的其他视图。我成功启动了活动但是在onCreate活动中我正在设置内容视图但是我收到此错误。我已将该活动包含在本机清单文件以及flex的用于android

的清单文件中
09-18 15:35:11.040: E/AndroidRuntime(7712): FATAL EXCEPTION: main
09-18 15:35:11.040: E/AndroidRuntime(7712): java.lang.RuntimeException: Unable to start activity ComponentInfo{air.TestAndroid.debug/com.aneexample.androiddialog.Second}: java.lang.NullPointerException
09-18 15:35:11.040: E/AndroidRuntime(7712):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
09-18 15:35:11.040: E/AndroidRuntime(7712):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
09-18 15:35:11.040: E/AndroidRuntime(7712):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-18 15:35:11.040: E/AndroidRuntime(7712):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
09-18 15:35:11.040: E/AndroidRuntime(7712):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-18 15:35:11.040: E/AndroidRuntime(7712):     at android.os.Looper.loop(Looper.java:123)
09-18 15:35:11.040: E/AndroidRuntime(7712):     at android.app.ActivityThread.main(ActivityThread.java:3691)
09-18 15:35:11.040: E/AndroidRuntime(7712):     at java.lang.reflect.Method.invokeNative(Native Method)
09-18 15:35:11.040: E/AndroidRuntime(7712):     at java.lang.reflect.Method.invoke(Method.java:507)
09-18 15:35:11.040: E/AndroidRuntime(7712):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
09-18 15:35:11.040: E/AndroidRuntime(7712):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
09-18 15:35:11.040: E/AndroidRuntime(7712):     at dalvik.system.NativeStart.main(Native Method)
09-18 15:35:11.040: E/AndroidRuntime(7712): Caused by: java.lang.NullPointerException
09-18 15:35:11.040: E/AndroidRuntime(7712):     at com.aneexample.androiddialog.Second.onCreate(Second.java:19)
09-18 15:35:11.040: E/AndroidRuntime(7712):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-18 15:35:11.040: E/AndroidRuntime(7712):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)

这就是我在onCreate方法上设置内容视图的方式。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        Log.d(TAG, "create Second activity");
        super.onCreate(savedInstanceState);
        FREContext freContext = AndroidDialogExtension.context;
        setContentView(freContext.getResourceId("layout.second"));
    }

2 个答案:

答案 0 :(得分:0)

您需要从FREFunction传入FREContext或在某处将其设置为静态var。

我会这样做(你应尽量避免使用静态变量,尽管我发现它们在我创建的两个ANE中非常方便):

public FREContext freContext;
public Boolean created = false;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        Log.d(TAG, "create Second activity");
        super.onCreate(savedInstanceState);
        if ( freContext != null ) {
            setContentView(freContext.getResourceId("layout.second"));
        }
        created = true;
    }

public void setFREContext(FREContext context){
    freContext = context;
    //this ensures that the contentView is only set the one time. If you didn't do it this way, it could potentially run twice which is redundant and would quickly flash the screen black.
    if ( created ) { 
        setContentView(freContext.getResourceId("layout.second"));
    }
}

(这是对FREContext为空注释的回应,它不会全部适合那个小注释空间)

答案 1 :(得分:0)

为了从活动中访问FREContext,请在此活动中创建一个公共属性:

public static FREContext context=null;

然后,在从FREObject或FREFunction开始活动之前,不要忘记将其设置为:

@Override
public FREObject call(FREContext context, FREObject[] arg1) {
...
MyActivity.context = context;