(Android Studio Java) FAB OnClick Alert Dialog makes app crash

WK BS :

Here is activity_main code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAlignment="center"
    android:gravity="center"
    android:textSize="36sp"
    android:textColor="@color/colorPrimary"
    android:text="To Do"
    android:layout_marginTop="45dp"
    android:fontFamily="@font/montserratlight"/>

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/listView"
    android:layout_marginTop="126dp"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp">
</ListView>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginBottom="50dp"
    android:src="@drawable/plussymbol" />



</RelativeLayout>

Here is my MainActivity Java Code:

package com.geoff.productivitywatch;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

int clickCounter = 0;

@Override
protected  void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ListView list = (ListView) findViewById(R.id.listView);

    final ArrayList<String> todo = new ArrayList<>();
    todo.add("Swipe Up");

    ArrayAdapter adapter = new ArrayAdapter(this, R.layout.tododesign, todo);
    list.setAdapter(adapter);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Here is where the pop-up box code goes
            final EditText todoEditText = new EditText(getApplicationContext());
            AlertDialog dialog = new AlertDialog.Builder(getApplicationContext())
                    .setTitle("New Task")
                    .setMessage("What next?")
                    .setView(todoEditText)
                    .setPositiveButton("Add", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //Add Task to listview
                            String task = String.valueOf(todoEditText.getText());
                            todo.add("" + task);
                        }
                    })
                    .setNegativeButton("Cancel", null)
                    .create();
            dialog.show();
        }
    });

    }
}

I tried to write a method in which clicking the floating action button brought up a dialog alert box where you could edit text and then add it to a listview. Android Studio does not see an error in my code, however when I test the app on my android device, as soon as the Floating Action Button is clicked, the app crashes. However, I know it is not an issue with my device since I have tested it successfully numerous times before, it only began crashing when I added the AlertDialog method. I think I may have gone wrong with the contexts, however I have changed them to all the variants I can think of and it has not helped.

aliraza12636 :

IDK what is the actual reason causing the crash as you didn't shared the logcat but I have noticed whatever you trying to achieve is something that is possible with the Dialog and not by the AlertDilaog here is my explanation :

  1. create a separate .xlm layout file for the dialog. with you edit text and some ok and cancel button.

  2. inside the onClick of the fab create/inflate and show the dialog

        // Create a custom dialog object
    
        final Dialog dialog = new Dialog(MainActivity.this);
        // Include dialog.xml file
        dialog.setContentView(R.layout.your_dialg_layout);
        // Set dialog title
        dialog.setTitle("Custom Dialog");
    
        // set values for custom dialog components - text, image or button
    
        EditText todoEditText = dialog.findViewById(R.id.your_edit_text);
    
        Button mPositivButton = dialog.findViewById(R.id.your_yes_button);
        Button mNegativeButton = dialog.findViewById(R.id.your_cancle_button);
    
        mPositivButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String task = String.valueOf(todoEditText.getText());
                todo.add("" + task);
                dialog.dismiss();
            }
        });
        mNegativeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    
        dialog.show();
    
    1. Also this post can help you

      How to make a edittext box in a dialog

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=321512&siteId=1