java.lang.NullPointerException:尝试调用虚拟方法';int android.view.view.getImportantForAccessibility()';使用BaseAdapter
如何使用BaseAdapter从SDcard中的文件夹中获取mp3文件并在自定义listview中显示,以及如何在单击项时播放歌曲 我正在使用基本适配器从SD卡特定文件夹获取mp3文件。正在获取mp3文件,但无法添加到自定义基本适配器。 我得到一个例外: java.lang.NullPointerException:尝试调用虚拟方法“int” null对象上的android.view.view.GetImportEntForAccessibility() 使用BaseAdapter进行引用 下面是我的片段:java.lang.NullPointerException:尝试调用虚拟方法';int android.view.view.getImportantForAccessibility()';使用BaseAdapter,android,listview,baseadapter,Android,Listview,Baseadapter,如何使用BaseAdapter从SDcard中的文件夹中获取mp3文件并在自定义listview中显示,以及如何在单击项时播放歌曲 我正在使用基本适配器从SD卡特定文件夹获取mp3文件。正在获取mp3文件,但无法添加到自定义基本适配器。 我得到一个例外: java.lang.NullPointerException:尝试调用虚拟方法“int” null对象上的android.view.view.GetImportEntForAccessibility() 使用BaseAdapter进行引用 下面
ListView lv_recordersList;
private ListViewAdapter adapter;
ProgressDialog mProgressDialog;
private List<String> myList;
File file;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.frag_recorders_list, null);
lv_recordersList = (ListView) v.findViewById(R.id.lv_recordersList);
myList = new ArrayList<String>();
new DownloadJSON().execute();
return v;
}
public class DownloadJSON extends AsyncTask<Void, Void, Void>{
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
mProgressDialog = new ProgressDialog(getActivity());
//mProgressDialog.setTitle("PlugLeads");
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
File directory = Environment.getExternalStorageDirectory();
file = new File(directory + "/Plugleads");
File list[] = file.listFiles();
for (int i = 0; i < list.length; i++) {
// if(checkExtension( list[i].getName())
if (checkExtension(list[i].getName())) {
myList.add(list[i].getName());
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
//listview = (ListView) findViewById(R.id.listview);
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapter(getActivity(), myList);
// Set the adapter to the ListView
lv_recordersList.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
}
private boolean checkExtension(String fileName) {
String ext = getFileExtension(fileName);
if (ext == null)
return false;
try {
if (SupportedFileFormat.valueOf(ext.toUpperCase()) != null) {
return true;
}
} catch (IllegalArgumentException e) {
return false;
}
return false;
}
public String getFileExtension(String fileName) {
int i = fileName.lastIndexOf('.');
if (i > 0) {
return fileName.substring(i + 1);
} else
return null;
}
}
请帮助我。Thaks提前…您的getView()方法返回null更改以下代码
改变这个
return convertView;
对此
return itemView;
更新:我只是在使用这段代码获取文件名
File file = new File("/storage/sdcard0/abc.mp3");
String strFileName = file.getName();
getView()方法返回null并更改以下代码
改变这个
return convertView;
对此
return itemView;
更新:我只是在使用这段代码获取文件名
File file = new File("/storage/sdcard0/abc.mp3");
String strFileName = file.getName();
在
protectedvoiddoinbackground
方法中,它返回null,因此您可能希望将其更改为某个值。另外,您还需要对方法publicstringgetfileextension
执行相同的操作。希望这有帮助!祝你的项目好运 在protectedvoiddoinbackground
方法中,它返回null,因此您可能希望将其更改为某个值。另外,您还需要对方法publicstringgetfileextension
执行相同的操作。希望这有帮助!祝你的项目好运 只需返回视图而不是null
登记入住
public View getView(int i, View view, ViewGroup viewGroup) {
View
mView=mLayoutInflater.inflate(R.layout.row_layout,viewGroup,false);
TextView mUserPhone,mUserId,mUserName;
mUserId=(TextView)mView.findViewById(R.id.mUserId);
mUserPhone=(TextView)mView.findViewById(R.id.mUserPhone);
mUserName=(TextView)mView.findViewById(R.id.mUserName);
UserData mUserData= (UserData) getItem(i);
mUserId.setText(mUserData.getuId());
mUserName.setText(mUserData.getuName());
mUserPhone.setText(mUserData.getuPhone());
return null;
}
相反
public View getView(int i, View view, ViewGroup viewGroup) {
View mView=mLayoutInflater.inflate(R.layout.row_layout,viewGroup,false);
TextView mUserPhone,mUserId,mUserName;
mUserId=(TextView)mView.findViewById(R.id.mUserId);
mUserPhone=(TextView)mView.findViewById(R.id.mUserPhone);
mUserName=(TextView)mView.findViewById(R.id.mUserName);
UserData mUserData= (UserData) getItem(i);
mUserId.setText(mUserData.getuId());
mUserName.setText(mUserData.getuName());
mUserPhone.setText(mUserData.getuPhone());
return mView;
}
只需返回视图而不是null 登记入住
public View getView(int i, View view, ViewGroup viewGroup) {
View
mView=mLayoutInflater.inflate(R.layout.row_layout,viewGroup,false);
TextView mUserPhone,mUserId,mUserName;
mUserId=(TextView)mView.findViewById(R.id.mUserId);
mUserPhone=(TextView)mView.findViewById(R.id.mUserPhone);
mUserName=(TextView)mView.findViewById(R.id.mUserName);
UserData mUserData= (UserData) getItem(i);
mUserId.setText(mUserData.getuId());
mUserName.setText(mUserData.getuName());
mUserPhone.setText(mUserData.getuPhone());
return null;
}
相反
public View getView(int i, View view, ViewGroup viewGroup) {
View mView=mLayoutInflater.inflate(R.layout.row_layout,viewGroup,false);
TextView mUserPhone,mUserId,mUserName;
mUserId=(TextView)mView.findViewById(R.id.mUserId);
mUserPhone=(TextView)mView.findViewById(R.id.mUserPhone);
mUserName=(TextView)mView.findViewById(R.id.mUserName);
UserData mUserData= (UserData) getItem(i);
mUserId.setText(mUserData.getuId());
mUserName.setText(mUserData.getuName());
mUserPhone.setText(mUserData.getuPhone());
return mView;
}
如前所述,查看适配器的getView()
方法。它可能返回null,而不是行的主视图。对不起,这没什么特别的。只是一个无聊的bug。如前所述,查看适配器的getView()
方法。它可能返回null,而不是行的主视图。对不起,这没什么特别的。只是一只无聊的虫子
ImageView img_recorders_listitem;
TextView tv_recorders_listitem;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.frag_recorders_listitem, parent, false);
img_recorders_listitem = (ImageView) itemView.findViewById(R.id.img_recorders_listitem);
tv_recorders_listitem = (TextView) itemView.findViewById(R.id.tv_recorders_listitem);
return convertView;
在您的代码中,只需将其更改为returnitemview
在您的代码中,只需将其更改为
return itemView
可能重复的可能重复的可能重复的可能重复的可能重复的可能重复的可能重复的感谢您的回复。是的,它工作正常,但如何获取文件名并将其设置为listview BaseAdapter?如何获取要播放的mp3文件的文件名和位置?@Naveen see此处是获取所有mp3名称的有用链接。感谢您的回复,我如何使用您的推荐信?实际上,我想从myListp对象中获取文件名?我正在获取文件。我想添加ListItem单击并播放所选文件。您能帮助我吗?谢谢您的回复。是的,它工作正常,但如何获取文件名并将其设置为listview BaseAdapter?如何获取要播放的mp3文件的文件名和位置?@Naveen see此处是获取所有mp3名称的有用链接。感谢您的回复,我如何使用您的推荐信?实际上,我想从myListp对象中获取文件名?我正在获取文件。我想添加ListItem单击并播放所选文件。你能帮我吗。