【问题标题】:Open a new activity within an alertdialog in Android Studio在 Android Studio 的警报对话框中打开一个新活动
【发布时间】:2020-12-30 15:03:45
【问题描述】:

我希望在 alertDialog 中启动一个新活动,但从我使用 startActivity 或创建新意图的那一刻起,我的 alertDialog 按钮就会出现错误并关闭整个应用程序。

这个想法是,如果用户选择“选择名称”选项,他会打开一个新的 Intent,其中包含另一个名为 “ChangeNameView.class” 的 Activity,并在 alertDialog 中使用另一个布局。

NewMeasureScreen.class

private void opcaoPilhaUser() {
    final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
    alertDialog.setTitle("Deseja inserir um novo nome para pilha ou escolher um já existente?");

    //Em caso afirmativo o usuário insere uma novo nome para pilha e uma nova lista.
    alertDialog.setPositiveButton("INSERIR", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            newMeasureDialog();
        }
    });

    //Em caso afirmativo o usuário insere um nome de uma pilha já existente através de uma lista.
    alertDialog.setNegativeButton("ESCOLHER NOME", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            dialogInterface.dismiss();  //Ele só finaliza a janela atual.
            startActivity(new Intent(NewMeasureScreen.this,ChangeNameView.class));

        }
    });

    //Em caso neutro sair do app e ir para TELA INICIAL DO APP e não registrar nenhum valor.
    alertDialog.setNeutralButton("CANCELAR", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            finish();
        }
    });

    //Volta para a primeira tela "HomeScreen" caso o botão de voltar no celular seja pressionado

    alertDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialogInterface) {
           finish();
        }
    });

    //Mostrar janela.
    alertDialog.show();

}

ChangeNameView.class

public class ChangeNameView extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.screen_nome_existente); fullScreen(); } private void fullScreen() { getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); } }

【问题讨论】:

  • 将日志添加到问题中。这可能是一个 NPE。还要添加方法 newMeasureDialog()

标签: java android android-activity


【解决方案1】:

Process: br.com.visit.hammerhead2, PID: 13408 android.content.ActivityNotFoundException: Unable to find explicit activity class {br.com.visit.hammerhead2/br.com.visit.hammerhead2.ChangeNameView}; have you declared this activity in your AndroidManifest.xml?

我没有在 AndroidManifest.xml 中声明 ChangeNameView 活动,所以现在我声明了它。

<activity android: name = ". ChangeNameView"
            android: label = "Hammerhead2"
            android: screenOrientation = "landscape"
            android: theme = "@ style / AppTheme" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多