java - 音乐 ListView 无法在 4.4 或更高版本上运行?

标签 java android android-4.4-kitkat

此代码在 4.2 及更低版本上工作正常,但在 4.4 或更高版本上工作正常。它显示错误,不幸的是应用程序已停止工作,我不知道为什么。请帮忙!因为我是安卓新手。不是因为不推荐使用的方法吗?或者是什么 ?

This is my 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" />
<小时/>

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

关于java - 音乐 ListView 无法在 4.4 或更高版本上运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36449689/

相关文章:

java - 如何检查表中的特定 TD 是否包含我想要的值?

java - 带有嵌入式表单的 Camunda Spring Boot 启动器

android - Android应用程序证书的目的

java - 如何在 android API 19 (KitKat) 中保留权限?

javascript - WebGL 被检测为支持,但实际上并不支持

java - 在java中序列化

java - ConcurrentSkipListMap 排序

java - Android 内部存储 VS 共享首选项

android - 在 kivy 中,如何使用 android toast 方法?

android - 应用程序安装但在 KitKat 更新后未在 Google Glass 中显示