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

the*_*eld 2 java sms android this android-alertdialog

该应用程序拦截短信息并显示一条Dialog消息.

但是我无法DialogTest课堂上解决我的错误.我究竟做错了什么?

(我还包括了我的其他2个文件).

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();
    }                         
}
Run Code Online (Sandbox Code Playgroud)

}


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>
Run Code Online (Sandbox Code Playgroud)

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;
    }  
}
Run Code Online (Sandbox Code Playgroud)

pro*_*007 6

做这个:

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

AlertDialog dialog = builder.create();
dialog.show();
Run Code Online (Sandbox Code Playgroud)

代替:

AlertDialog show = new AlertDialog.Builder(this)
    .setTitle("Message")
    .setMessage(str)
    .setNeutralButton("OK", null)
    .show();
Run Code Online (Sandbox Code Playgroud)

您必须创建第一个实例AlertDialog.Builder.然后你可以建立Dialogbuilder.create().然后你可以显示Dialog.show().