这个函数没有看到activity_listing_music.xml中的id我只想对我的音乐播放器做一个简单的列表视图活动但是java代码看不到来自xml的id 所以我希望有人可以帮忙 AAA 一个 一个 一个 一个 一个 一个 一个 一个 一个 一个 一个 一个
这是代码:
package musync.demo_app;
<import is here>
public class ListingMusic extends AppCompatActivity {
<useless code>
public void doStuff() {
listView = findViewById(R.id.list);
arrayList = new ArrayList<>();
getMusic();
adapter = new ArrayAdapter<String>(this, android.R.simple_List_item_1, arrayList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//music plater here
}
});
}
public void getMusic() {
ContentResolver contentResolver = getContentResolver();
Uri songUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor songCursor = contentResolver.query(songUri, null, null, null, null);
if(songCursor != null && songCursor.moveToFirst()) {
int songTitle = songCursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
int songArtist = songCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
do {
String currentTitle = songCursor.getString(songTitle);
String currentArtist = songCursor.getString(songArtist);
arrayList.add(currentTitle + '\n' + currentArtist);
} while(songCursor.moveToNext());
}
}
}
,这是xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:paddingBottom="0dp"
android:paddingTop="0dp"
android:paddingRight="0dp"
android:paddingLeft="0dp"
tools:context="musync.demo_app.ListingMusic">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="1dp"
/>
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:-1)
你错过了你的onCreate()?在java文件中添加onCreate函数,并在调用R.id.listview
函数后输入setContentView()
。
答案 1 :(得分:-1)
改变这个:
ListView listView = findViewById(R.id.list);
要:
ListView listView = (ListView) findViewById(R.id.list);