-1

我正在开发声音播放器应用程序,何时在模拟器上运行它可以工作,但它不能在物理设备上工作。单击图标启动时的错误,我收到错误并异常终止。有人可以看看这段代码吗?这是代码。

声音播放列表活动@

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);

    }
    });

}

声音播放器适配器@

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);

    }
}

}

日志跟踪@

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
4

1 回答 1

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 检查。

于 2012-11-12T06:58:27.443 回答