AlertDialog秀=新AlertDialog.Builder(这)是未定义 [英] AlertDialog show = new AlertDialog.Builder(this) is Undefined
问题描述
该应用程序拦截短信,并显示一个对话框
的消息。
但我不能让我的测试
类解决了我的对话框
错误。我在做什么错了?
(我也包括我的其他2个文件)。
在Eclipse中所示错误: AlertDialog.Builder(测试)未定义
test.java
包com.example.firstapp;进口android.app.AlertDialog;
进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.os.Bundle;
进口android.telephony.SmsMessage;公共类测试扩展广播接收器
{
@覆盖
公共无效的onReceive(上下文的背景下,意图意图)
{
// ---获得通过SMS消息---
捆绑包= intent.getExtras();
SmsMessage []封邮件= NULL;
字符串str =;
如果(捆绑!= NULL)
{
// ---检索收到的短信---
[对象]的PDU =(对象[])bundle.get(的PDU);
封邮件=新SmsMessage [pdus.length]
的for(int i = 0; I< msgs.length;我++){
封邮件[I] = SmsMessage.createFromPdu((字节[])的PDU [I]);
STR + =短信来自+封邮件[I] .getOriginatingAddress();
STR + =;
STR + =封邮件[I] .getMessageBody()的toString()。
STR + =\\ n;
} AlertDialog秀=新AlertDialog.Builder(本)
.setTitle(信息)
.setMessage(STR)
.setNeutralButton(OK,NULL)
。显示();
}
}
}
的AndroidManifest.xml
<清单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.example.firstapp
安卓版code =1
机器人:=的versionName1.0> <用途-SDK
安卓的minSdkVersion =9
机器人:targetSdkVersion =15/>
<使用许可权的android:NAME =android.permission.SEND_SMS>
< /使用许可权>
<使用许可权的android:NAME =android.permission.RECEIVE_SMS>
< /使用许可权>
<应用
机器人:图标=@绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME
机器人:主题=@风格/ AppTheme>
<活动
机器人:你好NAME =
机器人:标签=@字符串/ title_activity_hello>
&所述;意图滤光器>
<作用机器人:名字=android.intent.action.MAIN/> <类机器人:名字=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
<接收机器人:测试NAME =>
&所述;意图滤光器>
<作用机器人:名字=
android.provider.Telephony.SMS_RECEIVED/>
&所述; /意图滤光器>
< /接收器>
< /用途>< /清单>
hello.java
包com.example.firstapp;进口android.app.Activity;
进口android.os.Bundle;
进口android.view.Menu;公共类您好延伸活动{ @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_hello);
} @覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
。getMenuInflater()膨胀(R.menu.activity_hello,菜单);
返回true;
}
}
做到这一点:
AlertDialog.Builder建设者=新AlertDialog.Builder(背景);
builder.setTitle(信息)
.setMessage(STR)
.setNeutralButton(OK,NULL);AlertDialog对话框= builder.create();
dialog.show();
而不是:
AlertDialog秀=新AlertDialog.Builder(本)
.setTitle(信息)
.setMessage(STR)
.setNeutralButton(OK,NULL)
。显示();
您必须 AlertDialog.Builder
首先创建一个实例。然后,你可以建立对话框
与 builder.create()
。然后你就可以显示对话框
与 .show()
。
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;
}
}
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()
.
这篇关于AlertDialog秀=新AlertDialog.Builder(这)是未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!