0

我正在努力从活动外部使用自定义对话框,我已经在一个类中编写了对话框代码,然后在单击按钮时在活动中调用该对话框。但这对我不起作用当我单击 showDialog 按钮时应用程序崩溃。下面是代码

活动代码

package com.Customers;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources.Theme;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    Context context;
    int theme;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog);
        Button showDialog=(Button)findViewById(R.id.show_dialog);
        showDialog.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Custom_Dialog calcDialog=new Custom_Dialog(context,R.style.myCoolDialog);
                calcDialog.dailogCalculator();
            }
        });
    }
}

自定义对话框类

package com.Customers;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

public class Custom_Dialog extends Dialog implements android.view.View.OnClickListener{
        Context mContext;
        protected Custom_Dialog(Context context, int theme) {
            super(context, theme);
         }
        public boolean TouchEvent(int actionOutside) {
            return false;
    }

    public void dailogCalculator(){
                Custom_Dialog alertbox = new Custom_Dialog(mContext, R.style.myCoolDialog);
                alertbox.requestWindowFeature(Window.FEATURE_NO_TITLE);
                alertbox.setContentView(R.layout.calculator);
                EditText calcDisplay=(EditText)alertbox.findViewById(R.id.editText1);
                Button value1=(Button) alertbox.findViewById(R.id.button1);
                value1.setOnClickListener(this);
                Button value2=(Button) alertbox.findViewById(R.id.button2);
                value2.setOnClickListener(this);
                Button value3=(Button) alertbox.findViewById(R.id.button3);
                value3.setOnClickListener(this);
                Button value4=(Button) alertbox.findViewById(R.id.button4);
                value4.setOnClickListener(this);
                Button value5=(Button) alertbox.findViewById(R.id.button5);
                value5.setOnClickListener(this);
                Button value6=(Button) alertbox.findViewById(R.id.button6);
                value6.setOnClickListener(this);
                Button value7=(Button) alertbox.findViewById(R.id.button7);
                value7.setOnClickListener(this);
                Button value8=(Button) alertbox.findViewById(R.id.button8);
                value8.setOnClickListener(this);
                Button value9=(Button) alertbox.findViewById(R.id.button9);
                value9.setOnClickListener(this);
                Button value00=(Button) alertbox.findViewById(R.id.button00);
                value00.setOnClickListener(this);
                Button value0=(Button) alertbox.findViewById(R.id.button0);
                value0.setOnClickListener(this);
                Button backspaceBtn=(Button) alertbox.findViewById(R.id.backspace);
                backspaceBtn.setOnClickListener(this);
                Button Ok_dialog=(Button)alertbox.findViewById(R.id.ok_dialog);
                Ok_dialog.setOnClickListener(this);
                Button Qty_btn=(Button)alertbox.findViewById(R.id.btn_qty);
                Qty_btn.setOnClickListener(this);
                alertbox.show();  
    }
        @Override
        public void onClick(View v) {

            switch(v.getId()){
            case R.id.button1:

                break;
            case R.id.button2:

                break;
            case R.id.button3:

                break;
            case R.id.button4:

                break;
            case R.id.button5:

                break;
            case R.id.button6:

                break;
            case R.id.button7:

                break;
            case R.id.button8:

                break;
            case R.id.button9:

                break;
            case R.id.button0:
                break;
            case R.id.button00:
                break;
            case R.id.backspace:
                break;
            case R.id.btn_qty:
                break;
            case R.id.ok_dialog:
                break;
            }

        }   

    }

日志猫

03-04 16:54:47.293: E/AndroidRuntime(275):  at java.lang.reflect.Method.invokeNative(Native Method)
03-04 17:16:22.793: E/AndroidRuntime(332): FATAL EXCEPTION: main
03-04 17:16:22.793: E/AndroidRuntime(332): java.lang.NullPointerException
03-04 17:16:22.793: E/AndroidRuntime(332):  at android.app.Dialog.<init>(Dialog.java:141)
03-04 17:16:22.793: E/AndroidRuntime(332):  at com.Customers.Custom_Dialog.<init>(Custom_Dialog.java:13)
03-04 17:16:22.793: E/AndroidRuntime(332):  at com.Customers.MainActivity$1.onClick(MainActivity.java:24)
03-04 17:16:22.793: E/AndroidRuntime(332):  at android.view.View.performClick(View.java:2408)
03-04 17:16:22.793: E/AndroidRuntime(332):  at android.view.View$PerformClick.run(View.java:8816)
03-04 17:16:22.793: E/AndroidRuntime(332):  at android.os.Handler.handleCallback(Handler.java:587)
03-04 17:16:22.793: E/AndroidRuntime(332):  at android.os.Handler.dispatchMessage(Handler.java:92)
03-04 17:16:22.793: E/AndroidRuntime(332):  at android.os.Looper.loop(Looper.java:123)
03-04 17:16:22.793: E/AndroidRuntime(332):  at android.app.ActivityThread.main(ActivityThread.java:4627)
03-04 17:16:22.793: E/AndroidRuntime(332):  at java.lang.reflect.Method.invokeNative(Native Method)
03-04 17:16:22.793: E/AndroidRuntime(332):  at java.lang.reflect.Method.invoke(Method.java:521)
03-04 17:16:22.793: E/AndroidRuntime(332):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-04 17:16:22.793: E/AndroidRuntime(332):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-04 17:16:22.793: E/AndroidRuntime(332):  at dalvik.system.NativeStart.main(Native Method)

请指导我在哪里做错了。它也确实对其他人有帮助。

4

5 回答 5

1

将构造函数更改Custom_Dialog为:

Context mContext;
        protected Custom_Dialog(Context context, int theme) {
            super(context, theme);
           this.mContext=context;  //<<<<<<  initialize  mContext here
         }

并且在将其传递给构造函数之前使用 Activity 上下文进行MainActivityonCreate 初始化:contextCustom_Dialog

 //......your code here
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog);
        context=MainActivity.this //<<<<< initialize  context here
        //......your code here
于 2013-03-04T12:08:34.700 回答
1

您没有mContext在构造函数中实例化上下文。问题似乎在那里。

于 2013-03-04T12:10:32.863 回答
1

试试这个方法。它会解决它。

   public class MainActivity extends Activity {
    Context context;
    int theme;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog);
        context= this;
        Button showDialog=(Button)findViewById(R.id.show_dialog);
        showDialog.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Custom_Dialog calcDialog=new Custom_Dialog(context,R.style.myCoolDialog);
                calcDialog.show();
            }
        });
    }
}

自定义对话框.java

@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.calculator);

            EditText calcDisplay=(EditText)findViewById(R.id.editText1);
            Button value1=(Button) findViewById(R.id.button1);
            value1.setOnClickListener(this);
            Button value2=(Button) findViewById(R.id.button2);
            value2.setOnClickListener(this);
            ....... 

  }
于 2013-03-04T12:13:23.493 回答
0

这不是我认为的正确方式。您必须使用主题对话框创建一个活动并调用此活动。该活动应该看起来像一个对话框。

<activity android:theme="@android:style/Theme.Dialog" />
于 2013-03-04T12:13:07.913 回答
0

您必须像这样在单独的类中创建对话框

   Custom_Dialog alertbox = new Custom_Dialog(currentActivity);

currentActivity 是当前活动参考。

之后

LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

因此,您必须为另一个类传递上下文和当前活动引用。

于 2013-03-04T12:33:10.470 回答