我的应用中出现Java错误,不确定该怎么办

时间:2018-09-06 13:43:36

标签: java android

正在玩游戏

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>

当我单击错误时,它带我到nowPlaying.setOnClickListener(new View.OnClickListener(),但我不确定该怎么做。

@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)

2 个答案:

答案 0 :(得分:0)

您是否正确初始化了nowIntent nowPlayingIntent?

您可以显示NowPlayingActivity类吗?

答案 1 :(得分:0)

您需要更新:

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

还更新所有已初始化的变量,因为在您创建的xml中存在的任何ID都需要初始化。