0

我正在编写一个扩展,显示按钮单击的其他视图。我成功启动了该活动,但在 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"));
    }
4

2 回答 2

0

您需要从 FREFunction 传入 FREContext 或将其设置为某处的静态变量。

我会这样做(您应该尽可能避免使用静态变量,尽管我发现它们在我创建的两个 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 为空评论的回应,它只是不适合那个小评论空间)

于 2012-09-18T17:15:10.463 回答
0

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

public static FREContext context=null;

然后,在从 FREObject 或 FREFunction 开始活动之前,不要忘记像这样设置它:

@Override
public FREObject call(FREContext context, FREObject[] arg1) {
...
MyActivity.context = context;
于 2015-03-06T20:54:34.810 回答