获取错误:无法在Android中解析方法'makeText'

时间:2016-06-21 13:22:20

标签: android android-afilechooser

我是新手。 得到错误:

  

无法解析方法'makeText'

我正在使用导航抽屉,我的班级名称为MainFragment.java。 实际上我是试图使用文件和文件夹资源管理器,只有这个错误。

我尝试过:尝试使用MainFragment.getContext()getActivity()以及contextthis代替MainFragment.this,但没有一个正在使用。

MainFragment.java

public class MainFragment extends Fragment
{
    //Defined for file edittext.
    EditText editText2;

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

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

            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.fragment_main, container, false);
        }
    */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_main, container, false);

        Button dirChooserButton = (Button) view.findViewById(R.id.skipButton);       //Browse Button
        dirChooserButton.setOnClickListener(new OnClickListener()
        {
            private String m_chosenDir = "";
            private boolean m_newFolderEnabled = true;

            @Override
            public void onClick(View v)
            {
                // Create DirectoryChooserDialog and register a callback
                DirectoryChooserDialog directoryChooserDialog =
                        new DirectoryChooserDialog(MainFragment.this,
                                new DirectoryChooserDialog.ChosenDirectoryListener()
                                {
                                    @Override
                                   public void onChosenDir(String chosenDir)
                                    {
                                        m_chosenDir = chosenDir;
                                        Toast.makeText(
                                                getActivity(), "Chosen directory: " +
                                                        chosenDir, Toast.LENGTH_LONG).show();
                                    }
                                });
                // Toggle new folder button enabling
                directoryChooserDialog.setNewFolderEnabled(m_newFolderEnabled);
                // Load directory chooser dialog for initial 'm_chosenDir' directory.
                // The registered callback will be called upon final directory selection.
                directoryChooserDialog.chooseDirectory(m_chosenDir);
                m_newFolderEnabled = ! m_newFolderEnabled;
            }
        });

        @Override
        public void onChosenDir(String chosenDir)
        {
            m_chosenDir = chosenDir;
            Toast.makeText(MainFragment.this, "Chosen  directory: " +chosenDir,Toast.LENGTH_LONG).show();
        }
    }   
}                                                

DirectoryChooserDialog.java

实际上,我尝试使用此链接制作文件和文件夹选择器:link to the webpage

2 个答案:

答案 0 :(得分:1)

片段无法展示你的祝酒词,它必须来自活动。请尝试以下方法:

Toast.makeText(getActivity(), "Chosen directory: " + chosenDir, Toast.LENGTH_LONG).show();

答案 1 :(得分:0)

尝试这个 Toast.makeText(MainActivity.this,“已插入数据”,Toast.LENGTH_SHORT).show();

Mainactivity是类的名称