简体   繁体   English

找不到类符号--Android Studio

[英]Cannot find class symbol - Android Studio

I have just started building an android app following this tutorial. 我刚刚开始按照教程构建一个Android应用程序。 Everything went fine till the last step where we are making an Intent object to bring up the NoteSelect activity, despite the class being in the same folder. 一切都很顺利,直到我们制作一个Intent对象来调出NoteSelect活动的最后一步,尽管该类在同一个文件夹中。 Here is the error - 这是错误 -

Information:Gradle tasks [:app:assembleDebug]
F:\Path\Memos\app\src\main\java\com\example\memos\memos\MainActivity.java
Error:(99, 57) error: cannot find symbol class NoteSelect
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
Information:BUILD FAILED in 19s
Information:2 errors
Information:0 warnings
Information:See complete output in console

Here is the hierarchy - 这是层次结构 -

在此输入图像描述

MainActivity.java (onOptionsItemSelected()) MainActivity.java (onOptionsItemSelected())

package com.example.memos.memos;

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        Intent myIntent = new Intent(MainActivity.this, NoteSelect.class);
        if (id == R.id.action_settings) {
            startActivity(myIntent);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

NoteSelect.java (didn't mention trivial imports) NoteSelect.java (未提及琐碎的导入)

import com.example.memos.memos.R;

public class NoteSelect extends AppCompatActivity {

    private List <NotesBuilder> notesList = new ArrayList<>();
    private NotesAdapter nAdapter;
    private RecyclerView notesRecycler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_note_select);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        notesRecycler = findViewById(R.id.notes);

        nAdapter = new NotesAdapter(notesList);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        notesRecycler.setLayoutManager(mLayoutManager);
        notesRecycler.setItemAnimator(new DefaultItemAnimator());
        notesRecycler.setAdapter(nAdapter);

        prepareNotes();

    }

    private void prepareNotes() {
        File directory;
        directory = getFilesDir();
        File[] files = directory.listFiles();
        String theFile;
        for (int f = 1; f <= files.length; f++) {
            theFile = "Note" + f + ".txt";
            NotesBuilder note = new NotesBuilder(theFile, Open(theFile));
            notesList.add(note);
        }

    }

    public String Open(String fileName) {
        String content = "";
        try {
            InputStream in = openFileInput(fileName);
            if ( in != null) {
                InputStreamReader tmp = new InputStreamReader( in );
                BufferedReader reader = new BufferedReader(tmp);
                String str;
                StringBuilder buf = new StringBuilder();
                while ((str = reader.readLine()) != null) {
                    buf.append(str + "\n");
                } in .close();

                content = buf.toString();
            }
        } catch (java.io.FileNotFoundException e) {} catch (Throwable t) {
            Toast.makeText(this, "Exception: " + t.toString(), Toast.LENGTH_LONG).show();
        }

        return content;
    }
}

Issue seems to be there with AndroidManifest.xml , content_note_select.xml and activity_note_select.xml . AndroidManifest.xmlcontent_note_select.xmlactivity_note_select.xml似乎存在问题。

在此输入图像描述

So far I have tried including NoteSelect.java in the same package as MainActivity.java that resolves the NoteSelect class not found issue but further leads to NoteBuilder and NoteAdapter class not found the issue in NoteSelect.java . 到目前为止,我已尝试在与MainActivity.java相同的包中包含NoteSelect.java ,它解决了NoteSelect类未找到的问题,但进一步导致NoteBuilderNoteAdapter类在NoteSelect.java中找不到问题。 Moreover, since nothing like this has been mentioned in the original tutorial I don't prefer it anyway. 此外,由于在原始教程中没有提到这样的内容,我不喜欢它。

Any ideas where I am going wrong? 我出错的任何想法?

So far I have tried including NoteSelect.java in the same package as MainActivity.java that resolves the NoteSelect class not found the issue but further leads to NoteBuilder and NoteAdapter class not found 到目前为止,我已经尝试在与MainActivity.java相同的包中包含NoteSelect.java,它解决了NoteSelect类没有找到问题,但进一步导致找不到NoteBuilder和NoteAdapter类

Well, that was correct, but you also need to move the other classes instead of leaving them in src/main/java folder 嗯,这是正确的,但你还需要移动其他类而不是将它们留在src/main/java文件夹中

Java cannot resolve classes "above" the current package. Java无法解析当前包“之上”的类。

Try this layout 试试这个布局

com.example.memos/
    MainActivity.java
    notes/
        NoteSelectionActivity.java   <--- It's an activity, so rename your class to this
        NotesAdapter.java
        Note.java  <---- A POJO class. // There's no reason to name it "Builder" when it isn't using a "Builder Pattern"

Then in the MainActivity 然后在MainActivity中

package com.example.memos;

import com.example.memos.notes.NoteSelectionActivity;

And in the NoteSelectionActivity 并且在NoteSelectionActivity中

package com.example.memos.notes;

// No imports needed for classes in the same package

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM