Android应用无法启动

时间:2016-06-10 07:57:35

标签: android webview

我正在尝试隐藏我的应用的标题栏但是如果我尝试添加以下代码&发射时立即停止&显示不幸的应用程序(应用程序名称)已停止如果我从代码中删除以下行,则它可以正常工作。

以下代码我正在使用onCreate()方法

this.requestWindowFeature(Window.FEATURE_NO_TITLE);

我只想隐藏标题栏。代码

有什么问题吗?
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mWebView = (WebView) findViewById(R.id.activity_main_webview);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.loadUrl("http://google.com");
    }

更新错误

Error:Error: A fatal exception has occurred. Program will exit.
Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: java.lang.RuntimeException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.7.0_79\bin\java.exe'' finished with non-zero exit value 1
Error:Error: Could not create the Java Virtual Machine.

3 个答案:

答案 0 :(得分:1)

您必须在requestWindowFeature

之前致电setContentView()

像这样:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);
    mWebView = (WebView) findViewById(R.id.activity_main_webview);
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    mWebView.loadUrl("http://google.com");
}

答案 1 :(得分:1)

试试这个,

在您的' MainActivity.java'

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView mWebView = (WebView) findViewById(R.id.activity_main_webview);
        mWebView.loadUrl("http://www.cnn.com");
    }
}

并在android manifest

选择/设置主题为NoTitleBar

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/Theme.AppCompat.NoActionBar">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"></action>
            <category android:name="android.intent.category.LAUNCHER"></category>
        </intent-filter>
    </activity>
</application>

答案 2 :(得分:0)

查看错误堆栈跟踪:

  

引起:android.util.AndroidRuntimeException:在添加内容之前必须调用requestFeature()

您必须在setContentView()

之前调用requestWindowFeature

所以您的问题是代码的以下行 this.requestWindowFeature(Window.FEATURE_NO_TITLE);

之前需要调用它
setContentView(R.layout.activity_main);

这样说:

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); mWebView = (WebView) findViewById(R.id.activity_main_webview); WebSettings webSettings = mWebView.getSettings(); webSettings.setJavaScriptEnabled(true); mWebView.loadUrl("http://google.com"); }

thx to Nongthonbam Tonthoi - 我只是想要更清楚地了解如何摆脱未来的问题; - )