0

I am writing an extension that show other view on button click. I succeed to launch the activity but on the onCreate activity i am setting content view but I am getting this error. I have included that activity in the native manifest file as well as in flex's manifest file for 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)

This is how I am setting content view on onCreate method.

@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"));
    }
iCoder
  • 1,298
  • 1
  • 9
  • 25
  • Yes you right can tell me any solution for that ? – iCoder Sep 18 '12 at 11:36
  • now I am doing like that yet facing the same issue.if(freContext !=null){ setContentView(freContext.getResourceId("layout.second"));} else{ Log.d(TAG, "freContext is null"); } – iCoder Sep 18 '12 at 11:50

2 Answers2

0

You need to either pass in the FREContext from a FREFunction or set it as a static var somewhere.

I would do it like this (you should try to avoid static vars whenever possible, though I have found them to be very handy in the two ANEs I have created):

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"));
    }
}

(This was in response to the FREContext being null comment, it just wouldn't all fit within that little comment space)

Josh
  • 8,079
  • 3
  • 24
  • 49
  • Thanks for the reply but i am facing the problem when activity executes for the first time . – iCoder Sep 19 '12 at 04:36
0

In order to access FREContext from an activity create a public property like this into the activity:

public static FREContext context=null;

and then, before starting the activity from the FREObject or FREFunction, don't forget to set it up like this:

@Override
public FREObject call(FREContext context, FREObject[] arg1) {
...
MyActivity.context = context;
Delcasda
  • 371
  • 4
  • 13