pearmak
pearmak

Reputation: 5027

Android: calling methods of parent in an extended view

i am having an activity (A) of which there is a

private SpotOnView view; // displays and manages the game

In Activity A's OnCreate

  // create a new SpotOnView and add it to the RelativeLayout
  RelativeLayout layout = (RelativeLayout) findViewById(R.id.relativeLayout);
  view = new SpotOnView(this, layout); 
  layout.addView(view, 0); // add view to the layout

In SpotOnView:

public class SpotOnView extends View 
{
   // constructs a new SpotOnView
   public SpotOnView(Context context, RelativeLayout parentLayout)
   {
      super(context);
      ...
}

In this relativelayout of the view, there is a countdown timer where when time is up, a dialog box is created for either replay or quit, code as follows:

public void replay_dialog() 
{
    pause();
    final Dialog dialog1 = new Dialog(getContext(), android.R.style.Theme_Translucent_NoTitleBar);
    WindowManager.LayoutParams lp = dialog1.getWindow().getAttributes();
    lp.dimAmount = 0.7f;
    dialog1.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    ...
    alert_quit.setOnClickListener(new OnClickListener() 
    {
        public void onClick(View v) 
        {               
            ActivityA.onBackPressed();
        }
    });

    alert_replay.setOnClickListener(new OnClickListener() 
    {
        public void onClick(View v) 
        {               
            ActivityA.restarting(); //Error line 570
        }
    }); 

Logcat:

11-20 00:15:51.320: E/AndroidRuntime(31395): FATAL EXCEPTION: main
11-20 00:15:51.320: E/AndroidRuntime(31395): java.lang.NullPointerException
11-20 00:15:51.320: E/AndroidRuntime(31395):    at com.abc.abc.SpotOnView$5.onClick(SpotOnView.java:570)
11-20 00:15:51.320: E/AndroidRuntime(31395):    at android.view.View.performClick(View.java:4223)
11-20 00:15:51.320: E/AndroidRuntime(31395):    at android.view.View$PerformClick.run(View.java:17275)
11-20 00:15:51.320: E/AndroidRuntime(31395):    at android.os.Handler.handleCallback(Handler.java:615)
11-20 00:15:51.320: E/AndroidRuntime(31395):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-20 00:15:51.320: E/AndroidRuntime(31395):    at android.os.Looper.loop(Looper.java:137)
11-20 00:15:51.320: E/AndroidRuntime(31395):    at android.app.ActivityThread.main(ActivityThread.java:4898)
11-20 00:15:51.320: E/AndroidRuntime(31395):    at java.lang.reflect.Method.invokeNative(Native Method)
11-20 00:15:51.320: E/AndroidRuntime(31395):    at java.lang.reflect.Method.invoke(Method.java:511)
11-20 00:15:51.320: E/AndroidRuntime(31395):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
11-20 00:15:51.320: E/AndroidRuntime(31395):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
11-20 00:15:51.320: E/AndroidRuntime(31395):    at dalvik.system.NativeStart.main(Native Method)

Logcat2 if ((ActivityA)getContext()).restarting();

11-20 00:31:42.315: E/AndroidRuntime(6089): FATAL EXCEPTION: main
11-20 00:31:42.315: E/AndroidRuntime(6089): java.lang.RuntimeException: Unable to pause activity {com.abc.abc/com.abc.abc.ActivityA}: java.lang.NullPointerException
11-20 00:31:42.315: E/AndroidRuntime(6089):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2879)
11-20 00:31:42.315: E/AndroidRuntime(6089):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2835)
11-20 00:31:42.315: E/AndroidRuntime(6089):     at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2813)
11-20 00:31:42.315: E/AndroidRuntime(6089):     at android.app.ActivityThread.access$800(ActivityThread.java:140)
11-20 00:31:42.315: E/AndroidRuntime(6089):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1238)
11-20 00:31:42.315: E/AndroidRuntime(6089):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-20 00:31:42.315: E/AndroidRuntime(6089):     at android.os.Looper.loop(Looper.java:137)
11-20 00:31:42.315: E/AndroidRuntime(6089):     at android.app.ActivityThread.main(ActivityThread.java:4898)
11-20 00:31:42.315: E/AndroidRuntime(6089):     at java.lang.reflect.Method.invokeNative(Native Method)
11-20 00:31:42.315: E/AndroidRuntime(6089):     at java.lang.reflect.Method.invoke(Method.java:511)
11-20 00:31:42.315: E/AndroidRuntime(6089):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
11-20 00:31:42.315: E/AndroidRuntime(6089):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
11-20 00:31:42.315: E/AndroidRuntime(6089):     at dalvik.system.NativeStart.main(Native Method)
11-20 00:31:42.315: E/AndroidRuntime(6089): Caused by: java.lang.NullPointerException
11-20 00:31:42.315: E/AndroidRuntime(6089):     at com.abc.abc.SpotOnView.pause(SpotOnView.java:201)
11-20 00:31:42.315: E/AndroidRuntime(6089):     at com.abc.abc.ActivityA.onPause(ActivityA.java:176)
11-20 00:31:42.315: E/AndroidRuntime(6089):     at android.app.Activity.performPause(Activity.java:5304)
11-20 00:31:42.315: E/AndroidRuntime(6089):     at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1240)
11-20 00:31:42.315: E/AndroidRuntime(6089):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2866)
11-20 00:31:42.315: E/AndroidRuntime(6089):     ... 12 more

Question:

The logcat states NullPointerException for calling methods in ActivityA. How could an extended view to call method in the holding activity?

Thanks!!

Upvotes: 1

Views: 393

Answers (1)

Blackbelt
Blackbelt

Reputation: 157457

If the view is hosted by ActivityA you should be able to perform the following Cast

   ((ActivityA)getContext()).restarting();

Upvotes: 1

Related Questions