0

I am currently working on an app that includes a splash screen with a sound. After the splash screen is done, I want it to be able to go to my Main Menu. The problem I have is that it keeps crashing after the sound is played. I've tried going on other threads having the same problem with different ways of doing it but it keeps crashing at the end of the splash screen.

Edit: When I run the app, my event log says that the app was successful and no errors are found. I ran the debugger and it says

E/ArrayAdapter: You must supply a resource ID for a TextView
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.finalproject, PID: 12755
    java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView

Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
        at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:388)

I believe it must be something with my MainActivity that is causing the problem.

My Splash code:

public class SplashActivity extends AppCompatActivity {

    MediaPlayer ourSound;

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

        Thread timer = new Thread(){
            @Override
            public void run(){
                try {
                    ourSound = MediaPlayer.create(SplashActivity.this, R.raw.splashsound);
                    ourSound.start();
                    sleep(4000);
                } catch (InterruptedException e){

                } finally {
                    Intent i = new Intent(SplashActivity.this, MainActivity.class);
                    startActivity(i);
                } // end finally
            } // end run
        };
        timer.start();
    } // end onCreate
    @Override

    protected void onPause(){
        super.onPause();
        ourSound.release();

    } // end onPause
} // end SplashActivity

My MainActivity Code:

public class MainActivity extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        String[] mainMenu = {"Websites", "Directions", "Scheduling", "Photos"};
        setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_main, mainMenu));
    } // end onCreate

    protected void onListItemClick(ListView l, View v, int position, long id) {
        switch (position) {
            case 0:
                startActivity(new Intent(MainActivity.this, Websites.class));
                break;
            case 1:
                startActivity(new Intent(MainActivity.this, Directions.class));
                break;
            case 2:
                startActivity(new Intent(MainActivity.this, Scheduling.class));
                break;
            case 3:
                startActivity(new Intent(MainActivity.this, Photos.class));
                break;
        } // end switch
    }// end onListItemClick
} // end MainActivity
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Ethan C
  • 1
  • 1
  • Please always post your log when you need help with crashing. – sanjeev Nov 13 '19 at 06:37
  • Also. have you heard of `CountdownTimer`? Please refer and check if it helps your purpose. – sanjeev Nov 13 '19 at 06:38
  • 1
    Please [edit] your question to provide the complete [stack trace from the crash](https://stackoverflow.com/a/23353174). – Mike M. Nov 13 '19 at 06:46
  • Run your app as debug and when it crashes show error log. – Syed Ahmed Jamil Nov 13 '19 at 06:46
  • Where is your `setListAdapter` code? – Md. Asaduzzaman Nov 13 '19 at 07:39
  • some where you are casting `RelativeLayout` to a `TextView` strange but I guess it must be a typo. I think the error is not in the code you provided – Syed Ahmed Jamil Nov 13 '19 at 07:41
  • `new ArrayAdapter(this, R.layout.activity_main, ...)` <-- That's your problem. Firstly, you don't want to pass the `Activity`'s layout there. That's meant to be a separate layout for the individual list items. Secondly, using that particular constructor means that `ArrayAdapter` will expect the layout to be a single, simple ``. That's why you're crashing. It's trying to cast your `Activity` layout's root `RelativeLayout` to a `TextView`. Simply change `R.layout.activity_main` to the built-in `android.R.layout.simple_list_item_1`, since the list is just simple `String`s. – Mike M. Nov 13 '19 at 07:53

2 Answers2

0

This should work for you

setListAdapter(new ArrayAdapter(this, R.layout.activity_main, mainMenu));

problem is here on R.layout.activity_main. It should be R.layout.simple_list_item

0

You are giving wrong layout in MainActivity
Replace R.layout.activity_main with android.R.layout.activity_list_item
For more info on ListActivity refer https://developer.android.com/reference/android/app/ListActivity

nayana bhoj
  • 115
  • 1
  • 11