首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >我的应用程序中出现Java错误,不知道该怎么办

我的应用程序中出现Java错误,不知道该怎么办
EN

Stack Overflow用户
提问于 2018-09-06 21:43:36
回答 2查看 59关注 0票数 0

正在播放活动

代码语言:javascript
复制
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);
            }
        });

    }
}

代码语言:javascript
复制
<?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( View.OnClickListener() ),但我不确定要做什么。

代码语言:javascript
复制
@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);
        }
    });
代码语言:javascript
复制
**

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)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-09-06 22:50:25

您需要更新:

代码语言:javascript
复制
TextView nowPlaying = (TextView) findViewById(R.id.nowplaying);

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

票数 0
EN

Stack Overflow用户

发布于 2018-09-06 22:42:51

是否正确初始化了显式意图nowPlayingIntent?

你能展示一下NowPlayingActivity类吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52205476

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档