java.lang.NullPointerException:尝试调用虚方法' int android.view.View.getImportantForAccessibility()'使用BaseAdapter

时间:2016-06-09 05:30:25

标签: android listview baseadapter

如何从SD卡中的文件夹中获取mp3文件并使用BaseAdapter在自定义列表视图中显示,并且还想在项目点击中播放该歌曲。

我使用base adapter从SDCard特定文件夹中获取mp3文件。获取mp3文件但无法添加到自定义基本适配器。 我得到了这个例外:

  

java.lang.NullPointerException:尝试调用虚方法' int   android.view.View.getImportantForAccessibility()'在null对象上   使用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;
        }


    }

这是我的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)

请帮助我。提前告知......

5 个答案:

答案 0 :(得分:3)

你的getView()方法返回null改变下面的代码。

更改此

 return convertView;

到这个

return itemView;

更新:只是我使用的代码对我有用,可以获取文件名。

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

答案 1 :(得分:1)

protected Void doInBackground方法中,它返回null,因此您可能希望将其更改为某个值。此外,您还希望对方法public String getFileExtension执行相同操作。希望这可以帮助!祝你的项目好运!

答案 2 :(得分:1)

如上所述here请查看适配器的getView()方法。它可能返回null而不是行的主视图。对不起,没什么异国情调的。只是一个无聊的愚蠢错误。

答案 3 :(得分:0)

只返回视图而不是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;
    }

答案 4 :(得分:0)

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;

在您的代码中代替return convertView,只需将其更改为return itemView