我正在进行一项(A)活动,
private SpotOnView view; // displays and manages the game
在活动A的
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
在SpotOnView中:
public class SpotOnView extends View
{
// constructs a new SpotOnView
public SpotOnView(Context context, RelativeLayout parentLayout)
{
super(context);
...
}
在该视图的相对布局中,有一个倒数计时器,当时间到时,将为重播或退出创建一个对话框,代码如下:
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
题:
logcat指出
NullPointerException
用于在ActivityA中调用方法。扩展活动如何在保持活动中调用方法?谢谢!!
最佳答案
如果视图由ActivityA
托管,则您应该能够执行以下Cast
((ActivityA)getContext()).restarting();