本文介绍了java.lang.NullPointerException:尝试使用BaseAdapter调用虚拟方法'int android.view.View.getImportantForAccessibility()'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用BaseAdapter从SDcard的文件夹中获取mp3文件并在自定义列表视图中显示,以及如何在Item Click上播放歌曲.

How to get mp3 files from a folder in SDcard and show in custom listview using BaseAdapter and also want to play the song on Item Click.

我正在使用基本适配器从SDCard特定文件夹中获取mp3文件.正在获取mp3文件,但无法添加到自定义基本适配器中.我收到此异常:

I am getting the mp3 files from a SDCard particular folder using base adapter.Getting the mp3 files but unable to add to custom base Adapter.I am getting this exception :

下面是我的片段:

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


    }

这是我的CustomBaseAdapter

This is my CustomBaseAdapter

public class ListViewAdapter extends BaseAdapter{

Context context;
LayoutInflater inflater;
private List<String> myListp;

public ListViewAdapter(Context context,
        List<String> myList) {
    this.context = context;
    myListp= myList;

}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return myListp.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return myListp.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

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

我的Logcat

 06-09 10:30:43.378: E/AndroidRuntime(21491): FATAL EXCEPTION: main
        06-09 10:30:43.378: E/AndroidRuntime(21491): Process: com.plugleads.feedbackform, PID: 21491
        06-09 10:30:43.378: E/AndroidRuntime(21491): java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getImportantForAccessibility()' on a null object reference
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.widget.AbsListView.obtainView(AbsListView.java:2363)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.widget.ListView.makeAndAddView(ListView.java:1864)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.widget.ListView.fillDown(ListView.java:698)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.widget.ListView.fillFromTop(ListView.java:759)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.widget.ListView.layoutChildren(ListView.java:1673)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.widget.AbsListView.onLayout(AbsListView.java:2151)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.view.View.layout(View.java:15689)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.view.ViewGroup.layout(ViewGroup.java:5040)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.view.View.layout(View.java:15689)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.view.ViewGroup.layout(ViewGroup.java:5040)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.view.View.layout(View.java:15689)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.view.ViewGroup.layout(ViewGroup.java:5040)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1043)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.view.View.layout(View.java:15689)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.view.ViewGroup.layout(ViewGroup.java:5040)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.view.View.layout(View.java:15689)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.view.ViewGroup.layout(ViewGroup.java:5040)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.view.View.layout(View.java:15689)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.view.ViewGroup.layout(ViewGroup.java:5040)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.view.View.layout(View.java:15689)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.view.ViewGroup.layout(ViewGroup.java:5040)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.view.View.layout(View.java:15689)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.view.ViewGroup.layout(ViewGroup.java:5040)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.view.View.layout(View.java:15689)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.view.ViewGroup.layout(ViewGroup.java:5040)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2116)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1873)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1084)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5990)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.view.Choreographer.doCallbacks(Choreographer.java:580)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.view.Choreographer.doFrame(Choreographer.java:550)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.os.Handler.handleCallback(Handler.java:739)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.os.Handler.dispatchMessage(Handler.java:95)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.os.Looper.loop(Looper.java:135)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at android.app.ActivityThread.main(ActivityThread.java:5343)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at java.lang.reflect.Method.invoke(Native Method)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at java.lang.reflect.Method.invoke(Method.java:372)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
        06-09 10:30:43.378: E/AndroidRuntime(21491):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

请帮助我.提前进行提醒...

Please Help me.Thaks in advance...

推荐答案

您的getView()方法返回null更改以下代码.

your getView() method return null change the below code.

更改此

 return convertView;

对此

return itemView;

更新:只是我正在使用适用于我的代码来获取文件名.

Update : simply I am using this code that work for me to get the name of file.

File file = new File("/storage/sdcard0/abc.mp3");
String strFileName = file.getName();

这篇关于java.lang.NullPointerException:尝试使用BaseAdapter调用虚拟方法'int android.view.View.getImportantForAccessibility()'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:12