0

Now Playing Activity

public class NowPlayingActivity extends AppCompatActivity {


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

        // The buttons on the screen
        ImageButton playButton = findViewById(R.id.playButton);
        ImageButton previousSongButton = findViewById(R.id.previousButton);
        ImageButton nextSongButton = findViewById(R.id.nextButton);
        ImageButton repeatButton = findViewById(R.id.repeatButton);
        ImageButton shuffleButton = findViewById(R.id.shuffleButton);

        Button albumsMenu = (Button) findViewById(R.id.albumsMenu);
        Button artistsMenu = (Button) findViewById(R.id.artistsMenu);

        albumsMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent albumsIntent = new Intent(NowPlayingActivity.this, AlbumsActivity.class);
                startActivity(albumsIntent);
            }
        });


        artistsMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent ArtistIntent = new Intent(NowPlayingActivity.this, ArtistsActivity.class);
                startActivity(ArtistIntent);
            }
        });

    }
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="#FFF7DA"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/nowplaying"
        style="@style/CategoryStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FD8E09"
        android:gravity="center_horizontal"
        android:text="Now Playing" />

    <ImageView
        style="@style/CategoryIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="Now Playing"
        android:src="@drawable/playicon" />

    <TextView
        android:id="@+id/artists"
        style="@style/CategoryStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#379237"
        android:gravity="center_horizontal"
        android:text="Artists" />

    <ImageView
        style="@style/CategoryIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="Artists"
        android:src="@drawable/artisticon" />

    <TextView
        android:id="@+id/albums"
        style="@style/CategoryStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#8800A0"
        android:gravity="center_horizontal"
        android:text="Albums" />

    <ImageView
        style="@style/CategoryIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="Albums"
        android:src="@drawable/albumicon" />

</LinearLayout>

When I click on the error it takes me to nowPlaying.setOnClickListener(new View.OnClickListener() but I'm not sure what to do.

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

    // Find the View that shows the now playing category
    LinearLayout nowPlaying = findViewById(R.id.activity_nowPlaying);
    //Find the View that shows the artists category
    LinearLayout artists = findViewById(R.id.activity_artists);
    //Find the View that shows the albums category
    LinearLayout albums = findViewById(R.id.activity_albums);

    nowPlaying.setOnClickListener(new View.OnClickListener() {

        // This method will be executed when the now playing category is clicked on.
        @Override
        public void onClick(View view) {
            // Create a new intent to open the {@link NowPlayingActivity}
            Intent nowPlayingIntent = new Intent(MainActivity.this, NowPlayingActivity.class);

            // Start the new activity
            startActivity(nowPlayingIntent);
        }
    });
**

This is the error I'm receiving.
-----------

**
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                      at com.example.alexaquinones.musicalstructure.MainActivity.onCreate(MainActivity.java:24)
Alexa Q.
  • 15
  • 5

2 Answers2

0

Have you initialized the explicit intent nowPlayingIntent correctly?

can you show the NowPlayingActivity class?

afkhero
  • 16
  • 1
  • 5
0

You need to update :

TextView nowPlaying = (TextView) findViewById(R.id.nowplaying);

Also update all vars you have initialized, because any of your ids that exist in the xml you have made need to be initialized.

afkhero
  • 16
  • 1
  • 5
rusito23
  • 995
  • 8
  • 20