android代码适配器 报空指向错误 求大神帮忙啊
activity_main.xml<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
/>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放"
android:layout_marginTop="370sp"
android:layout_centerHorizontal="true"
/>
<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一首"
android:layout_marginTop="370sp"
android:layout_toRightOf="@id/play"
/>
<Button
android:id="@+id/before"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="上一首"
android:layout_marginTop="370sp"
android:layout_toLeftOf="@id/play"
/>
<SeekBar
android:layout_height="wrap_content"
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_marginTop="320sp"/>
</RelativeLayout>
Activity
public class MainActivity extends Activity {
private TextView text1;
private ListView list;
private Button play,next,before;
private SeekBar seekBar;
File[] currentFiles;
int state=2;
IntentFilter inf=new IntentFilter();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text1=(TextView)findViewById(R.id.text1);
play=(Button)findViewById(R.id.play);
next=(Button)findViewById(R.id.next);
before=(Button)findViewById(R.id.before);
seekBar=(SeekBar)findViewById(R.id.seekBar1);
seekBar.setMax(100);
inf.addAction("change");
registerReceiver(broad,inf);
File root = new File("/mnt/sdcard/");
currentFiles = root.listFiles();
// 使用当前目录下的全部文件、文件夹来填充ListView
inflateListView(currentFiles);
play.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (state) {
case 1:
state=2;
break;
default:
state=1;
break;
}
Intent intent=new Intent(MainActivity.this,MusicService.class);
intent.putExtra("action",state);
startService(intent);
}
});
}
private void inflateListView(File[] files) {
// TODO Auto-generated method stub
List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
for (int i = 0; i < files.length; i++) {
Map<String, Object> listItem = new HashMap<String, Object>();
// 添加一个文件名称
listItem.put("filename", files[i].getName());
File myFile = new File(files[i].getName());
listItems.add(listItem);
}
SimpleAdapter adapter = new SimpleAdapter(
MainActivity.this, listItems, R.layout.other,
new String[] { "filename" }, new int[] {
R.id.fileName});
// 填充数据集
list.setAdapter(adapter);
}
public BroadcastReceiver broad=new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
int i=intent.getIntExtra("action",-1);
Log.v("aaa", String.valueOf(i));
switch (i) {
case 1:
play.setText("暂停");
break;
default:
play.setText("播放");
break;
}
}
};
}
other.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/fileName"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>/
</LinearLayout>
(Service.java略) Android
补充:Java , Eclipse