2016-05-29 2 views
0

У меня возникает пара проблем, связанных с моей кнопкой onClick на моем TextView. В моем коде, я делаю alertDialog, чтобы показать, когда есть событие onClick. Тем не менее, приложение запирающее усилие:IllegalStateException: AlertDialog, onClick, ThemeWrapper

Это ProfilePage.java

package com.tanishqsharma.unanswered.BackgroundProcesses; 

import android.app.Activity; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.support.v7.app.AlertDialog; 
import android.view.ContextThemeWrapper; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.tanishqsharma.unanswered.R; 
import com.tanishqsharma.unanswered.dbHandler.PrefManager; 


import de.hdodenhof.circleimageview.CircleImageView; 

public class ProfilePage extends Activity { 

    //UI elements 
    TextView userMobile; 
    TextView userName; 
    Button updateButton; 

    String mobile; 
    String name; 
    PrefManager prefManager; 
    CircleImageView profile_image; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_profile_page); 

     userMobile = (TextView)findViewById(R.id.userMobile); 
     userName = (TextView)findViewById(R.id.userName); 
     updateButton = (Button)findViewById(R.id.updateButton); 
     profile_image = (CircleImageView)findViewById(R.id.profile_image); 

     prefManager = new PrefManager(getApplicationContext()); 


     mobile = "+91 "+prefManager.getUserMobile(); 
     name = prefManager.getUserName(); 


     //Setting Up Name 
     if(name.equals("")) 
     { 
      userName.setText("Enter Your Name"); 
     } 
     else 
     { 
      userName.setText(name); 
     } 


     //Setting Up Mobile Display Button 
     userMobile.setText(mobile); 
     userMobile.setEnabled(false); 


    } 


    public void editNameDialogue(View v) 
    { 
     //This shows the dialogue box which sets the text to the name textview 

     AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(getApplicationContext(), R.style.AlertDialogCustom)); 
     LayoutInflater inflater = this.getLayoutInflater(); 
     final View dialogue = inflater.inflate(R.layout.editing_name, null); 
     builder.setView(dialogue); 

     final EditText entered_name = (EditText)dialogue.findViewById(R.id.edited_name); 

     builder.setMessage("Entered Name"); 
     builder.setPositiveButton("Set", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       String newName = entered_name.getText().toString(); 
       userName.setText(newName); 
      } 
     }); 
     builder.setCancelable(false); 
     builder.create(); 
     builder.show(); 

    } 

    public void selectDP(View view) 
    { 
     //This will select DP and set it to the CircularImageView waiting to be transfered to the server. 
     Toast.makeText(ProfilePage.this, "Lets select DP", Toast.LENGTH_SHORT).show(); 
    } 

    public void updateDetails(View view) 
    { 
     Toast.makeText(ProfilePage.this, "Nothing Right Now", Toast.LENGTH_SHORT).show(); 
     //This will send everything to the server and preference manager. 
     //And send to the main activity 
    } 


} 

Это ошибка:

05-29 21:39:13.622 15754-15754/com.tanishqsharma.unanswered E/AndroidRuntime: FATAL EXCEPTION: main 
Process: com.tanishqsharma.unanswered, PID:15754 java.lang.IllegalStateException: Could not execute method for android:onClick                 at android.view.View$DeclaredOnClickListener.onClick(View.java:4463) 
at android.view.View.performClick(View.java:5209) 
at android.view.View$PerformClick.run(View.java:21185) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5468) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: java.lang.reflect.InvocationTargetException 
at java.lang.reflect.Method.invoke(Native Method) 
at android.view.View$DeclaredOnClickListener.onClick(View.java:4458) 
at android.view.View.performClick(View.java:5209) 
at android.view.View$PerformClick.run(View.java:21185) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5468) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 
at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:343) 
at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:312) 
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:277) 
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:256) 
at android.app.Dialog.dispatchOnCreate(Dialog.java:394) 
at android.app.Dialog.show(Dialog.java:295) 
at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:918) 
at com.tanishqsharma.unanswered.BackgroundProcesses.ProfilePage.editNameDialogue(ProfilePage.java:91) 
at java.lang.reflect.Method.invoke(Native Method) 
at android.view.View$DeclaredOnClickListener.onClick(View.java:4458) 
at android.view.View.performClick(View.java:5209) 
at android.view.View$PerformClick.run(View.java:21185) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5468) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

ответ

1
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 

Видимо, R.style.AlertDialogCustom не основано на AppCompat тема.

+0

Как исправить это? Просто замените его на любой другой из списка? –

+0

Я изменил его на R.style.app, но возникла другая проблема: вызвана: android.view.WindowManager $ BadTokenException: не удалось добавить окно - токен null не для приложения –

+0

@TanishqSharma: «Как сделать Я исправлю это? - вы создали 'R.style.AlertDialogCustom'. Вам нужно, чтобы он наследовал тему 'Theme.AppCompat', например' Theme.AppCompat.Dialog'. – CommonsWare