我是新來的。 我正在構建一個應用程序,但每當涉及到AlertDialog .show()它都會崩潰。我已經在互聯網上進行了研究,但找不到任何解決方案。大部分時間我已經找到了案件上了車 AlertDialog.Builder例如=新AlertDialog.Builder(上下文) 固定,通過改變「語境」的問題 AlertDialog.Builder例如=新AlertDialog.Builder(MyActivity.this);AlertDialog上的應用程序崩潰.show() - 我找到了解決方案
但是,這不是我的情況,我一直在使用MyActivity.this,並且仍然崩潰。
這裏是我的代碼:
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;
請添加例外,堆棧跟蹤您收到。 – ventiseis
我已經將我的代碼更改爲我的MainActivity(同一個項目),因爲它更易於閱讀並且因爲問題相同。第40行是行adb.create()。show();如果它是adb.show(),則會發生同樣的情況。 我還添加了在我的設備上運行的Logcat。 –
你救了我,謝謝! – luidgi27