我刚刚开始在this教程之后构建一个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
似乎存在问题。
到目前为止,我已尝试将 NoteSelect.java 包含在与MainActivity.java相同的包中,以解决 NoteSelect类未找到问题,但进一步导致 NoteBuilder 和 NoteAdapter 类未在 NoteSelect.java 中找到问题。此外,由于原始教程中没有提到这样的内容,我不喜欢它。
我出错的任何想法?
答案 0 :(得分:1)
到目前为止,我已经尝试将NoteSelect.java包含在与MainActivity.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