类型未在我的应用中显示

时间:2018-01-17 13:56:17

标签: android database android-mediaplayer

我想在我的音乐应用程序中显示流派但是当应用程序运行时,当我切换选项卡时,我的应用程序崩溃,应用程序停止工作并给出以下logcat error.i不知道该错误是什么。请帮助。

这是logcat

  

android.database.sqlite.SQLiteException:没有这样的列:_display_name   (代码1):,编译时:SELECT name,0,_display_name FROM   audio_genres                                                                                   在   android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:179)                                                                                   在   android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)                                                                                   在   android.content.ContentProviderProxy.query(ContentProviderNative.java:418)                                                                                   在android.content.ContentResolver.query(ContentResolver.java:754)                                                                                   在android.content.ContentResolver.query(ContentResolver.java:704)                                                                                   在android.content.ContentResolver.query(ContentResolver.java:662)                                                                                   在layout.BlankFragment3.getGenreList(BlankFragment3.java:97)                                                                                   在layout.BlankFragment3.onCreateView(BlankFragment3.java:77)                                                                                   在   android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)                                                                                   在   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)                                                                                   在   android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)                                                                                   在   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)                                                                                   在   android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758)                                                                                   在   android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)                                                                                   在   android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)                                                                                   在   android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)                                                                                   在   android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1984)                                                                                   在   android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:626)                                                                                   在   android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)                                                                                   在android.support.v4.view.ViewPager.populate(ViewPager.java:1268)                                                                                   在   android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:668)                                                                                   在   android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:630)                                                                                   在   android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:611)                                                                                   在   android.support.design.widget.TabLayout $ ViewPagerOnTabSelectedListener.onTabSelected(TabLayout.java:2191)                                                                                   在   android.support.design.widget.TabLayout.dispatchTabSelected(TabLayout.java:1164)                                                                                   在   android.support.design.widget.TabLayout.selectTab(TabLayout.java:1157)                                                                                   在   android.support.design.widget.TabLayout.selectTab(TabLayout.java:1127)                                                                                   在   android.support.design.widget.TabLayout $ Tab.select(TabLayout.java:1426)                                                                                   在   android.support.design.widget.TabLayout $ TabView.performClick(TabLayout.java:1536)                                                                                   在android.view.View.onKeyUp(View.java:12222)                                                                                   在android.view.KeyEvent.dispatch(KeyEvent.java:2712)                                                                                   在android.view.View.dispatchKeyEvent(View.java:11465)

主要活动

public class BlankFragment3 extends Fragment {
    ArrayList<genreInfo> genreList = new ArrayList<>();
    RecyclerView recyclerView2;
    private genreAdapter genreAdapter1;
    long id;
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;


    public BlankFragment3() {
        // Required empty public constructor
    }


    // TODO: Rename and change types and number of parameters
    public static BlankFragment newInstance(String param1, String param2) {
        BlankFragment fragment = new BlankFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.genrere, container, false);
        recyclerView2 = rootView. findViewById(R.id.recyclerView2);
        genreAdapter1 = new genreAdapter(genreList, getActivity());
        recyclerView2.setAdapter(genreAdapter1);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView2.getContext(),
                linearLayoutManager.getOrientation());
        recyclerView2.setLayoutManager(linearLayoutManager);
        recyclerView2.addItemDecoration(dividerItemDecoration);
        getGenreList();
        return rootView;
    }


    public void getGenreList(){

final Uri uri = MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI;

        String name=MediaStore.Audio.Genres.NAME;
        try {
            id = Long.parseLong(MediaStore.Audio.Genres._ID);
        }catch (Exception e)
        {
            e.printStackTrace();
        }
        String dName=MediaStore.Audio.Media.DISPLAY_NAME;
        //  final String tracks = MediaStore.Audio.Albums.NUMBER_OF_SONGS;

        final String[] columns = { name, String.valueOf(id), dName };
        Cursor cursor = getContext().getContentResolver().query(uri, columns, null, null, null);

        if(cursor!=null && cursor.moveToFirst()) do {

            id = cursor.getLong(cursor.getColumnIndex(String.valueOf(id)));
            name = cursor.getString(cursor.getColumnIndex(name));
            dName = cursor.getString(cursor.getColumnIndex(dName));
            //String artPath = cursor.getString(cursor.getColumnIndex(albumart));
            //Bitmap art = BitmapFactory.decodeFile(artPath);
            //    int nr = Integer.parseInt(cursor.getString(cursor.getColumnIndex(tracks)));
            genreInfo g = new genreInfo(id, name,dName);
            genreList.add(g);
        } while (cursor.moveToNext());
        if (cursor != null) {
            cursor.close();
        }
        recyclerView2.setAdapter(genreAdapter1);
    }


    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }

    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }



    public interface OnFragmentInteractionListener {
        void onFragmentInteraction(Uri uri);
    }
}

genreAdapter

public class genreAdapter extends RecyclerView.Adapter<genreAdapter.AlbumHolder> {
    ArrayList<genreInfo> genreList=new ArrayList<>();
    Context context;

    public genreAdapter(ArrayList<genreInfo> genreList, Context context) {
        this.genreList = genreList;
        this.context = context;
    }

    @Override
    public genreAdapter.AlbumHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View myView = LayoutInflater.from(context).inflate(R.layout.genre,parent,false);
        return new AlbumHolder(myView);
    }

    @Override
    public void onBindViewHolder(AlbumHolder holder, int position) {
        final genreInfo a = genreList.get(position);
        holder.album.setText(genreList.get(position).getName());
        holder.artist.setText(genreList.get(position).getdName());
     //   holder.album_art.setImageDrawable(Drawable.createFromPath(String.valueOf(albumList.get(position).getAlbumImg())));

    }
    @Override
    public int getItemCount() {
        return genreList.size();
    }

    public class AlbumHolder extends RecyclerView.ViewHolder {
        public TextView album;
        public TextView artist;
        //public ImageView album_art;
        public AlbumHolder(View itemView) {
            super(itemView);
           // album_art = (ImageView) itemView.findViewById(R.id.albumArt);
            artist = (TextView) itemView.findViewById(R.id.artistName);
            album = (TextView) itemView.findViewById(R.id.songName);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

问题在于名为_display_name的列。检查此列的拼写。还要检查在创建audio_genres表时是否创建了此列。