Java 单击应用程序时在浏览器中打开URL地址:Can';t解析符号“;主要活动“;

Java 单击应用程序时在浏览器中打开URL地址:Can';t解析符号“;主要活动“;,java,android,url,web,Java,Android,Url,Web,我正在尝试创建一个android应用程序,当它被点击时会打开一些URL地址。我知道在类似的主题上有几个问题,但我无法为此创建一个应用程序。在尝试应用程序后,出现了一些错误,因此我真的需要您的帮助 在AndroidManifest中,我得到一个错误: -无法解析符号“MainActivity” 在XML AndroidManifest中是这样的代码: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:androi

我正在尝试创建一个android应用程序,当它被点击时会打开一些URL地址。我知道在类似的主题上有几个问题,但我无法为此创建一个应用程序。在尝试应用程序后,出现了一些错误,因此我真的需要您的帮助

在AndroidManifest中,我得到一个错误: -无法解析符号“MainActivity”

在XML AndroidManifest中是这样的代码:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="test222.intenrt2" >
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

在MainActivity中,我得到一个错误: -预期识别器

-无效的方法删除;需要返回类型

-缺少方法或声明摘要

-未知类:'BrowserContent'

-开始活动(浏览内容)

在MainActivity.Java中就是这个

package test222.intenrt2;

import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Application;
import android.test.ApplicationTestCase;
import android.content.Intent;

public class ApplicationTest extends ApplicationTestCase<Application> {
    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));

    startActivity(browserIntent);
}
包test222.intent2;
导入android.net.Uri;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.app.Application;
导入android.test.ApplicationTestCase;
导入android.content.Intent;
公共类ApplicationTest扩展了ApplicationTestCase{
Intent browserIntent=新意图(Intent.ACTION\u视图,Uri.parse(“http://www.google.com"));
startActivity(浏览器内容);
}

您的主要活动应该是扩展活动

然后将代码放在onCreate方法中(它就在类声明中,而不是任何方法中…)


希望这能有所帮助。

您甚至似乎没有MainActivity类。 您应该创建一个新类并将其命名为MainActivity,从ActivityAppCompatActivity扩展它(如果您正在使用支持库),在新创建的类重写onCreate方法中,并将代码移到其中。 最后一段代码应该是这样的

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));

    startActivity(browserIntent);
    }
}

谢谢你的帮助,我明天早上会试试。谢谢你,它成功了!如果您知道页面何时打开,应用程序将关闭,您是否可以这样做?我很高兴它对您有效,为了关闭您的应用程序,您应该完成所有运行的活动,因为在
onCreate()
方法的末尾,您只有一个简单的调用
finish()