java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getImportantForAccessibility()' using BaseAdapter

一个人想着一个人 提交于 2020-04-20 18:37:10

问题


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.

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 :

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getImportantForAccessibility()' on a null object reference using BaseAdapter.

Below is my Fragment:

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


    }

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

My 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...


回答1:


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

change this

 return convertView;

to this

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();



回答2:


In your protected Void doInBackground method it returns null so you might want to change that to some value. Also, you would want to do the same with the method public String getFileExtension. Hope this helps! Good Luck with your project!




回答3:


As mentioned here look at your getView() method of the adapter. It probably returns null rather than the master view of the line. Sorry, nothing too exotic about that. Just a boring silly bug.




回答4:


simply return the view instead of null

check in

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

Instead of that

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


来源:https://stackoverflow.com/questions/37717531/java-lang-nullpointerexception-attempt-to-invoke-virtual-method-int-android-vi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!