-1

I just started to learn android app development. I try to create a very simple app which just redirects the user to our homepage in fullscreen mode.

But if I start the app, then a black window shows for less than a half second and the app closes.

I have no clue how to debug/fix this, I hope someone can help a beginner out! Thx!

app\src\main\res\layout\activity_fullscreen.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0099cc"
    tools:context=".FullscreenActivity">

    <!-- The primary full-screen view. This can be replaced with whatever view
         is needed to present your content, e.g. VideoView, SurfaceView,
         TextureView, etc. -->
    <WebView android:id="@+id/botecView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:keepScreenOn="true"
        android:textColor="#33b5e5"
        android:textStyle="bold"
        android:textSize="50sp"
        android:gravity="center"
        android:text="@string/dummy_content" />

    <!-- This FrameLayout insets its children based on system windows using
         android:fitsSystemWindows. -->
    <FrameLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <LinearLayout android:id="@+id/fullscreen_content_controls"
            style="?metaButtonBarStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|center_horizontal"
            android:background="@color/black_overlay"
            android:orientation="horizontal"
            tools:ignore="UselessParent">

        </LinearLayout>
    </FrameLayout>
</FrameLayout>

app\src\main\java\com\example\botec\FullscreenActivity.java

package com.example.botec;

import android.annotation.SuppressLint;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebView;
import android.util.Log;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 */
public class FullscreenActivity extends AppCompatActivity {
    /**
     * Whether or not the system UI should be auto-hidden after
     * {@link #AUTO_HIDE_DELAY_MILLIS} milliseconds.
     */
    private static final boolean AUTO_HIDE = true;

    /**
     * If {@link #AUTO_HIDE} is set, the number of milliseconds to wait after
     * user interaction before hiding the system UI.
     */
    private static final int AUTO_HIDE_DELAY_MILLIS = 3000;

    /**
     * Some older devices needs a small delay between UI widget updates
     * and a change of the status and navigation bar.
     */
    private static final int UI_ANIMATION_DELAY = 300;

    private View mContentView;
    private View mControlsView;
    private boolean mVisible;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_fullscreen);

        mVisible = true;

        // Set up the user interaction to manually show or hide the system UI.

        // Upon interacting with UI controls, delay any scheduled hide()
        // operations to prevent the jarring behavior of controls going away
        // while interacting with the UI.
        //findViewById(R.id.dummy_button).setOnTouchListener(mDelayHideTouchListener);

        WebView botecView = findViewById(R.id.botecView);
        botecView.getSettings().setJavaScriptEnabled(true);
        botecView.loadUrl("https://www.botec.de");
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        // Trigger the initial hide() shortly after the activity has been
        // created, to briefly hint to the user that UI controls
        // are available.
        delayedHide(100);
    }

    /**
     * Touch listener to use for in-layout UI controls to delay hiding the
     * system UI. This is to prevent the jarring behavior of controls going away
     * while interacting with activity UI.
     */
    private final View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if (AUTO_HIDE) {
                delayedHide(AUTO_HIDE_DELAY_MILLIS);
            }
            return false;
        }
    };

    private void toggle() {
        if (mVisible) {
            hide();
        } else {
            show();
        }
    }

    private void hide() {
        // Hide UI first
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        mControlsView.setVisibility(View.GONE);
        mVisible = false;

        // Schedule a runnable to remove the status and navigation bar after a delay
        mHideHandler.removeCallbacks(mShowPart2Runnable);
        mHideHandler.postDelayed(mHidePart2Runnable, UI_ANIMATION_DELAY);
    }

    private final Runnable mHidePart2Runnable = new Runnable() {
        @SuppressLint("InlinedApi")
        @Override
        public void run() {
            // Delayed removal of status and navigation bar

            // Note that some of these constants are new as of API 16 (Jelly Bean)
            // and API 19 (KitKat). It is safe to use them, as they are inlined
            // at compile-time and do nothing on earlier devices.
            mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        }
    };

    @SuppressLint("InlinedApi")
    private void show() {
        // Show the system bar
        mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
        mVisible = true;

        // Schedule a runnable to display UI elements after a delay
        mHideHandler.removeCallbacks(mHidePart2Runnable);
        mHideHandler.postDelayed(mShowPart2Runnable, UI_ANIMATION_DELAY);
    }

    private final Runnable mShowPart2Runnable = new Runnable() {
        @Override
        public void run() {
            // Delayed display of UI elements
            ActionBar actionBar = getSupportActionBar();
            if (actionBar != null) {
                actionBar.show();
            }
            mControlsView.setVisibility(View.VISIBLE);
        }
    };

    private final Handler mHideHandler = new Handler();
    private final Runnable mHideRunnable = new Runnable() {
        @Override
        public void run() {
            hide();
        }
    };

    /**
     * Schedules a call to hide() in delay milliseconds, canceling any
     * previously scheduled calls.
     */
    private void delayedHide(int delayMillis) {
        mHideHandler.removeCallbacks(mHideRunnable);
        mHideHandler.postDelayed(mHideRunnable, delayMillis);
    }
}

Logcat (empty):

enter image description here

Black
  • 18,150
  • 39
  • 158
  • 271
  • "I just started to learn android app development", "I have no clue how to debug this", Hint: use **Logcat**. – Geno Chen Jan 21 '19 at 13:58
  • Ok, I get `7847-7847/com.huawei.hwdetectrepair E/SmartNotifyService: intent null in onStartCommand`. I have no clue where the onStartCommand is located – Black Jan 21 '19 at 14:00
  • 1
    This seems not related to **your** current application. Hint: use **filter** to find the logcat generated by _current_ application, and catch the most important error generated at _current_ crash realtime. Then you may edit the question to show the logcat. – Geno Chen Jan 21 '19 at 14:04
  • @GenoChen how do I know if it is related to my current application or not? – Black Jan 21 '19 at 14:07
  • 1
    Set the filter condition, produce the crash, capture the logcat generated during crash on time, use it to debug, or use it to ask others to help you. – Geno Chen Jan 21 '19 at 14:08
  • @GenoChen ok I undertand you fully. there is no log output at all. – Black Jan 21 '19 at 14:09
  • 1
    You can choose "show current application" (just this meaning, maybe not precise text) in the filter condition, lies in the top right corner in logcat window in Android Studio. – Geno Chen Jan 21 '19 at 14:10
  • @GenoChen, Yes thanks, but there is no log output at all, even on verbose. – Black Jan 21 '19 at 14:11
  • It is very very very strange to have no logcat. Have you connected that device to Android Studio (actually, the tool `adb` in Android SDK)? And did you choose the right device and the right application in the top left corner of logcat window in Android Studio? – Geno Chen Jan 21 '19 at 14:13
  • @GenoChen, yes, look at the screenshot in my question. – Black Jan 21 '19 at 14:14
  • You choose the wrong application in the top left window. Not `com.huawei.detect...`, but (very possible) `com.example.botec`. – Geno Chen Jan 21 '19 at 14:15
  • @GenoChen, this is the only available option. – Black Jan 21 '19 at 14:16
  • Ouch... Is your `packageName` (in `src/build.gradle`) defined as `com.huawei.detect...`? I think this is nearly impossible. But why you can't see your current application leads to a very strange problem. Would you try to run this application again **via Run button in Android Studio**, and **keep the logcat window opened**, to see if the current application changes? (And the crash logcat may occur, if everything are running as expected). – Geno Chen Jan 21 '19 at 14:21
  • @GenoChen, there is no folder called `src` – Black Jan 21 '19 at 14:25
  • 1
    Sorry for my failure. It may be `app/build.gradle` or `app/src/build.gradle`, I forgot that. But this maybe not so important at this time. Just a small guess. – Geno Chen Jan 21 '19 at 14:27
  • Logcat -> dropdown in the right select : Error. Then post a screenshot of the problem that causes. – Elio Lako Jan 21 '19 at 14:29

2 Answers2

0

I have two options for the error that you've showed in the comments: 7847-7847/com.huawei.hwdetectrepair E/SmartNotifyService: intent null in onStartCommand

First one:

You should extend your check to this:

if(intent != null && intent.getAction() != null) {

If you want to add an Action to your intent you need to call [setAction()][1]:

Intent i = new Intent(this, MainService.class);
i.setAction("foo");
startService(i);

Second one:

Just try adding this simple check :

intent.getAction() != null

Hope that helps. Let me know :)

clauub
  • 1,134
  • 9
  • 19
  • Hi @android, thx for your answer. Where is this code located at? I cant find the word `intent` in my code? I just started a new empty fullscreen project, I don't believe that it has already bugs? – Black Jan 21 '19 at 14:46
0

Ok, it seems like that there was unneeded code. I changed it to this:

app\src\main\res\layout\activity_fullscreen.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0099cc"
    tools:context=".FullscreenActivity">

    <!-- The primary full-screen view. This can be replaced with whatever view
         is needed to present your content, e.g. VideoView, SurfaceView,
         TextureView, etc. -->
    <WebView android:id="@+id/botecView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:keepScreenOn="true"
        android:gravity="center" />
</FrameLayout>

app\src\main\java\com\example\botec\FullscreenActivity.java

package com.example.botec;

import android.annotation.SuppressLint;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebView;
import android.util.Log;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 */
public class FullscreenActivity extends AppCompatActivity {
    /**
     * Whether or not the system UI should be auto-hidden after
     * {@link #AUTO_HIDE_DELAY_MILLIS} milliseconds.
     */
    private static final boolean AUTO_HIDE = true;

    /**
     * If {@link #AUTO_HIDE} is set, the number of milliseconds to wait after
     * user interaction before hiding the system UI.
     */
    private static final int AUTO_HIDE_DELAY_MILLIS = 3000;

    /**
     * Some older devices needs a small delay between UI widget updates
     * and a change of the status and navigation bar.
     */
    private static final int UI_ANIMATION_DELAY = 300;
    private boolean mVisible;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_fullscreen);

        mVisible = true;

        WebView botecView = findViewById(R.id.botecView);
        botecView.getSettings().setJavaScriptEnabled(true);
        botecView.loadUrl("https://www.botec.de");
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        // Trigger the initial hide() shortly after the activity has been
        // created, to briefly hint to the user that UI controls
        // are available.
        delayedHide(100);
    }

    /**
     * Touch listener to use for in-layout UI controls to delay hiding the
     * system UI. This is to prevent the jarring behavior of controls going away
     * while interacting with activity UI.
     */
    private final View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if (AUTO_HIDE) {
                delayedHide(AUTO_HIDE_DELAY_MILLIS);
            }
            return false;
        }
    };

    private void toggle() {
        if (mVisible) {
            hide();
        } else {
            show();
        }
    }

    private void hide() {
        // Hide UI first
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        mVisible = false;

        // Schedule a runnable to remove the status and navigation bar after a delay
        mHideHandler.removeCallbacks(mShowPart2Runnable);
        mHideHandler.postDelayed(mHidePart2Runnable, UI_ANIMATION_DELAY);
    }

    private final Runnable mHidePart2Runnable = new Runnable() {
        @SuppressLint("InlinedApi")
        @Override
        public void run() {
            // Delayed removal of status and navigation bar

            // Note that some of these constants are new as of API 16 (Jelly Bean)
            // and API 19 (KitKat). It is safe to use them, as they are inlined
            // at compile-time and do nothing on earlier devices.
        }
    };

    @SuppressLint("InlinedApi")
    private void show() {
        // Show the system bar
        mVisible = true;

        // Schedule a runnable to display UI elements after a delay
        mHideHandler.removeCallbacks(mHidePart2Runnable);
        mHideHandler.postDelayed(mShowPart2Runnable, UI_ANIMATION_DELAY);
    }

    private final Runnable mShowPart2Runnable = new Runnable() {
        @Override
        public void run() {
            // Delayed display of UI elements
            ActionBar actionBar = getSupportActionBar();
            if (actionBar != null) {
                actionBar.show();
            }
        }
    };

    private final Handler mHideHandler = new Handler();
    private final Runnable mHideRunnable = new Runnable() {
        @Override
        public void run() {
            hide();
        }
    };

    /**
     * Schedules a call to hide() in delay milliseconds, canceling any
     * previously scheduled calls.
     */
    private void delayedHide(int delayMillis) {
        mHideHandler.removeCallbacks(mHideRunnable);
        mHideHandler.postDelayed(mHideRunnable, delayMillis);
    }
}

Now if I execute my code, then I get a page saying "Website not available. The website under https://www.botec.de/ could not load because: net::ERR_CACHE_MISS" (solution for this new problem can be found here).

So the problem with the blackscreen and "crash" or rather closing of the app, was because of that unnecessary code which was there after creating a new empty fullscreen project.

Black
  • 18,150
  • 39
  • 158
  • 271