gpt4 book ai didi

java - AlertDialog show = new AlertDialog.Builder(this) 未定义

转载 作者:太空狗 更新时间:2023-10-29 15:55:32 25 4
gpt4 key购买 nike

应用拦截短信并显示消息的对话框

但是我无法在我的 Test 类中解决我的 Dialog 错误。我做错了什么?

(我还包含了我的其他 2 个文件)。

Eclipse 中显示错误:AlertDialog.Builder(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>

你好.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;
}
}

最佳答案

这样做:

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

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

代替:

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

您必须先创建一个 AlertDialog.Builder 的实例。然后您可以使用 builder.create() 构建 Dialog。然后您可以使用 .show() 显示 Dialog

关于java - AlertDialog show = new AlertDialog.Builder(this) 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11585099/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com