Java findviewbyid上的Android空指针异常
嗨,我现在正在做我的android mediaplayer项目,一切都很好,但后来我改变了布局设计,现在我每次启动时都会得到java.nullpointerexception,我到处搜索答案,但我所做的一切都不起作用。。。有人能帮我吗 这里是layout.xmlJava findviewbyid上的Android空指针异常,java,android,media-player,Java,Android,Media Player,嗨,我现在正在做我的android mediaplayer项目,一切都很好,但后来我改变了布局设计,现在我每次启动时都会得到java.nullpointerexception,我到处搜索答案,但我所做的一切都不起作用。。。有人能帮我吗 这里是layout.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical" >
<SeekBar
android:id="@+id/sbSong"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="72dp" />
<ImageButton
android:id="@+id/btnNext"
android:contentDescription="@string/Next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="@drawable/next" />
<ImageButton
android:id="@+id/btnBack"
android:contentDescription="@string/Previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:src="@drawable/previous" />
<ImageButton
android:id="@+id/btnPlay"
android:contentDescription="@string/btnPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="@drawable/play" />
<ImageButton
android:id="@+id/btnShuffle"
android:contentDescription="@string/shuffleOn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/sbSong"
android:layout_alignParentRight="true"
android:src="@drawable/shuffle_off" />
<ImageButton
android:id="@+id/btnRepeat"
android:contentDescription="@string/repeatOn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/sbSong"
android:layout_alignParentLeft="true"
android:src="@drawable/repeat_off" />
<TextView
android:id="@+id/txtDuration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/btnShuffle"
android:layout_toLeftOf="@+id/btnShuffle"
android:text="TextView" />
<TextView
android:id="@+id/txtCurrentPosition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/btnRepeat"
android:layout_toRightOf="@+id/btnRepeat"
android:text="TextView" />
<ImageView
android:id="@+id/AlbumArtwork"
android:contentDescription="@string/noSong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btnShuffle"
android:layout_centerHorizontal="true"
android:layout_marginBottom="66dp"
android:src="@drawable/noartwork" />
<TextView
android:id="@+id/txtSongTitle"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageButton
android:id="@+id/btnPlaylist"
android:contentDescription="@string/SongList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/dropdown" />
<ListView
android:id="@+id/lstSongs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:visibility="invisible"
android:layout_below="@+id/txtSongTitle" >
</ListView>
<EditText
android:id="@+id/searchbox"
android:visibility="invisible"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/btnPlaylist"
android:ems="10" >
<requestFocus />
</EditText>
</RelativeLayout>
好吧,我知道有很多代码。。。我在学校做最后一个项目已经有两个月了,但现在我遇到了一个错误,第117行试图使用返回空值或尚未初始化的
视图
您是否更改了布局文件中任何视图的id属性?最后,无论何时更改布局文件,您都需要清理和重建项目,因为旧版本会被缓存以加快启动速度,并且通常会破坏内容 这里是这条线
btnSongList.setOnClickListener(new OnClickListener(){@Override public void
onClick(View v) {visible();}});
您从未为此特定视图调用过findViewById()
,因此指针仍然为空。下面的代码是第117行:
btnSongList.setOnClickListener(new OnClickListener(){@Override public void
onClick(View v) {visible();}});
但是,BtnsGlist在第50行中只出现一次:
private ImageButton btnSongList;
代码缺少任何类型的btnsglist初始化。因此发生了NullPointerException。可能缺少以下代码(如果布局中的btnPlaylist是代码中的BTNSglist):
MainActivity中的第117行是什么。我的版面上有一个按钮,通过设置listview是否不可见来显示我的歌曲列表,没有什么复杂的BtnsGlist.setOnClickListener(new OnClickListener(){@Override public void onClick(View v){visible();});
btnSongList.setOnClickListener(new OnClickListener(){@Override public void
onClick(View v) {visible();}});
private ImageButton btnSongList;
btnSongList = (ImageButton) findViewById(R.id.btnPlaylist);