此代码在4.2及以下版本正常工作,但在4.4或更高版本没有。它显示错误不幸的应用程序已停止工作我不知道为什么。请帮助!因为我是android的新手。是不是因为弃用的方法?或者是什么 ?
以下是我的代码:
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;
}
}
答案 0 :(得分:0)
正如我在您的Logtrace中看到的那样,您没有提供READ_EXTERNAL_STORAGE
的权限,因为您需要此权限才能从设备读取媒体数据。因此,您必须打开manifest.xml
文件并在application
标记之前添加以下权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
关于在某些设备上工作而不在其他设备上工作,这是因为不同设备和Android版本的媒体存储位置不同,因此音乐媒体数据存储在某些设备的外部存储中(以及因此你需要获得许可),而其他人则不需要。