Android自定义对话框意味着强制关闭

时间:2011-11-13 20:34:54

标签: java android android-layout android-dialog

我在我的代码上调用了一个对话框函数,但每次我测试它都会使一个力量关闭... 这是我的代码:

   public void onLongPressFinished(MotionEvent arg0,
                ManagedOverlay arg1, GeoPoint arg2, ManagedOverlayItem item) {


            if (item!= null) // if the user longpresses a marker, it will pop up the route dialog
            {

                RouteDialog dialog = new RouteDialog();
                dialog.showCustomDialog();


            }

和我用来实现对话框的类:

package com.BuStopTracker;

import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class RouteDialog extends Activity{



        public void showCustomDialog(){

            final Dialog dialog = new Dialog(this);

            dialog.setContentView(R.layout.routetodialog);//loads layout from xml file
            dialog.setTitle("Título do custom dialog");

            final Button ok = (Button) dialog.findViewById(R.id.bt_ok);
            final Button cancelar = (Button) dialog.findViewById(R.id.bt_cancel);
            final EditText editText = (EditText) dialog.findViewById(R.id.inputText);

            ok.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {

                    //OK button action

                }
            });

            cancelar.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {


                    dialog.dismiss();

                }
            });

        dialog.show();

        }



}

我知道我的“LongPress探测器”功能正常工作,因为我用吐司测试了它,它工作得很好......

那么,有人可以帮助我吗?我很确定我的错是在我调用对话框的方式,我只是看不出原因或在哪里...

感谢。

PS:根据要求,这是logcat:

11-13 20:13:54.789: D/dalvikvm(406): GC_CONCURRENT freed 1153K, 54% free 3159K/6727K, external 2002K/2137K, paused 4ms+4ms
11-13 20:13:55.109: D/dalvikvm(406): GC_CONCURRENT freed 616K, 51% free 3363K/6727K, external 2002K/2137K, paused 5ms+7ms
11-13 20:13:55.369: D/dalvikvm(406): GC_EXTERNAL_ALLOC freed 721K, 54% free 3116K/6727K, external 2121K/2137K, paused 52ms
11-13 20:13:55.829: I/MapActivity(406): Handling network change notification:CONNECTED
11-13 20:13:55.829: E/MapActivity(406): Couldn't get connection factory client
11-13 20:13:55.979: D/dalvikvm(406): GC_EXTERNAL_ALLOC freed 215K, 56% free 3020K/6727K, external 2440K/2692K, paused 77ms
11-13 20:14:17.679: D/AndroidRuntime(406): Shutting down VM
11-13 20:14:17.679: W/dalvikvm(406): threadid=1: thread exiting with uncaught exception (group=0x40015560)
11-13 20:14:17.719: E/AndroidRuntime(406): FATAL EXCEPTION: main
11-13 20:14:17.719: E/AndroidRuntime(406): java.lang.IllegalStateException: System services not available to Activities before onCreate()
11-13 20:14:17.719: E/AndroidRuntime(406):  at android.app.Activity.getSystemService(Activity.java:3536)
11-13 20:14:17.719: E/AndroidRuntime(406):  at android.app.Dialog.<init>(Dialog.java:141)
11-13 20:14:17.719: E/AndroidRuntime(406):  at android.app.Dialog.<init>(Dialog.java:123)
11-13 20:14:17.719: E/AndroidRuntime(406):  at com.BuStopTracker.RouteDialog.showCustomDialog(RouteDialog.java:23)
11-13 20:14:17.719: E/AndroidRuntime(406):  at com.BuStopTracker.BusTrackerBetaActivity$7.onLongPressFinished(BusTrackerBetaActivity.java:278)
11-13 20:14:17.719: E/AndroidRuntime(406):  at de.android1.overlaymanager.ManagedOverlayGestureDetector.invokeLongPressFinished(ManagedOverlayGestureDetector.java:76)
11-13 20:14:17.719: E/AndroidRuntime(406):  at de.android1.overlaymanager.ManagedOverlay.draw(ManagedOverlay.java:79)
11-13 20:14:17.719: E/AndroidRuntime(406):  at com.google.android.maps.Overlay.draw(Overlay.java:179)
11-13 20:14:17.719: E/AndroidRuntime(406):  at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:42)
11-13 20:14:17.719: E/AndroidRuntime(406):  at com.google.android.maps.MapView.onDraw(MapView.java:530)
11-13 20:14:17.719: E/AndroidRuntime(406):  at android.view.View.draw(View.java:6880)
11-13 20:14:17.719: E/AndroidRuntime(406):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
11-13 20:14:17.719: E/AndroidRuntime(406):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-13 20:14:17.719: E/AndroidRuntime(406):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
11-13 20:14:17.719: E/AndroidRuntime(406):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-13 20:14:17.719: E/AndroidRuntime(406):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
11-13 20:14:17.719: E/AndroidRuntime(406):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-13 20:14:17.719: E/AndroidRuntime(406):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
11-13 20:14:17.719: E/AndroidRuntime(406):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-13 20:14:17.719: E/AndroidRuntime(406):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
11-13 20:14:17.719: E/AndroidRuntime(406):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-13 20:14:17.719: E/AndroidRuntime(406):  at android.view.View.draw(View.java:6883)
11-13 20:14:17.719: E/AndroidRuntime(406):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
11-13 20:14:17.719: E/AndroidRuntime(406):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
11-13 20:14:17.719: E/AndroidRuntime(406):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-13 20:14:17.719: E/AndroidRuntime(406):  at android.view.View.draw(View.java:6883)
11-13 20:14:17.719: E/AndroidRuntime(406):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
11-13 20:14:17.719: E/AndroidRuntime(406):  at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
11-13 20:14:17.719: E/AndroidRuntime(406):  at android.view.ViewRoot.draw(ViewRoot.java:1522)
11-13 20:14:17.719: E/AndroidRuntime(406):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
11-13 20:14:17.719: E/AndroidRuntime(406):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
11-13 20:14:17.719: E/AndroidRuntime(406):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-13 20:14:17.719: E/AndroidRuntime(406):  at android.os.Looper.loop(Looper.java:130)
11-13 20:14:17.719: E/AndroidRuntime(406):  at android.app.ActivityThread.main(ActivityThread.java:3683)
11-13 20:14:17.719: E/AndroidRuntime(406):  at java.lang.reflect.Method.invokeNative(Native Method)
11-13 20:14:17.719: E/AndroidRuntime(406):  at java.lang.reflect.Method.invoke(Method.java:507)
11-13 20:14:17.719: E/AndroidRuntime(406):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-13 20:14:17.719: E/AndroidRuntime(406):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-13 20:14:17.719: E/AndroidRuntime(406):  at dalvik.system.NativeStart.main(Native Method)
11-13 20:14:20.059: I/Process(406): Sending signal. PID: 406 SIG: 9

1 个答案:

答案 0 :(得分:1)

您不应在RouteDialog中扩展Activity。将一个上下文传递给showCustomDialog或者让一个构造函数接受它并将它存储在你的类中。

要解决这个问题,可以在RouteDialog中轻松更改:

public class RouteDialog {
        public void showCustomDialog(Context context){
            final Dialog dialog = new Dialog(context);
            /* leave rest as is */

然后更改代码以显示对话框:

RouteDialog dialog = new RouteDialog();
dialog.showCustomDialog(this);