android显示来自非UI或非活动类的警报对话框

编程入门 行业动态 更新时间:2024-10-21 09:23:59
本文介绍了android显示来自非UI或非活动类的警报对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

Android程序员,

Hi Android programmers,

此问题是先前提出的.但是没有答案.所以我需要解决方案.实际上,我正在尝试显示从test.java调用的Mainactivity.java警报框(非活动状态).

This question is previously asked. But there is no answer. So i need solution for that. Actually i am trying to display alert box of Mainactivity.java calling from test.java(Non-activity).

如果它们不是UI编码,则可以正常工作.如果该代码依赖于UI,则表示其抛出错误.

Thats working fine if their is no UI coding. if that code is dependent with UI means its throw errors.

活动类别:

public class Mainactivity extends Activity { public void message() { Log.i("Success : Call from non-UI & non activity class");//Upto this line working if i called message() function from other non-activity class DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { switch (which) { //Code } } }; AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("FMS Status Message :- "); builder.setMessage(msg) .setPositiveButton("OK",dialogClickListener).show(); } }

非活动类:

public class test extends BroadcastReceiver { Mainactvity in = new Mainactvity(); @Override public void onReceive(Context context, Intent intent) { in.message(); } }

如果解决了,那么我项目的主要目标将完成.请你们中的任何一个帮助我.

If it is resolved then my main objective of my project will be completed. Please any one of you help me.

谢谢.

推荐答案

首先,Android的不良编程实践,

First of All, Bad Programming Practice for Android,

Mainactvity in = new Mainactvity();

您不能创建Activity的构造方法类.

You can not make a Constructor of Activity class.

第二,您尝试在BroadcastReceiver中的未显示Activity的Dialog中显示UI元素.使用Activity的 Context (如果与UI有关)或Application来在BroadcastReceiver中显示对话框.

And second, You are trying to display UI elements in Dialog which has not reference of Activity, in BroadcastReceiver. Use Context of Activity (If it concern with UI) or Application for displaying Dialog in BroadcastReceiver.

更多推荐

android显示来自非UI或非活动类的警报对话框

本文发布于:2023-11-26 18:06:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634619.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:警报   对话框   或非   android   UI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!