相关疑难解决方法(0)

Android 1.6:"android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌null不适用于应用程序"

我正在尝试打开一个对话框窗口,但每次我尝试打开它都会抛出此异常:

Uncaught handler: thread main exiting due to uncaught exception
android.view.WindowManager$BadTokenException: 
     Unable to add window -- token null is not for an application
  at android.view.ViewRoot.setView(ViewRoot.java:460)
  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
  at android.app.Dialog.show(Dialog.java:238)
  at android.app.Activity.showDialog(Activity.java:2413)
Run Code Online (Sandbox Code Playgroud)

我是通过showDialog显示器的id 调用来创建的.该onCreateDialog处理器记录良好,我可以通过它没有问题的一步,但因为它看起来像我想的东西我已经把它贴吧:

@Override
public Dialog onCreateDialog(int id)
{
    Dialog dialog;
    Context appContext = this.getApplicationContext();
    switch(id)
    {
        case RENAME_DIALOG_ID:
            Log.i("Edit", "Creating rename dialog...");
            dialog = new Dialog(appContext);
            dialog.setContentView(R.layout.rename);
            dialog.setTitle("Rename " + noteName);
            break;
        default:
            dialog = null;
            break;
    }
    return dialog;      
}
Run Code Online (Sandbox Code Playgroud)

这有什么不足之处吗?有些问题在创建对话框时已经讨论过这个问题onCreate …

android runtimeexception android-dialog android-windowmanager

301
推荐指数
8
解决办法
19万
查看次数

何时调用活动上下文或应用程序上下文?

有很多关于这两种背景的帖子......但我仍然没有把它弄得恰到好处

据我所知到目前为止:每个都是它的类的一个实例,这意味着一些程序员建议你this.getApplicationContext()尽可能经常使用,以免"泄漏"任何内存.这是因为另一个this(获取Activity实例上下文)指向Activity每次用户倾斜手机或离开应用程序等时被销毁的内容.显然垃圾收集器(GC)没有捕获,因此使用太多内存..

但是,任何人都可以提出一些非常好的编码示例,它们是正确的使用this(获取当前Activity实例的上下文)并且应用程序上下文将是无用的/错误的吗?

android this android-context

259
推荐指数
5
解决办法
7万
查看次数

Android:ProgressDialog.show()与getApplicationContext崩溃

我似乎无法理解为什么会这样.这段代码:

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 progressdialog

108
推荐指数
6
解决办法
14万
查看次数

108
推荐指数
7
解决办法
9万
查看次数

如何获取我的活动背景?

我真的不明白这整个事情是如何工作的,所以如果我有一些类A需要B扩展的类的上下文,Activity我该如何获得该上下文?

我正在寻找一种比将上下文作为类A构造函数的参数更有效的方法.例如,如果类A将有数百万个实例,那么我们最终会有数百万个冗余指针,Context而我们应该能够以某种方式只有一个地方和一个getter函数......

android android-context

64
推荐指数
3
解决办法
21万
查看次数

SharedPreferences的应用程序上下文?

例如,我可以使用和开始ApplicationContext工作吗?SharedPreferencesRemoteService

从逻辑上讲,我认为我可以.这种方法有细微差别吗?

android sharedpreferences

19
推荐指数
3
解决办法
8756
查看次数

无法在未调用Looper.prepare()的线程上进行吐司

我尝试为我的Android应用程序运行测试,但我得到了这个跟踪.这是什么意思?

java.lang.RuntimeException: Can't toast on a thread that has not called Looper.prepare()
at android.widget.Toast$TN.<init>(Toast.java:390)
at android.widget.Toast.<init>(Toast.java:114)
at android.widget.Toast.makeText(Toast.java:277)
at android.widget.Toast.makeText(Toast.java:267)
at dev.android.gamex.CatchGame.onDraw(MainActivity.java:317)
at dev.android.gamex.JamieTest.useAppContext(JamieTest.java:45)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:58)
at …
Run Code Online (Sandbox Code Playgroud)

android

19
推荐指数
2
解决办法
1万
查看次数

android中有多少种类型的上下文,哪些更好用

我只想知道获取上下文的方法有多少,在哪种情况下使用哪种方法.哪一个更好用,以及它们之间的主要和关键因素是什么.

android android-context

15
推荐指数
1
解决办法
1万
查看次数

使用应用程序上下文到底意味着什么?

我是新手,如果这是一个非常愚蠢的问题,我很抱歉.我只想澄清一些事情.我的书说我可以使用该getApplicationContext()方法检索进程的应用程序上下文.我真的不知道在哪里键入这个或如何处理它.我可以进入层次结构,但我如何处理那里的所有脚本.另外我还会在main.xml中编写Activity Callbacks?练习要我在项目中添加日志标记,但我不知道该怎么做.确切的文字说:

"在onCreate()回调方法中,使用Log.i()方法添加信息日志消息."

另一个练习说:

"除了onCreate()之外,还实现了一些Activity回调方法,例如onStart().向每个回调方法添加一条日志消息,然后正常运行应用程序".

由于这些似乎是基本问题,有人可以帮助我.

我正在使用Android SDK和Eclipse.我已经创建了Hello World应用程序,但我不知道如何处理Context或Retrieving资源.请帮忙!

eclipse resources android android-context

11
推荐指数
1
解决办法
1万
查看次数

获取Context的更好方法是什么?

根据这个答案或android的文档,有几种方法可以在应用程序中获取Context并将其传递给其他类/方法/ anyuneed.

假设我在Foo活动中并且需要将上下文传递给Bar的构造函数.

Bar bar  = new Bar(Foo.this);
Bar bar2 = new Bar(this); //same as first i guess
Bar bar3 = new Bar(getApplicationContext());
Bar bar4 = new Bar(getBaseContext());
Bar bar5 = new Bar(MyApp.getContext); // get context statically 
Run Code Online (Sandbox Code Playgroud)

考虑到内存泄漏,速度,一般性能,所有这些可能性之间的更好方法是什么?

android android-context

11
推荐指数
1
解决办法
2407
查看次数