我似乎无法理解为什么会这样.这段代码:
mProgressDialog = ProgressDialog.show(this, "", getString(R.string.loading), true);
Run Code Online (Sandbox Code Playgroud)
工作得很好.但是,这段代码:
mProgressDialog = ProgressDialog.show(getApplicationContext(), "", getString(R.string.loading), true);
Run Code Online (Sandbox Code Playgroud)
抛出以下异常:
W/WindowManager( 569): Attempted to add window with non-application token WindowToken{438bee58 token=null}. Aborting.
D/AndroidRuntime( 2049): Shutting down VM
W/dalvikvm( 2049): threadid=3: thread exiting with uncaught exception (group=0x4001aa28)
E/AndroidRuntime( 2049): Uncaught handler: thread main exiting due to uncaught exception
E/AndroidRuntime( 2049): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tastekid.TasteKid/com.tastekid.TasteKid.YouTube}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
E/AndroidRuntime( 2049): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
E/AndroidRuntime( 2049): at …Run Code Online (Sandbox Code Playgroud) 所以在android中我想让我的应用程序类成为单例.
像这样:
object MyApplication: Application(){}
Run Code Online (Sandbox Code Playgroud)
不行.在运行时抛出以下错误:
java.lang.IllegalAccessException: private com....is not accessible from class android.app.Instrumentation.
Run Code Online (Sandbox Code Playgroud)
这样做也是不可能的:
class MyApp: Application() {
private val instance_: MyApp
init{
instance_ = this
}
override fun onCreate() {
super.onCreate()
if (BuildConfig.DEBUG) {
Timber.plant(Timber.DebugTree());
}
}
companion object{
fun getInstance() = instance_
}
}
Run Code Online (Sandbox Code Playgroud)
那么如何才能在我的应用程序中随处获得应用程序类的实例,MyApp.instance()而不是使用(applicationContext as MyApp).
还解释了为什么我想要这个:我在我的应用程序中有类,例如,使用上下文初始化的SharedPreference Singleton,并且作为它的单例,不能有参数.
我的问题类似于这个2岁的问题,我只是发布相同的问题来获得更新的答案,因为很多事情在两年内发生了变化.
我正在开发一个GingerBread +设备的应用程序,我有很多活动,在后台我收到服务器的一些数据.现在基于该数据在某些情况下我需要向Dialog用户显示一个.问题是我如何知道当前最活跃的活动?
我试过,
我曾试图给getApplicationContext()而Dialog创作,但是不工作.抛出一些例外.
一个办法 ?(我真的讨厌它),
一个解决方案可能是通过在Application类中使用变量并在onResume()每个活动上设置它来跟踪当前可见的活动.如果他们有更明智的方法来实现这一点,我真的不想做这本书,我相信他们是更明智的方法来实现这一目标,
我的简单问题是,
如何在当前可见活动上显示对话框?,所以我可以提供对AlertDialog.Builder的引用,我认为这将完成我的工作.如果不是我怎么能在最顶层的Activity上显示一个对话框?
编辑,我使用以下代码私有View.OnClickListener创建一个简单的对话框cancelClickListener = new OnClickListener(){
@Override
public void onClick(View v) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
LoginActivity.this);
// set title
alertDialogBuilder.setTitle("Roobroo will exit..");
// set dialog message
alertDialogBuilder
.setMessage("Are you sure you want to exit ?")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// if this button is clicked, close
// …Run Code Online (Sandbox Code Playgroud) 我的应用程序在后台,我使用的服务在满足某个条件时发送广播:
if(send)
{
Intent intent = new Intent(Tags.MSG_BROADCAST_ID);
intent.putExtra(Tags.MESSAGE, msg);
Log.w(Tags.DEBUG,"Broadcast");
sendBroadcast(intent);
}
}
Run Code Online (Sandbox Code Playgroud)
我的广播接收器获得广播,它应该显示一个警告对话框.我用这个:
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
if (bundle != null && Globals.MainActivity != null)
{
msg = bundle.getString(Tags.MESSAGE);
Globals.MainActivity.ShowMessage(msg);
}
}
Run Code Online (Sandbox Code Playgroud)
当我的主要活动位于前台时,警报会正确显示.当它在后台时,什么都看不见.
Runnable runnable = new Runnable()
{
public void run()
{
KeyguardManager km = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
final KeyguardManager.KeyguardLock kl = km.newKeyguardLock("My_App");
kl.disableKeyguard();
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.FULL_WAKE_LOCK, "My_App");
wl.acquire();
AlertDialog.Builder alertDialogBuilder = new …Run Code Online (Sandbox Code Playgroud)