gpt4 book ai didi

java - 带有 BroadcastReceiver 的 AsyncTask 中的 onPostExecute 抛出空指针异常

转载 作者:行者123 更新时间:2023-12-01 11:37:21 24 4
gpt4 key购买 nike

当收到新消息时,应该将消息传递到互联网以供我进一步的业务逻辑。

为了接收新消息,我使用了broadcastreceiver的onReceive,并在后台处理互联网业务逻辑,我使用了AsyncTask。

我在 AyncTask 的 onPostExecute 方法中遇到空指针异常,我阅读了许多 stackoverflow 和其他网站解决方案并创建了接口(interface)并在 AsyncTask 扩展类构造函数中对其进行了初始化。但只得到空指针。

我的完整代码:

主要 Activity :

public class SmsActivity extends Activity implements     ParseURL.OnAsyncRequestComplete {
private static SmsActivity inst;
public static final String SMS_BUNDLE = "pdus";
public static SmsActivity instance() {
return inst;
}

@Override
public void onStart() {
super.onStart();
inst = this;
}

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

@Override
public void processResp(String output){
String outpu1 = output+" in main";
}

}

广播接收器:

public class SmsBroadcastReceiver extends BroadcastReceiver{
public static final String SMS_BUNDLE = "pdus";
public void onReceive(Context context, Intent intent) {
Bundle intentExtras = intent.getExtras();
if (intentExtras != null) {
Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
String smsMessageStr = "";

boolean rechargeResult = false;
for (int i = 0; i < sms.length; ++i) {
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);
String smsBody = smsMessage.getMessageBody().toString();
String address = smsMessage.getOriginatingAddress();
smsMessageStr += "SMS From: " + address + "\n";
if (smsBody != null) {
String[] splitValues = smsBody.split(" ");
if (splitValues != null && splitValues.length > 0) {
String siteURL = "SITE_URL";
try {
ParseURL.OnAsyncRequestComplete procesInterf = null;
ParseURL urlParse = new ParseURL(procesInterf);
Toast.makeText(context, siteURL, Toast.LENGTH_LONG).show();
new ParseURL(procesInterf).execute(new String[]{siteURL});
} catch (Exception e) {
Toast.makeText(context, "123 "+e.getMessage(), Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(context, "split values is null", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(context, "smsbody is null", Toast.LENGTH_LONG).show();
}
}
}
}
}

}

解析URL:

public class ParseURL extends AsyncTask<String, Void, String> {


ProgressDialog progressDialog;

OnAsyncRequestComplete caller;
//Context context;


public ParseURL(OnAsyncRequestComplete a) {
caller = a;
// context = a;
}

public interface OnAsyncRequestComplete {
public void processResp(String response);
}


@Override
protected void onPreExecute()
{
progressDialog.setMessage("WAIT...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setCancelable(false);
progressDialog.show();
}



@Override
protected String doInBackground(String... strings) {
String responseStatus = "";
try {
if(strings!=null) {
if (null != strings[0]) {
Document doc = Jsoup.connect(strings[0]).timeout(0).get();
if (doc != null) {
String result = doc.select("body").text();

if (null != result) {

if (result.toLowerCase().contains("FAILED".toLowerCase())) {
responseStatus = result;
} else if (result.toLowerCase().contains("SUCCESS".toLowerCase())) {
responseStatus = "SUCCESS";
} else {
responseStatus = "FAILED";
}
} else {
responseStatus = "google";
}
} else {
responseStatus = "facebook";
}
} else {
responseStatus = "youtube";
}
}else{
responseStatus = "ebay";
}
} catch (Throwable t) {
t.printStackTrace();
}
return responseStatus;
}


@Override
protected void onPostExecute(String s) {
caller.processResp(s);
}

}

我尝试了许多在 stackoverflow 和其他站点中共享的解决方案。但我无法解决它。请不要将此标记为重复。

提前致谢。

最佳答案

哦,天哪...

ParseURL.OnAsyncRequestComplete procesInterf = null;
ParseURL urlParse = new ParseURL(procesInterf);
<小时/>
   public ParseURL(OnAsyncRequestComplete a) {
caller = a;
}
<小时/>
@Override
protected void onPostExecute(String s) {
caller.processResp(s);
}

你看到错误了吗?

您将 null 传递给 ParseUrl 构造函数,因此 PosteExecute() 尝试调用 null 回调的方法。

我怀疑你想这样做

ParseURL.OnAsyncRequestComplete procesInterf = SmsActivity.this;

但如果您的 SmsBroadcastReceiver 类是 SmsActivity 的内部类,它就会起作用。

关于java - 带有 BroadcastReceiver 的 AsyncTask 中的 onPostExecute 抛出空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29833350/

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