java 错误:找不到符号:方法 startActivity

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10099600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:37:14  来源:igfitidea点击:

Error: cannot find symbol: method startActivity

javaandroidandroid-intent

提问by user1325591

I am developing an application in order to connect to Google Navigator by the following code..

我正在开发一个应用程序,以便通过以下代码连接到 Google Navigator。

import android.app.Activity; 
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

public static void Call_GoogleMapsNavigation(int longitud,int latitud)
{
Intent i = new Intent(android.content.Intent.ACTION_VIEW, 
Uri.parse("google.navigation:q=" +latitud+ ","+longitud+"")); 
Context.startActivity(i); 
}

... but I get the following error:

...但我收到以下错误:

Error returned:

错误返回:

GWDCPSET_GlobalProcedures_MobileDevice.java:1223: cannot find symbol
symbol : method startActivity(android.content.Intent)
location: class antay.cfsatv30.wdgen.GWDCPSET_GlobalProcedures_MobileDevice
startActivity(i); 
^

I can not find the solution to the problem ...

我找不到问题的解决方案......

Thank you very much,

非常感谢你,

回答by ρяσ?ρ?я K

Try this way :

试试这个方法:

Context oContext;
oContext= mContext;
Intent i = new Intent(android.content.Intent.ACTION_VIEW, 
Uri.parse("google.navigation:q=" + latitud+ "," + longitud));
oContext.startActivity(i);

回答by ChrisCarneiro

To explain a bit further the answer above. What it means is that you can't/shouldn't not call statically startActivity(intent).

进一步解释一下上面的答案。这意味着你不能/不应该静态调用startActivity(intent).

Context.startActivity(intent); //wrong notice capital 'C'

You need a Context instance.

您需要一个 Context 实例。

So, the simplest thing to do, is add a parameter to your static method: (Notice lowercase 'c' as a convention for method names in Java)

因此,最简单的做法是向静态方法添加一个参数:( 注意小写的“c”作为 Java 中方法名称的约定)

public static void call_GoogleMapsNavigation(final Context context, int longitud,int latitud) { 

        ...
        context.startActivity(i); //right
}

So for example in your activity or any component in your app that holds a reference to a context instance, you call your method as follows

因此,例如在您的 Activity 或应用程序中包含对上下文实例的引用的任何组件中,您可以按如下方式调用您的方法

For simplicity I assume you call it from an activity(always holds a reference to a context):

为简单起见,我假设您从活动中调用它(始终持有对上下文的引用):

MainActivity extends AppCompatActivity { 

    OnCreate(Bundle savedInstance) {
        <YourHelperClass>.callGoogleMapsNavigation(this, 23, 44); //static call
    }
}

Hope this helps :)

希望这可以帮助 :)