在Codepath`Todo App`教程(Android)中无法解析符号`FileUtils`

tim*_*xyz 5 debugging android android-studio

我正在尝试Todo从Codepath(https://guides.codepath.com)关注应用程序教程.

资料来源:https://guides.codepath.com/android/Basic-Todo-App-Tutorial

据我所知,我已经按照教程,但我得到了以下问题.

Cannot resolve symbol `FileUtils`
Run Code Online (Sandbox Code Playgroud)

MainActivity.Java

package com.codepath.simpletodo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView; 
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private ArrayList<String> items;
    private ArrayAdapter<String> itemsAdapter;
    private ListView lvItems;

    private void readItems() {
        File filesDir = getFilesDir();
        File todoFile = new File(filesDir, "todo.txt");
        try {
            items = new ArrayList<String>(FileUtils.readLines(todoFile));
        } catch (IOException e) {
            items = new ArrayList<String>();
        }
    }

    private void writeItems() {
        File filesDir = getFilesDir();
        File todoFile = new File(filesDir, "todo.txt");
        try {
            FileUtils.writeLines(todoFile, items);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lvItems = (ListView) findViewById(R.id.lvItems);
        items = new ArrayList<String>();
        itemsAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, items);
        lvItems.setAdapter(itemsAdapter);
        items.add("Default item");
        items.add("Another default item");
        items.add("The third default item");
        // setup remove listener method call
        setupListViewListener();
    }

    // Attaches a long click listener to the listview
    private void setupListViewListener() {
        lvItems.setOnItemLongClickListener(
                new AdapterView.OnItemLongClickListener() {
                    @Override
                    public boolean onItemLongClick(AdapterView<?> adapter,
                                                   View item, int pos, long id) {
                        // Remove item within array at position
                        items.remove(pos);
                        // Refresh the adapter
                        itemsAdapter.notifyDataSetChanged();
                        // Return true consumes the long click event (marks it handled)
                        return true;
                    }
                });
    }

    public void onAddItem(View v) {
        EditText etNewItem = (EditText) findViewById(R.id.etNewItem);
        String itemText = etNewItem.getText().toString();
        itemsAdapter.add(itemText);
        etNewItem.setText("");
    }
}
Run Code Online (Sandbox Code Playgroud)

activity_mail.xml

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/lvItems"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_above="@+id/btnAddItem" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/etNewItem"
    android:layout_alignTop="@+id/btnAddItem"
    android:hint="Enter a new item"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_toLeftOf="@+id/btnAddItem"
    android:layout_toStartOf="@+id/btnAddItem"
    android:layout_alignParentBottom="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Add Item"
    android:id="@+id/btnAddItem"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:onClick="onAddItem"
    />
Run Code Online (Sandbox Code Playgroud)

我对Android非常陌生,只是为了让事情发挥作用而感到高兴,因为我还没有足够的背景来调试.

任何指导都将非常感激.

更新1

应用程序/的build.gradle

原始格式

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.0"

    defaultConfig {
        applicationId "com.codepath.simpletodo"
        minSdkVersion 14
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.0.0'
}
Run Code Online (Sandbox Code Playgroud)

我尝试过各种各样的组合'org.apache.commons:commons-io:1.3.2'.

  1. 替换'com.android.support:appcompat-v7:24.0.0''org.apache.commons:commons-io:1.3.2'
  2. 将两者放在同一条线上:compile 'com.android.support:appcompat-v7:24.0.0', 'org.apache.commons:commons-io:1.3.2'.
  3. 两者分开:compile 'com.android.support:appcompat-v7:24.0.0' compile 'org.apache.commons:commons-io:1.3.2'.

但是在尝试时Gradle project sync,下面的消息会出现故障.

Error:(24, 13) Failed to resolve: compile org.apache.commons:commons-io:1.3.2
Run Code Online (Sandbox Code Playgroud)

更新2

最终的工作版本需要看起来像这样.

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.0.0'
    compile group: 'org.apache.commons', name: 'commons-io', version: '1.3.2'
}
Run Code Online (Sandbox Code Playgroud)

Shr*_*hna 21

implementation 'org.apache.commons:commons-io:1.3.2'包含FileUtils类.您应该将此添加到您的依赖项.


USK*_*ity 5

我认为,在您的build.gradle中缺少以下几行

dependencies {
    compile 'org.apache.commons:commons-io:1.3.2'
}
Run Code Online (Sandbox Code Playgroud)

将以上行添加到您的build.gradle