0

I am new in android learning. Using Android Studio. My app name is Design1. I am learning events so created a event page with two buttons for testing two events, one is toast and another is Alert dialog box.The Toast is working fine but when I click ALERT button to show the alert dialog then a message comes ie "Design1 has stopped".

If I remove the showAlert event from activity_events.xml and events.java files then my works perfect.

events.java

package com.example.borntoflirt.design1;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Camera;
import android.hardware.camera2.CameraManager;
import android.os.Build;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class events extends AppCompatActivity {

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

public void showToast(View v) {
    Context context = getApplicationContext();  // OR getBaseContext()
    CharSequence text = "Hi Toast";
    int duration = Toast.LENGTH_SHORT;

    Toast toast = Toast.makeText(context, text, duration);
    toast.show();
}

public void showAlert(View v) {
    Context context1 = getApplicationContext();
    AlertDialog.Builder builder = new AlertDialog.Builder(context1);
    builder.setMessage("Write your message here.");
    builder.setCancelable(true);

    builder.setPositiveButton(
            "Yes",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });

    builder.setNegativeButton(
            "No",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });

    AlertDialog alert = builder.create();
    alert.show();
}

}

activity_events.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_red_light"
android:isScrollContainer="false"
android:orientation="vertical"
android:paddingLeft="15dp"
android:paddingRight="15dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="2dp"
        android:layout_weight=".30"
        android:background="@drawable/rounded_button"
        android:text="Toast"
        android:textColor="#000"
        android:onClick="showToast"/>

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".30"
        android:text="Alert"
        android:textColor="#000"
        android:background="@drawable/rounded_button"
        android:layout_margin="2dp"
        android:onClick="showAlert"/>

</LinearLayout>

</LinearLayout>
Shan Biswas
  • 397
  • 2
  • 8
  • 24
  • Post logs please – Valentun Nov 12 '17 at 14:08
  • 1
    Possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Zoe Nov 12 '17 at 14:09
  • Did you add events.java in AndroidManifest ? and also i do not see anywhere you are calling showtoast()and showAlert().. So the solution is Read your logs you will find the error . – ADM Nov 12 '17 at 14:11
  • @ADM pls see, I have updated the question with activity_events.xml where you will see that I am calling both functions – Shan Biswas Nov 12 '17 at 14:16

4 Answers4

0

You need to use Activity context. If you use application context, the IllegalStateExpetion will be thrown.

 ...
new AlertDialog.Buidler(this) // this is a Activity, which is a Context's subclass
 ....
Valentun
  • 1,661
  • 12
  • 23
  • @ShanBiswas for future questions, it's very recommended to provide logs. You can found it in 'Android monitor' tab ('error' filter) – Valentun Nov 12 '17 at 14:33
0

Use Activity's Context :

AlertDialog.Builder builder = new AlertDialog.Builder(event.this);

Instead Of

 Context context1 = getApplicationContext();
AlertDialog.Builder builder = new AlertDialog.Builder(context1);
ADM
  • 20,406
  • 11
  • 52
  • 83
0

Try to use getActivity() instead of getApplicationContext().

arma_best
  • 65
  • 1
  • 2
  • 13
0

It has been 7 months but none of the answers worked on me. So this is solution that i found. Try MainActivity.this instead of getApplicationContext() or this.