音乐列表视图不适用于4.4或更高版本



此代码在4.2及以下版本上运行良好,但在4.4或更高版本上运行不佳。它显示错误。不幸的是,应用程序已停止工作,我不知道为什么。请帮助!因为我是安卓系统的新手。这不是因为不推荐使用的方法吗?还是怎样

这是我的Stacktrace

下面是我的代码:

package com.example.hamza;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity{
    ListView lvfs;
    Cursor cursorfm;
    int count,middleperson;
    TextView tv,title;
    ContentResolver conres;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initializesomestuff();
    }
    private void initializesomestuff() {
        // TODO Auto-generated method stub
        lvfs=(ListView)findViewById(R.id.lview);
        findViewById(R.id.tv1);
        String[] projection={MediaStore.Audio.Media._ID,MediaStore.Audio.Media.DISPLAY_NAME,MediaStore.Audio.Media.ARTIST,};
        cursorfm=conres.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, null, null,null);
        count=cursorfm.getCount();
        ////get some data
        lvfs.setAdapter(new musiclistadapter(getApplicationContext()));
    }
    /////// cutomiztion walla area///start
public class musiclistadapter extends BaseAdapter{

private Context mcontext;
String songname,songartist;
private LayoutInflater mLayoutInflater;
    public musiclistadapter(Context applicationContext) {
    // TODO Auto-generated constructor stub
        mcontext=applicationContext;
         //get the layout inflater
         mLayoutInflater = (LayoutInflater) mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return count;
    }
    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }
    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }
    @Override
    public View getView(int position, View arg1, ViewGroup arg2) {
        // TODO Auto-generated method stub
         ViewHolder holder;
        System.gc();
        tv = new TextView(mcontext.getApplicationContext());
        String id=null;
        //if(arg1==null){
         holder = new ViewHolder(); 
         arg1 = mLayoutInflater.inflate(R.layout.textviewlayout, arg2, false);
        // get all views you need to handle from the cell and save them in the view holder
         holder.itemName = (TextView) arg1.findViewById(R.id.tv1);
         // save the view holder on the cell view to get it back latter
            middleperson=cursorfm.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
            cursorfm.moveToPosition(position);
            id=cursorfm.getString(middleperson);
            middleperson=cursorfm.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST);
            cursorfm.moveToPosition(position);
            id=id+cursorfm.getString(middleperson);
            arg1.setTag(holder);
            holder.itemName.setText(id);
            //middleperson=cursorfm.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST);
            //id=cursorfm.getString(middleperson);
            //id+="whatever it is"+id+cursorfm.getString(middleperson);
            //LayoutInflater inflater = getLayoutInflater();
            //View row;
            //row = inflater.inflate(R.layout.textviewlayout, arg2, false);
           // TextView title;
           // title = (TextView) row.findViewById(R.id.tv1);
           // title.setText(id);
            //tv.setText(id);
           // return (row);
    //  }
        //else{
            //title = (TextView) arg1;
            // the getTag returns the viewHolder object set as a tag to the view
            holder = (ViewHolder)arg1.getTag();
        //}
         //if (id != null) {
                //set the item name on the TextView
                //holder.itemName.setText(cursorfm.getString(middleperson));
           // } else {
                // make sure that when you have an if statement that alters the UI, you always have an else that sets a default value back, otherwise you will find that the recycled items will have the same UI changes
             //   holder.itemName.setText("Unknown");   
           // }
            //this method must return the view corresponding to the data at the specified position.
            return arg1;
        //return (title);
    }   
}
private class ViewHolder {
    protected TextView itemName;
}
}

正如我在您的Logtrace中看到的,您没有提供READ_EXTERNAL_STORAGE的权限,因为您需要此权限才能从设备读取媒体数据。因此,您必须打开manifest.xml文件,并在application标记之前添加以下权限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

关于在某些设备中工作和不在其他设备中工作,这是因为不同设备和安卓版本的媒体存储位置不同,因此音乐媒体数据存储在某些设备的外部存储中(因此您需要许可),而在其他设备则不需要。

最新更新