当我不能使用“this”作为上下文时如何使用Toast

时间:2011-04-12 20:16:31

标签: java android toast

我有一个位置监听器活动,我想要做吐司通知。但它不会让我通过this作为上下文。我该怎样做吐司?

7 个答案:

答案 0 :(得分:20)

如果吐司位于您的活动类中,您可以YourActiviy.this YourActivity是类名。如果它在你的课外,你需要得到你的活动上下文(在构造函数中传递它等)。

答案 1 :(得分:6)

如果您在内部班级,那么也尝试这个

  

getApplicationContext()

答案 2 :(得分:5)

您可以使用NameOfYourActivity.this

例如:

public class MyActivity extends Activity {

 ...
     Toast.makeText(MyActivity.this, text, duration).show();

答案 3 :(得分:2)

字段变量:12:02:43.508 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] FAILURE: Build failed with an exception. 12:02:43.508 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 12:02:43.508 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] * What went wrong: 12:02:43.508 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] Execution failed for task ':android-nova-main:lintDebug'. 12:02:43.508 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] > java.lang.StackOverflowError (no error message) 12:02:43.508 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 12:02:43.508 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] * Exception is: 12:02:43.511 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':android-nova-main:lintDebug'. 12:02:43.511 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:84) 12:02:43.511 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:55) 12:02:43.511 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:62) 12:02:43.511 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:58) 12:02:43.511 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:88) 12:02:43.511 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at org.gradle.api.internal.tasks.execution.ResolveTaskArtifactStateTaskExecuter.execute(ResolveTaskArtifactStateTaskExecuter.java:46) 12:02:43.511 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:51) 12:02:43.511 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:54) 12:02:43.511 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43) 12:02:43.511 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:34) 12:02:43.511 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker$1.execute(DefaultTaskGraphExecuter.java:236) 12:02:43.512 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker$1.execute(DefaultTaskGraphExecuter.java:228) 12:02:43.512 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at org.gradle.internal.Transformers$4.transform(Transformers.java:169) 12:02:43.512 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:106) 12:02:43.512 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:61) 12:02:43.512 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:228) 12:02:43.512 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:215) 12:02:43.512 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.processTask(AbstractTaskPlanExecutor.java:77) 12:02:43.512 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.run(AbstractTaskPlanExecutor.java:58) 12:02:43.512 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63) 12:02:43.512 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at org.gradle.internal.concurrent.StoppableExecutorImpl$1.run(StoppableExecutorImpl.java:46) 12:02:43.512 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 12:02:43.512 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 12:02:43.513 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at java.lang.Thread.run(Thread.java:745) 12:02:43.513 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] Caused by: java.lang.StackOverflowError 12:02:43.513 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding.readableName(ReferenceBinding.java:1635) 12:02:43.513 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding.debugName(ReferenceBinding.java:903) 12:02:43.513 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at org.eclipse.jdt.internal.compiler.lookup.MethodBinding.toString(MethodBinding.java:1262) 12:02:43.513 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at com.android.tools.lint.EcjParser$EcjResolvedMethod.getSignature(EcjParser.java:1797) 12:02:43.513 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at com.squirrel_explorer.eagleeye.lint.b.a.d.visitMethodInvocation(ProGuard:27) 12:02:43.513 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at lombok.ast.MethodInvocation.accept(MethodInvocation.java:114) 12:02:43.513 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at lombok.ast.MethodInvocation.accept(MethodInvocation.java:115) 12:02:43.513 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at lombok.ast.VariableDefinitionEntry.accept(VariableDefinitionEntry.java:105) 12:02:43.513 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at lombok.ast.VariableDefinition.accept(VariableDefinition.java:155) 12:02:43.513 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at lombok.ast.VariableDeclaration.accept(VariableDeclaration.java:103) 12:02:43.513 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at lombok.ast.Block.accept(Block.java:106) 12:02:43.514 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at lombok.ast.MethodDeclaration.accept(MethodDeclaration.java:240) 12:02:43.514 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at lombok.ast.NormalTypeBody.accept(NormalTypeBody.java:57) 12:02:43.514 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at lombok.ast.ClassDeclaration.accept(ClassDeclaration.java:239) 12:02:43.514 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at com.squirrel_explorer.eagleeye.lint.a.a.l.a(ProGuard:109) 12:02:43.514 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at com.squirrel_explorer.eagleeye.lint.a.a.l.a(ProGuard:127) 12:02:43.514 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at com.squirrel_explorer.eagleeye.lint.a.a.l.a(ProGuard:127) 12:02:43.514 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at com.squirrel_explorer.eagleeye.lint.a.a.l.a(ProGuard:127) 12:02:43.514 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at com.squirrel_explorer.eagleeye.lint.a.a.l.a(ProGuard:127) 12:02:43.514 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at com.squirrel_explorer.eagleeye.lint.a.a.l.a(ProGuard:127) 12:02:43.514 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at com.squirrel_explorer.eagleeye.lint.a.a.l.a(ProGuard:127) 12:02:43.514 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at com.squirrel_explorer.eagleeye.lint.a.a.l.a(ProGuard:127) 12:02:43.514 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at com.squirrel_explorer.eagleeye.lint.a.a.l.a(ProGuard:127) 12:02:43.514 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at com.squirrel_explorer.eagleeye.lint.a.a.l.a(ProGuard:127) 12:02:43.515 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at com.squirrel_explorer.eagleeye.lint.a.a.l.a(ProGuard:127)

在OnCreate中:Context context;

Xamarin / C#语法: context = this;

Android / Java语法: Toast.MakeText(context, "your message", ToastLength.Long).Show();

答案 4 :(得分:1)

例如,如果你的侦听器里面有一个名为“onComplete”的方法,那么这段代码应该可以工作。

public void onComplete(String response, Object state) {
        final String response_complete = response;
        MyActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(MyActivity.this, text, duration).show();
            }
        });
    }

应该这样做。

答案 5 :(得分:0)

听起来你在活动的内部课堂。如果是这种情况,请尝试ActivityName.this。

答案 6 :(得分:0)

而是尝试getApplicationContext()

 Toast tea = Toast.makeText(getApplicationContext(), "Send", Toast.LENGTH_LONG);
 tea.show();