[英]Cannot find class symbol - Android Studio
我刚刚开始按照本教程构建一个Android应用程序。 一切都很顺利,直到我们制作一个Intent对象来调出NoteSelect活动的最后一步,尽管该类在同一个文件夹中。 这是错误 -
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
这是层次结构 -
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 (未提及琐碎的导入)
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;
}
}
AndroidManifest.xml
, content_note_select.xml
和activity_note_select.xml
似乎存在问题。
到目前为止,我已尝试在与MainActivity.java相同的包中包含NoteSelect.java ,它解决了NoteSelect类未找到的问题,但进一步导致NoteBuilder和NoteAdapter类在NoteSelect.java中找不到问题。 此外,由于在原始教程中没有提到这样的内容,我不喜欢它。
我出错的任何想法?
到目前为止,我已经尝试在与MainActivity.java相同的包中包含NoteSelect.java,它解决了NoteSelect类没有找到问题,但进一步导致找不到NoteBuilder和NoteAdapter类
嗯,这是正确的,但你还需要移动其他类而不是将它们留在src/main/java
文件夹中
Java无法解析当前包“之上”的类。
试试这个布局
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"
然后在MainActivity中
package com.example.memos;
import com.example.memos.notes.NoteSelectionActivity;
并且在NoteSelectionActivity中
package com.example.memos.notes;
// No imports needed for classes in the same package
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.