SUN
SUN

Reputation: 973

Android app failed to launch

I am trying to hide title bar of my app but if I try to add following code & while launching it immediately stops & shows Unfortunately app(app name) has stopped. If I remove following line from code then it works.

Following code I am using onCreate() Method

this.requestWindowFeature(Window.FEATURE_NO_TITLE);

I just want to hide title bar. Is there anything wrong with code

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");
    }

UPDATE ERROR

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.

Upvotes: 0

Views: 142

Answers (3)

Pramod Waghmare
Pramod Waghmare

Reputation: 1283

Try this,

In your '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");
    }
}

And in android manifest

select / set theme as 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>

Upvotes: 1

LuXoR
LuXoR

Reputation: 53

Have a look at your error stack trace:

Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content

You have to call requestWindowFeature before the setContentView()

so your problem is the following line of your code this.requestWindowFeature(Window.FEATURE_NO_TITLE);

it needs to be called before
setContentView(R.layout.activity_main);

so to say like this:

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 - I just wannted it to be more clear how get rid of future problems ;-)

Upvotes: 0

Nongthonbam Tonthoi
Nongthonbam Tonthoi

Reputation: 12953

You have to call requestWindowFeature before the setContentView()

Like this:

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");
}

Upvotes: 1

Related Questions