2016-11-02 44 views
-2

這是我的OnClickListener其中i超載我的方法稱爲我在android活動中的對話崩潰,我錯過了什麼?

DialogueSuccessful(); 

應啓動的臺詞。

if (Inserted == true) { 
    DialogueSuccessful(); 
    txt_FromDate.setText(""); 
    txt_ToDate.setText(""); 
    txt_total_payment.setText(""); 
} else { 
    Toast.makeText(getApplicationContext(), "Did not Insert Data", Toast.LENGTH_LONG).show(); 
} 

這裏是我的方法實現,方法的具體實現過程中的應用程序崩潰

public void DialogueSuccessful() { 
    AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext()); 
    builder.setMessage("Successfull") 
     .setTitle("Hotels Reservation") 
     .setPositiveButton("Nice", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       // FIRE ZE MISSILES! 
      } 
     }); 
    // Create the AlertDialog object and return it 
    AlertDialog Dialogue = builder.create(); 
    Dialogue.show(); 
} 

這是我在logcat的例外:

11 - 02 10: 00: 51.993 28999 - 28999/com.example.prof_mohamedatef.listview E/AndroidRuntime: FATAL EXCEPTION: main 
java.lang.IllegalStateException: Could not execute method 
for android: onClick 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java: 293) 
at android.view.View.performClick(View.java: 4432) 
at android.view.View$PerformClick.run(View.java: 18339) 
at android.os.Handler.handleCallback(Handler.java: 725) 
at android.os.Handler.dispatchMessage(Handler.java: 92) 
at android.os.Looper.loop(Looper.java: 137) 
at android.app.ActivityThread.main(ActivityThread.java: 5283) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java: 511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 1102) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 869) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.reflect.InvocationTargetException 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java: 511) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java: 288) 
at android.view.View.performClick(View.java: 4432)  
at android.view.View$PerformClick.run(View.java: 18339)  
at android.os.Handler.handleCallback(Handler.java: 725)  
at android.os.Handler.dispatchMessage(Handler.java: 92)  
at android.os.Looper.loop(Looper.java: 137)  
at android.app.ActivityThread.main(ActivityThread.java: 5283)  
at java.lang.reflect.Method.invokeNative(Native Method)  
at java.lang.reflect.Method.invoke(Method.java: 511)  
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 1102)  
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 869)  
at dalvik.system.NativeStart.main(Native Method)  
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme(or descendant) with this activity. 
at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java: 347) 
at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java: 316) 
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java: 281) 
at android.support.v7.app.AppCompatDialog.setContentView(AppCompatDialog.java: 80) 
at android.support.v7.app.AlertController.installContent(AlertController.java: 214) 
at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java: 257) 
at android.app.Dialog.dispatchOnCreate(Dialog.java: 357) 
at android.app.Dialog.show(Dialog.java: 261) 
at com.example.prof_mohamedatef.listview.Booking_hotels.DialogueSuccessful(Booking_hotels.java: 448) 
at com.example.prof_mohamedatef.listview.Booking_hotels.Confirm_Reservation(Booking_hotels.java: 419) 
at java.lang.reflect.Method.invokeNative(Native Method)  
at java.lang.reflect.Method.invoke(Method.java: 511)  
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java: 288)  
at android.view.View.performClick(View.java: 4432)  
at android.view.View$PerformClick.run(View.java: 18339)  
at android.os.Handler.handleCallback(Handler.java: 725)  
at android.os.Handler.dispatchMessage(Handler.java: 92)  
at android.os.Looper.loop(Looper.java: 137)  
at android.app.ActivityThread.main(ActivityThread.java: 5283)  
at java.lang.reflect.Method.invokeNative(Native Method)  
at java.lang.reflect.Method.invoke(Method.java: 511)  
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 1102)  
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 869)  
at dalvik.system.NativeStart.main(Native Method)  

這是所有的代碼,我使用啓動我的期望的臺詞,我應該添加任何東西嗎?

幫我一個忙,如果ü請,您的幫助將不勝感激 謝謝

+0

更改方法簽名'公共無效DialogueSuccessful(查看視圖)'如果使用'機器人:的onclick = 「DialogueSuccessful」'以xml –

+0

此外代替使用'getApplicationContext()'使用當前活動上下文創建AlertDialog –

+0

謝謝,切換到活動上下文是成功的 –

回答

0

getApplicationContext() 

切換到活動上下文是succesul

public void DialogueSuccessful(){ 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setMessage("Successfull") 
      .setTitle("Hotels Reservation") 
      .setPositiveButton("Nice", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        // FIRE ZE MISSILES! 
       } 
      }); 
    // Create the AlertDialog object and return it 
    AlertDialog Dialogue=builder.create(); 
    Dialogue.show(); 
} 
0

在AndroidManifest.xml檔案活動標籤添加

android:theme="@style/Theme.AppCompat.Light" 

0

我認爲你已經在xml-layout文件中使用onClick屬性。你也沒有使用適合你的項目的主題。顯示你的XML或應用主題活動。

0

修改您dialogueSuccessful方法

替換此線

AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext()); 

AlertDialog.Builder builder =new AlertDialog.Builder(
    new ContextThemeWrapper(getApplicationContext(), android.R.style.Theme_Dialog)); 
相關問題