listview与bug

时间:2012-11-12 06:50:54

标签: android listview

我正在开发音响播放器应用程序以及何时在emulater上运行它可以正常工作,但它无法在物理设备上运行。单击图标开始时出错,我收到错误并且被忽略了。有人可以看看这段代码吗? 这是代码。

SoundPlayList Activity @

public class SoundPlayList extends Activity {
public final static int PLAY_MUSIC = 0;
private ListView mListView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.soundplayerlist);

    mListView = (ListView)findViewById(R.id.listView1);

    SoundPlayerAdapter adapter = new SoundPlayerAdapter(this);
    mListView.setAdapter(adapter);

    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent,View view,int position,long id){
        ListView listView = (ListView)parent;

        String item = (String)listView.getItemAtPosition(position);
        Intent intent = new Intent(SoundPlayList.this, SoundPlayerActivity.class);
        intent.putExtra("id",parent.getItemIdAtPosition(position));
        intent.putExtra("name",item);
        startActivityForResult(intent,PLAY_MUSIC);

    }
    });

}

SoundPlayerAdapter @

public class SoundPlayerAdapter extends ArrayAdapter<String>{
    private ArrayList<Long> mMusicIds = new ArrayList<Long>();
    public SoundPlayerAdapter(Context context) {
        super(context, android.R.layout.simple_list_item_1);
        ContentResolver resolver = context.getContentResolver();
        Cursor cursor = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null,null,null,null);
    while(cursor.moveToNext()){
        String name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
        long id = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID));

        mMusicIds.add(id);

        this.add(name);

    }
    }
    public long getItemId(int position){
        return mMusicIds.get(position);

    }
}

}

log trace @

11-12 15:45:04.985: D/AndroidRuntime(5288): Shutting down VM
11-12 15:45:04.985: W/dalvikvm(5288): threadid=1: thread exiting with uncaught exception (group=0x2aac8578)
11-12 15:45:04.985: E/AndroidRuntime(5288): FATAL EXCEPTION: main
11-12 15:45:04.985: E/AndroidRuntime(5288): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.soundplayer/com.example.soundplayer.SoundPlayList}: java.lang.NullPointerException
11-12 15:45:04.985: E/AndroidRuntime(5288):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
11-12 15:45:04.985: E/AndroidRuntime(5288):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
11-12 15:45:04.985: E/AndroidRuntime(5288):     at android.app.ActivityThread.access$1500(ActivityThread.java:121)
11-12 15:45:04.985: E/AndroidRuntime(5288):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
11-12 15:45:04.985: E/AndroidRuntime(5288):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-12 15:45:04.985: E/AndroidRuntime(5288):     at android.os.Looper.loop(Looper.java:130)
11-12 15:45:04.985: E/AndroidRuntime(5288):     at android.app.ActivityThread.main(ActivityThread.java:3701)
11-12 15:45:04.985: E/AndroidRuntime(5288):     at java.lang.reflect.Method.invokeNative(Native Method)
11-12 15:45:04.985: E/AndroidRuntime(5288):     at java.lang.reflect.Method.invoke(Method.java:507)
11-12 15:45:04.985: E/AndroidRuntime(5288):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
11-12 15:45:04.985: E/AndroidRuntime(5288):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
11-12 15:45:04.985: E/AndroidRuntime(5288):     at dalvik.system.NativeStart.main(Native Method)
11-12 15:45:04.985: E/AndroidRuntime(5288): Caused by: java.lang.NullPointerException
11-12 15:45:04.985: E/AndroidRuntime(5288):     at com.example.soundplayer.SoundPlayList$SoundPlayerAdapter.<init>(SoundPlayList.java:52)
11-12 15:45:04.985: E/AndroidRuntime(5288):     at com.example.soundplayer.SoundPlayList.onCreate(SoundPlayList.java:29)
11-12 15:45:04.985: E/AndroidRuntime(5288):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-12 15:45:04.985: E/AndroidRuntime(5288):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
11-12 15:45:04.985: E/AndroidRuntime(5288):     ... 11 more
11-12 15:45:10.105: I/Process(5288): Sending signal. PID: 5288 SIG: 9

1 个答案:

答案 0 :(得分:0)

11-12 15:45:04.985: E/AndroidRuntime(5288): Caused by: java.lang.NullPointerException
11-12 15:45:04.985: E/AndroidRuntime(5288): at com.example.soundplayer.SoundPlayList$SoundPlayerAdapter.(SoundPlayList.java:52)
11-12 15:45:04.985: E/AndroidRuntime(5288): at com.example.soundplayer.SoundPlayList.onCreate(SoundPlayList.java:29)
11-12 15:45:04.985: E/AndroidRuntime(5288): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-12 15:45:04.985: E/AndroidRuntime(5288): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
11-12 15:45:04.985: E/AndroidRuntime(5288): ... 11 more
11-12 15:45:10.105: I/Process(5288): Sending signal. PID: 5288 SIG: 9

根据日志信息,请参阅SoundPlayList.java:29文件。

修改

ContentResolver.query方法可能返回null。所以你需要NullPoint检查。