2

On my weather app, the splash screen runs well but when it's getting into the HomeActivity, it displays app has stopped when I tested it on my device

on the logcat, it displays:

No Activity found to handle Intent {  }

Meanwhile I passed intent in my splash screen:

        int SPLASH_SCREEN = 5000;
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(MainActivity.this, HomeActivity.class);
                startActivity(intent);
                finish();
            }
        }, SPLASH_SCREEN);
    }
}

My HomeActivity code:

public class HomeActivity extends AppCompatActivity {
    ConstraintLayout constraintLayout;
    public static int count=0;
    int[] drawable =new int[]{R.drawable.burj_khalifa,R.drawable.central_bank_of_nigeria,R.drawable.eiffel_tower,R.drawable.hong_kong,R.drawable.statue_of_liberty};
    Timer _t;

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

        constraintLayout = (ConstraintLayout) findViewById(R.id.layout);
        constraintLayout.setBackgroundResource(R.drawable.burj_khalifa);
            _t = new Timer();
            _t.scheduleAtFixedRate(new TimerTask() {
                @Override
                        public void run() {
                    runOnUiThread(new Runnable() { // run on ui thread
                        @Override
                        public void run() {
                            if (count < drawable.length) {

                                constraintLayout.setBackgroundResource(drawable[count]);
                                count = (count + 1) % drawable.length;
                            }
                        }
                    });
            }
        }, 5000, 5000);
    }
}

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tex.lightweatherforecast">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".Activity.HomeActivity"
            android:label="@string/title_activity_home" />
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <meta-data
            android:name="preloaded_fonts"
            android:resource="@array/preloaded_fonts" />
    </application>

</manifest> 

activity_home.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/burj_khalifa"
    tools:context=".Activity.HomeActivity">


</androidx.constraintlayout.widget.ConstraintLayout>

Is there anything to correct this?

Zain
  • 37,492
  • 7
  • 60
  • 84
Chinez
  • 551
  • 2
  • 6
  • 29

2 Answers2

3

So, removing finish(); from inside your handler will solve this

       new Handler().postDelayed(new Runnable() {
           @Override
           public void run() {
               Intent intent = new Intent(MainActivity.this, HomeActivity.class);
               startActivity(intent);
               finish();
           }
       }, SPLASH_SCREEN);

If you want to remove the splash screen from the stack, then you could do

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

Attempt to invoke virtual method 'void androidx.constraintlayout.widget.ConstraintLayout.setBackgroundResource(int)' on a null object reference

Now you've in the HomeActivity onCreate, and this is different from No Activity found to handle Intent { } Error.

this tells that your constraintLayout in below statement is null and you can't call setBackgroundResource unless you intialize it with a non-null value

 constraintLayout.setBackgroundResource(R.drawable.burj_khalifa);

You got it from your activity layout, you need to check that this id R.id.layout actually exists in activity_home.xml layout as a first troubleshoot step

 constraintLayout = (ConstraintLayout) findViewById(R.id.layout);
Zain
  • 37,492
  • 7
  • 60
  • 84
  • The major issue now is the constraintlayout and I don't know how to check that R.id.layout exists in activity_home.xml layout but I'll post the code up – Chinez Nov 28 '20 at 15:35
  • 2
    Your activity layout doesn't have a layout id. – private static Nov 28 '20 at 15:39
  • 2
    Wow, I can't believe this was the bug that caused this error since! Thanks for your help guys, it finally fixed after I added the id. – Chinez Nov 28 '20 at 15:48
0

In your manifest try writing only .HomeActivity instead of .Activity.HomeActivity Moreover if it still shows error than I think you cannot write 'R.id.layout' just because 'layout' is a keyword so try defining any other id to your constraint layout.

Jamal
  • 111
  • 8