AlertDialog show = new AlertDialog.Builder(this) is Undefined

29,168

Solution 1

Do this:

AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Message")
    .setMessage(str)
    .setNeutralButton("OK", null);

AlertDialog dialog = builder.create();
dialog.show();

Instead of:

AlertDialog show = new AlertDialog.Builder(this)
    .setTitle("Message")
    .setMessage(str)
    .setNeutralButton("OK", null)
    .show();

You have to create an instance of an AlertDialog.Builder first. Then you can build the Dialog with builder.create(). Then you can show the Dialog with .show().

Solution 2

I'm new to android but found that sometimes you cant create an alert inside a class that is not an activity. you should then get the context directly and create the alert.

public void showAlert(String message){
    AlertDialog.Builder builder = new AlertDialog.Builder(this.getContext());
    builder.setTitle("Here is a message message from my activity")
        .setMessage(message)
        .setNeutralButton("OK", null);
    AlertDialog dialog = builder.create();
    dialog.show();
}
Share:
29,168
thedeepfield
Author by

thedeepfield

Ruby/RoR, Python/Django, JAVA/Android.

Updated on August 21, 2020

Comments

  • thedeepfield
    thedeepfield over 3 years

    The app intercepts sms messages and displays a Dialog of the message.

    However I am unable to get my Dialog error resolved in my Test class. What am I doing wrong?

    (I've also included my other 2 files).

    ERROR shown in Eclipse: AlertDialog.Builder(Test) is undefined


    test.java

    package com.example.firstapp;
    
    import android.app.AlertDialog;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.telephony.SmsMessage;
    
    public class Test extends BroadcastReceiver
    {
        @Override
        public void onReceive(Context context, Intent intent) 
        {
            //---get the SMS message passed in---
            Bundle bundle = intent.getExtras();        
            SmsMessage[] msgs = null;
            String str = "";            
            if (bundle != null)
            {
                //---retrieve the SMS message received---
                Object[] pdus = (Object[]) bundle.get("pdus");
                msgs = new SmsMessage[pdus.length];            
                for (int i=0; i<msgs.length; i++){
                    msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
                    str += "SMS from " + msgs[i].getOriginatingAddress();                     
                    str += " :";
                    str += msgs[i].getMessageBody().toString();
                    str += "\n";                  
                }
    
                AlertDialog show = new AlertDialog.Builder(this)
                .setTitle("Message")
                .setMessage(str)
                .setNeutralButton("OK", null)
                .show();
        }                         
    }
    

    }


    AndroidManifest.xml

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.firstapp"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="9"
            android:targetSdkVersion="15" />
        <uses-permission android:name="android.permission.SEND_SMS">
        </uses-permission>
        <uses-permission android:name="android.permission.RECEIVE_SMS">
        </uses-permission>
        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".Hello"
                android:label="@string/title_activity_hello" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <receiver android:name=".test"> 
                <intent-filter> 
                    <action android:name=
                        "android.provider.Telephony.SMS_RECEIVED" /> 
                </intent-filter> 
            </receiver>
        </application>
    
    </manifest>
    

    hello.java

    package com.example.firstapp;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;
    
    public class Hello extends Activity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.activity_hello);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_hello, menu);
            return true;
        }  
    }