应用程序在AlertDialog .show()崩溃 - 我找到了解决方案

时间:2017-06-22 18:03:49

标签: android alertdialog

我是新来的。 我正在建立一个应用程序,但每当涉及到AlertDialog .show()时,它都会崩溃。我已经在互联网上进行过研究,但无法找到任何解决方案。我发现的大多数情况都是通过更改“上下文”来解决问题。上 AlertDialog.Builder示例= new AlertDialog.Builder(Context) 至 AlertDialog.Builder示例= new AlertDialog.Builder(MyActivity.this);

但那不是我的情况,我已经在使用MyActivity.this all long and still cshhes。

这是我的代码:

ublic类MainActivity扩展了AppCompatActivity {

Button btMudaTela;

final Funcoes funcao = new Funcoes();

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

    Button btMudaTela = (Button) findViewById(R.id.btMudaTela);

    //Ao clicar no botão outra activity será aberta (ActivityCadastro)
    btMudaTela.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertDialog.Builder adb = new AlertDialog.Builder(MainActivity.this);
            adb.setTitle("teste");
            adb.setMessage("ok");
            adb.create().show();
            funcao.mudaActivity(MainActivity.this, ActivityCadastro.class);
            finish();
        }
    });

}

}

以下是我设备上Logcat的日志:

06-23 07:41:42.113  3303  3303 E AndroidRuntime: Process: com.example.teste.projetofinal, PID: 3303

06-23 07:41:42.113  3303  3303 E AndroidRuntime:    at com.example.teste.projetofinal.MainActivity$1.onClick(MainActivity.java:46)

06-23 07:41:42.123   954  1367 W ActivityManager:   Force finishing activity com.example.teste.projetofinal/.MainActivity

06-23 07:41:43.334   954  1602 I ActivityManager: Process com.example.teste.projetofinal (pid 3303) (adj 9) has died.

06-23 07:41:43.334   954  1454 I WindowState: WIN DEATH: Window{444ca450 u0 com.example.teste.projetofinal/com.example.teste.projetofinal.MainActivity}

06-23 07:50:39.406  6806  6806 W InstallAppProgress: Replacing package:com.example.teste.projetofinal

06-23 07:50:42.219  5389  5389 I Finsky  : [1] com.google.android.finsky.verifier.impl.br.c(104): Verification complete: id=0, package_name=com.example.teste.projetofinal

06-23 07:50:42.960   954  1046 I PackageManager: Package com.example.teste.projetofinal codePath changed from /data/app/com.example.teste.projetofinal-19.apk to /data/app/com.example.teste.projetofinal-20.apk; Retaining data and using new

06-23 07:50:43.921   954  1046 W PackageManager: Code path for pkg : com.example.teste.projetofinal changing from /data/app/com.example.teste.projetofinal-19.apk to /data/app/com.example.teste.projetofinal-20.apk

06-23 07:50:43.921   954  1046 W PackageManager: Resource path for pkg : com.example.teste.projetofinal changing from /data/app/com.example.teste.projetofinal-19.apk to /data/app/com.example.teste.projetofinal-20.apk

06-23 07:50:45.592   954  1036 I CrashAnrDetector: onPackageUpdateFinished : com.example.teste.projetofinal

06-23 07:50:46.073  6987  6987 E dalvikvm: >>>>> com.example.teste.projetofinal [ userId:0 | appId:10200 ]

06-23 07:50:47.444  6987  6987 E AndroidRuntime: Process: com.example.teste.projetofinal, PID: 6987

06-23 07:50:47.444  6987  6987 E AndroidRuntime:    at com.example.teste.projetofinal.MainActivity$1.onClick(MainActivity.java:40)

06-23 07:50:47.444   954  1602 W ActivityManager:   Force finishing activity com.example.teste.projetofinal/.MainActivity

06-23 07:50:48.806   954  1602 I WindowState: WIN DEATH: Window{433e3670 u0 com.example.teste.projetofinal/com.example.teste.projetofinal.MainActivity}

06-23 07:50:48.836   954  1545 I ActivityManager: Process com.example.teste.projetofinal (pid 6987) (adj 9) has died.

06-23 07:50:51.188  3939  3939 E SPPClientService: [PackageInfoChangeReceiver] [handlePkgRemovedEvent] PackageName : com.example.teste.projetofinal, true, false

06-23 07:50:54.531  5389  5389 I Finsky  : [1] com.google.android.finsky.externalreferrer.d.run(9): Package state data is missing for com.example.teste.projetofinal

06-23 07:50:55.792  3498  7271 I FontsPackageChangeOp: Package com.example.teste.projetofinal has no metadata

呀!!!!在互联网上挖掘了2天后,我终于找到了解决方案: Crash in AlertDialog builder when android suport library updated to 24

您需要为AlertDialog导入此内容:

 import android.app.AlertDialog;

而不是:

 import android.support.v7.app.AlertDialog; 

2 个答案:

答案 0 :(得分:0)

如果没有你的堆栈跟踪,确切地找出问题有点困难,但是,我猜它与你的构建器有关。

你有这一行:

AlertDialog.Builder alerta = new AlertDialog.Builder(ActivityCadastro.this);

然后在对象上设置各种字段。接近结束时,请致电

alerta.show()

问题是,这不是您正在调用它的警告对话框,这是一个构建器对象。要解决此问题,您需要通过在构建器上调用build来实际创建警报对话框对象;只需将代码更改为以下内容:

alerta.create().show();

参考:https://developer.android.com/reference/android/app/AlertDialog.Builder.html

如果能解决问题,请告诉我。

-Sil

答案 1 :(得分:0)

简单地使用

adb.show();

而不是

adb.create().show();