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

功能实现后想提取出来,要不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>

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值