所以我一直在Android Studio中处理这个Android项目,我让用户创建待办事项列表。这是课堂作业,我必须与单身人士合作。我一直在阅读很多关于它的信息和问题/答案,但这只会让我越来越困惑。
在我的MainActivity
我尝试调用方法ReadTitlesFromFile
,但我收到错误:
修改者' public'不允许在这里
和
内部类不能有静态声明
即使代码与我在某处找到的示例完全相同。有人可以帮我解决错误吗?
这是我的单身人士:
public class ToDoManagerSingleton {
private ArrayList<ToDoList> listTitles;
// One single instance for the entire app
private static ToDoManagerSingleton ourInstance = new ToDoManagerSingleton();
// Constructor
private ToDoManagerSingleton() {
// Empty ArrayList with in it the different lists
listTitles = new ArrayList<ToDoList>();
}
// Method to get the one single instance
public static ToDoManagerSingleton getInstance() {
return ourInstance;
}
// Getter and setter
public void setListTitles (ArrayList listTitlesArg){
listTitles = listTitlesArg;
}
public ArrayList getListTitles(){
return listTitles;
}
// Add new title to list
public void AddNewTitle(ToDoList title){
listTitles.add(title);
}
// Write list titles to a file
public void WriteListTitlesToFile (Context context){
PrintStream outstream = null;
try{
outstream = new PrintStream(context.openFileOutput("titlesfile.txt", Context.MODE_PRIVATE));
for (ToDoList listTitle : listTitles) {
outstream.println(listTitle);
}
}
catch (FileNotFoundException e){
e.printStackTrace();
}
}
// Read titlesfile.txt
public void ReadTitlesFromFile(Context context){
try {
FileInputStream inStream = context.openFileInput("titlesfile.txt");
InputStreamReader inReader = new InputStreamReader(inStream);
BufferedReader bufferedReader = new BufferedReader(inReader);
StringBuilder sBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null){
sBuilder.append(line).append("\n");
}
}
catch (FileNotFoundException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
}
// Read itemsfile.txt
public void ReadItemsFromFile(Context context){
try {
FileInputStream inStream = context.openFileInput("itemsfile.txt");
InputStreamReader inReader = new InputStreamReader(inStream);
BufferedReader bufferedReader = new BufferedReader(inReader);
StringBuilder sBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null){
sBuilder.append(line).append("\n");
}
}
catch (FileNotFoundException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
}
}
这是MainActivity
类:
public class MainActivity extends AppCompatActivity {
ListView showSavedFilesListview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize listview
showSavedFilesListview = (ListView) findViewById(R.id.ListView1);
// Read titles from titlesfile.txt
public class ReadTitles {
public static void main (Context context) {
ToDoManagerSingleton tmp = ToDoManagerSingleton.getInstance();
tmp.ReadTitlesFromFile(Context context);
}
}
}
我很抱歉,如果这是一个愚蠢的问题,或者之前已经被问过,但我是编程的新手,所有的信息都会让我感到困惑。
答案 0 :(得分:1)
您已在类中放置了一个类。这就是您收到错误的原因:"Modifier 'public' not allowed here"
无论如何你不需要班级ReadTitles
;即使您使用它,也请从static
方法中删除main
修饰符。
简单地说,而不是:
// Read titles from titlesfile.txt
public class ReadTitles {
public static void main (Context context) {
ToDoManagerSingleton tmp = ToDoManagerSingleton.getInstance();
tmp.ReadTitlesFromFile(Context context);
}
}
把这个:
// Read titles from titlesfile.txt
ToDoManagerSingleton tmp = ToDoManagerSingleton.getInstance();
tmp.ReadTitlesFromFile(this); // use the Activity context
答案 1 :(得分:0)
如前所述,您试图在ReadTitles
方法中声明类型为onCreate
的类。你不能这样做。
而不是:
public class ReadTitles {
public static void main (Context context) {
ToDoManagerSingleton tmp = ToDoManagerSingleton.getInstance();
tmp.ReadTitlesFromFile(Context context);
}
}
您只需在onCreate
内调用您的单身人士实例即可。但是,您需要先获取应用Context
,然后才能调用ReadTitlesFromFile
方法。目前,您在将参数传递给ReadTitlesFromFile
时尝试显式声明类型。你不这样做,因为它会导致编译器错误。而是使用getApplicationContext()
方法获取Context
。见下文:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize listview
showSavedFilesListview = (ListView) findViewById(R.id.ListView1);
// Read titles from titlesfile.txt
ToDoManagerSingleton tmp = ToDoManagerSingleton.getInstance();
Context context = getApplicationContext();
tmp.ReadTitlesFromFile(context);
}