DiaLogUtils报错java.lang. IllegalStateException You need to use a Theme.AppCompat theme

来源:互联网 发布:淘宝拒收快递运费谁出 编辑:程序博客网 时间:2024/06/06 00:42

功能实现后想提取出来,要不activity看起来颇为头疼,传递context报错

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

但是和主题应该无关,google,baidu  说AppCompatActivity的问题,直接集成Activity就不报错了,我继承Activity还是报错。。。

最后在测试的时候发现传递Activity竟然可以运行,这也是偶然发现,还是dialog这块不熟悉,留个教训


package test.test.test.Utils;import android.app.Activity;import android.app.Dialog;import android.content.Context;import android.content.DialogInterface;import android.os.Handler;import android.support.v7.app.AlertDialog;import android.view.LayoutInflater;import android.view.View;import android.view.inputmethod.InputMethodManager;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;import test.test.test.R;/** * Created by 12406 on 2016/3/5. */public class DialogUtil {    public static void dialog(final Activity activity, final TextView tv_name) {        LayoutInflater factory = LayoutInflater.from(activity);        View view = factory.inflate(R.layout.dialog, null);        final EditText edit = (EditText) view.findViewById(R.id.editText);        new AlertDialog.Builder(activity)                .setTitle("请输入昵称")                .setView(view)                .setPositiveButton("确定", new DialogInterface.OnClickListener() {                    public void onClick(DialogInterface dialog, int which) {                        String trim = edit.getText().toString().trim();                        tv_name.setText(trim);                        Toast.makeText(activity, "修改成功", Toast.LENGTH_LONG).show();                    }                }).setNegativeButton("取消", null).create().show();        new Handler().postDelayed(new Runnable() {            @Override            public void run() {                //这里用handler延迟时间来自动显示键盘,不延迟键盘比EditText提前出来,然后当EditText出来时把键盘又挤下去了,有兴趣的可以测试                InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);                inputManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);            }        }, 1);    }}


写的Demo没问题,导入项目中报错了,还是这个错,网上查资料,解决了:

在AndroidMenifest.xml中加入一句:

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

     <style name="Theme.AppCompat.Light.NoActionBar" parent="@style/Theme.AppCompat.Light">         <item name="android:windowNoTitle">true</item>     </style>

0 0
原创粉丝点击