在运行时未找到的方法

时间:2014-01-27 02:58:43

标签: java android eclipse

我在myAndroid应用程序中使用org.apache.commons.io.FileUtils.readFileToByteArray方法。我导入了jar文件,我看到这个方法在我的项目中。 (我使用Eclipse。)实际上,编译是可以的。这是来自LogCat的消息:

01-26 18:43:08.177: I/dalvikvm(897): Could not find method org.apache.commons.io.FileUtils.readFileToByteArray, referenced from method com.example.anagrams.MainActivity.readFile

所以这不是一个错误,但是我得到了一个N​​ullPointerException,显然是因为声明:

private Button button_read_file = (Button) findViewById(R.id.button_readfile);

我不知道如何纠正NullPointerException。我也对未找到方法的消息感到困惑。该应用程序应该读取包含英语单词的文本文件,然后为每个单词找到所有存在的字谜。目前我只使用System.out.println来编写anagrams。

欢迎任何帮助。以下是我的整个MainActivity(目前唯一的一个):

public class MainActivity extends Activity {

private String[] words;
private Button button_read_file = (Button) findViewById(R.id.button_readfile);
private EditText input_file_name = (EditText) findViewById(R.id.editText1) ;
private Button button_write_file = (Button) findViewById(R.id.button_writefile);    
private EditText output_file_name = (EditText) findViewById(R.id.editText2) ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button_read_file.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            String inputFileName = input_file_name.getText().toString();
            try {
            words = readFile(inputFileName, Charset.defaultCharset());
            } catch (FileNotFoundException e) {
                e.getStackTrace();
            } catch (IOException ioe) {
                ioe.getStackTrace();
            }
        }});

    button_write_file.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
        for (int i = 0; i < words.length; ++i) {
            String s = words[i];
            System.out.println(" words[" + i + "] = " + s);
            HashSet<String> a = new HashSet<String>();
            permutations(s,a);
                Iterator<String> iterator = a.iterator();
                while(iterator.hasNext()) {
                    System.out.println(iterator.next());
                }
        }
        }});
}

public String[] readFile(String path, Charset encoding) throws IOException {                                                                                                                                              
    File file = new File(path);
    byte[] encoded = FileUtils.readFileToByteArray(file);
    String s = encoding.decode(ByteBuffer.wrap(encoded)).toString();
    s = s.replace(" ","");
    return s.split("[,.\\n\\s]");                                                                                                  
}                  

public ArrayList<String> getDictionary(String filename) throws IOException, FileNotFoundException {
    ArrayList<String> dictionary = new ArrayList<String>();
    FileReader fileReader = new FileReader(filename);
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    String line = null;
    while ((line = bufferedReader.readLine()) != null) {
        dictionary.add(line);
    }
    bufferedReader.close();
    return dictionary;
}

public boolean isInDictionary(String s) {
    String dictionaryname = "enable1.txt";
    ArrayList<String> dictionary = new ArrayList<String>();
    try {
    dictionary = getDictionary(dictionaryname);
    } catch (FileNotFoundException e) {
        e.getStackTrace();
    } catch (IOException e) {
        e.getStackTrace();
    }
    if (dictionary.contains(s))
        return true;
    else
        return false;
}

public void permutations(String word, HashSet<String> anagrams) {
    generatePermutations("",word,anagrams);
}

public void generatePermutations(String prefix, String word, HashSet<String> anagrams) {
    int n = word.length();
    if (n == 0) {
        if (prefix != word && isInDictionary(prefix))
            anagrams.add(prefix);
    }
    else {
        for (int i = 0; i < n; ++i) {
            generatePermutations(prefix + word.charAt(i),word.substring(0,i) + word.substring(i+1),anagrams);
        }
    }
}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

这是布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<Button
    android:id="@+id/button_readfile"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginTop="17dp"
    android:text="@string/read_file" />

<Button
    android:id="@+id/button_writefile"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button_readfile"
    android:layout_below="@+id/button_readfile"
    android:layout_marginTop="24dp"
    android:text="@string/write_file" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/button_readfile"
    android:layout_toRightOf="@+id/button_writefile"
    android:ems="10" >

    <requestFocus />

</EditText>

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/button_writefile"
    android:layout_alignBottom="@+id/button_writefile"
    android:layout_alignLeft="@+id/editText1"
    android:ems="10" >
</EditText>

1 个答案:

答案 0 :(得分:1)

在布局设置之前,您正在初始化这些:

private Button button_read_file = (Button) findViewById(R.id.button_readfile);
private EditText input_file_name = (EditText) findViewById(R.id.editText1) ;
private Button button_write_file = (Button) findViewById(R.id.button_writefile);    
private EditText output_file_name = (EditText) findViewById(R.id.editText2) ;

将它们更改为:

private Button button_read_file;
private EditText input_file_name;
private Button button_write_file;    
private EditText output_file_name;

然后在onCreate中的setContentView(...)之后声明它们,如:

button_read_file = (Button) findViewById(R.id.button_readfile);
input_file_name = (EditText) findViewById(R.id.editText1) ;
button_write_file = (Button) findViewById(R.id.button_writefile);    
output_file_name = (EditText) findViewById(R.id.editText2) ;

如果您看到NoClassDefFoundError,请在Eclipse中尝试:

  • 转到项目属性 Java构建路径订购和导出选项卡。
  • 检查 Android私有库选项。

Android Private Libraries option