2013-03-04 23 views
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; 
      } 

     } 

    } 

logcat的

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) 

請指導我在哪裏做錯了。它確實也有助於其他人。

+0

這是Custom_Dialog類中的13個數字行嗎? – 2013-03-04 12:08:45

回答

1

變化Custom_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 
+0

好吧,我會嘗試這個 – Suraj 2013-03-04 12:11:42

+0

仍然得到同樣的問題點擊showDialog按鈕 – Suraj 2013-03-04 12:13:19

+0

@Suraj:看到我的答案,你將需要改變它在內部onCreate或其他內部Custom_Dialog類 – 2013-03-04 12:14:37

1

您還沒有實例化的背景下mContext內:

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

而且裏面MainActivity的onCreate它傳遞給Custom_Dialog構造函數之前,活動上下文初始化context構造函數。問題似乎在那裏。

0

這不是我想的。您必須使用主題對話框創建一個活動並調用此活動。該活動應該看起來像一個對話框。

<activity android:theme="@android:style/Theme.Dialog" /> 
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(); 
      } 
     }); 
    } 
} 

定製Dialog.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); 
      ....... 

    } 
0

你有一個單獨的類來創建對話框這樣

Custom_Dialog alertbox = new Custom_Dialog(currentActivity); 

currentActivity是當前活動的參考。

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

所以,你必須同時通過上下文和用於其他類當前活動的引用。