所以我一直在Android Studio中處理這個Android項目,我讓用戶創建待辦事項列表。這是一個班級任務,我必須與單身人士合作。我一直在閱讀大量關於它的信息和問題/答案,但這隻會讓我越來越困惑。在活動中使用Singleton方法(Android)
在我MainActivity
我試圖調用該方法ReadTitlesFromFile
,但我得到的錯誤:
Modifier 'public' not allowed here
和
Inner classes cannot have static declarations
即使代碼是完全一樣的,我發現的例子某處。有人可以幫我解決這個錯誤嗎?
這是我的單身:
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);
}
}
}
對不起,如果這是一個愚蠢的問題,或者如果它有之前已經被問過了,但我是編程新手,所有的信息都會讓我困惑。
據我所知,android是一樣的正常的java程序,所以你不能運行與公共靜態void main方法的代碼。這段代碼需要針對android生命週期方法調用,onCreate等運行。 – Simon
在方法中不能有類聲明。在這種情況下,你可以在'onCreate'中聲明一個類('ReadTitles')。只需在'onCreate'方法中獲取單例實例即可。 – TEK