所以基本上这个代码应该做的是拼写检查从EditText
字段作为输入的某个单词,并在TextFields
中显示建议。
getAll()
是调用所有其他需要执行的操作的函数。
在所有功能执行完毕后,Public member variable "list"
包含所有建议。
最后一项功能sort()
添加了List
list
中的所有建议。在onClick()
我们正在尝试将它们打印到textfield
。但
t2.setText(list.get(1).toString());
返回NullPointerException
错误。
在eclipse上独立运行时,拼写检查代码完全正常。
这是错误日志:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.daipayan.myapplication, PID: 2667
java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
at com.example.daipayan.myapplication.MainActivity$1.onClick(MainActivity.java:69)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
代码:
public class MainActivity extends AppCompatActivity {
public static char keyboard[][] = {{'0','0','0','0','0','0','0','0','0','0','0','0'},
{'0','Q','W','E','R','T','Y','U','I','O','P','0'}, //mapping the keyboard to a multidimensional array
{'0','0','A','S','D','F','G','H','J','K','L','0'},
{'0','0','0','Z','X','C','V','B','N','M','0','0'},
{'0','0','0','0','0','0','0','0','0','0','0','0'}};
public static String possibleArray[];
public static ArrayList<String> comb;
public static ArrayList<String> filtered;
public static Set<String> dictionary;
public static HashMap<String , Integer> distances;
public static List list;
public static String incorrect_word;
EditText input;
TextView t1,t2,t3;
Button bt1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input=(EditText)findViewById(R.id.editText);
bt1 = (Button)findViewById(R.id.button);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String inputText = input.getText().toString();
String words[] = inputText.split("\\s");
String lastWord = words[words.length-1];
incorrect_word = "HELLP";
try {
getAll();
} catch (IOException e) {
e.printStackTrace();
}
t1 = (TextView)findViewById(R.id.text1);
t2 = (TextView)findViewById(R.id.text2);
t3 = (TextView)findViewById(R.id.text3);
t1.setText(""+list.get(0));
t2.setText(list.get(1).toString());
t3.setText(list.get(2).toString());
}
});
}
public static void getAll() throws IOException {
loadDictionary();
Log.d("getall","called");
//System.out.println(checkWord(incorrect_word));
//
//fetching the location of characters on the keyboard
//
int index[][] = new int[incorrect_word.length()][2];
for ( int i = 0 ; i < incorrect_word.length() ; i++){
int xy[] = locate(incorrect_word.charAt(i));
index[i][0]=xy[0];
index[i][1]=xy[1];
}
//print(index, incorrect_word.length(), 2);
//
//Fetching all the possible words with the
//
ArrayList<String> possible = characters(incorrect_word, index);
{
possibleArray = possible.toArray(new String[0]);
comb = new ArrayList<>();
dfs("",0);
}
filtered = new ArrayList<String>();
filterCombinations(); //filter out the words that aren't in the dictionary
Levenshtein(incorrect_word);//Calculate distances between words and the incorrect word
printHash();//Print the hash map consisting of the words and their respective distances
sort();
}
public static int[] locate(char a){
int i=1,j=0;
for( ; i < 4 ; i++ ){
for( j=1 ; j < 11; j++ ){
if(keyboard[i][j]==a)
return new int[] {i,j};
}
}
return new int[] {i,j};
}
public static ArrayList<String> characters(String incorrect_word, int index[][]){
ArrayList<String> possible = new ArrayList<String>();
for ( int i = 0 ; i < incorrect_word.length() ; i++){
String character=""+incorrect_word.charAt(i);
int x=index[i][0] , y=index[i][1];
String a = (keyboard[x-1][y] != '0' ? character+=keyboard[x-1][y]: "0");//Upper Right
a = (keyboard[x-1][y-1] != '0' ? character+=keyboard[x-1][y-1]: "0");//Upper Left
a = (keyboard[x][y-1] != '0' ? character+=keyboard[x][y-1]: "0");//left
a = (keyboard[x][y+1] != '0' ? character+=keyboard[x][y+1]: "0");//right
a = (keyboard[x+1][y] != '0' ? character+=keyboard[x+1][y]: "0");//Lower Left
a = (keyboard[x+1][y+1] != '0' ? character+=keyboard[x+1][y+1]: "0");//Lower Right
possible.add(character);
}
return possible;
}
public static void dfs(String x,int i) {
if(i == possibleArray.length) { // there is no more string that can be generated
if(checkWord(x))
comb.add(x); // save the found string
return;
}
for(int j=0;j<possibleArray[i].length();j++) // for each character in the current string
dfs(x+possibleArray[i].charAt(j),i+1); // take the current character and move to the next string
}
public static void loadDictionary() throws IOException{
BufferedReader dictReader = new BufferedReader(new FileReader("dictionary.txt"));
dictionary = new HashSet<>();
while (dictReader.ready())
{
String dictInput = dictReader.readLine() ;
String [] dict = dictInput.split("\\s");
for(int i = 0; i < dict.length;i++)
{
// key and value are identical
dictionary.add(dict[i]);
}
}
dictReader.close();
}
public static Boolean checkWord(String str){
String toCheck = str.toLowerCase();
if(dictionary.contains(toCheck)){
return true;
}
else
return false;
}
public static void filterCombinations(){ for(String x:comb)
if ( checkWord(x) )
filtered.add(x);
}
public static void printHash(){
Iterator iterator = distances.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next().toString();
String value = distances.get(key).toString();
System.out.println(key + " " + value);
}
}
public static void Levenshtein(String incorrect_word){
distances = new HashMap<String, Integer>();
for(String x:filtered)
distances.put(x,distance(incorrect_word , x ));
}
public static int distance(String a, String b) {
a = a.toLowerCase();
b = b.toLowerCase();
// i == 0
int [] costs = new int [b.length() + 1];
for (int j = 0; j < costs.length; j++)
costs[j] = j;
for (int i = 1; i <= a.length(); i++) {
// j == 0; nw = lev(i - 1, j)
costs[0] = i;
int nw = i - 1;
for (int j = 1; j <= b.length(); j++) {
int cj = Math.min(1 + Math.min(costs[j], costs[j - 1]), a.charAt(i - 1) == b.charAt(j - 1) ? nw : nw + 1);
nw = costs[j];
costs[j] = cj;
}
}
return costs[b.length()];
}
public static void sort(){
list=new ArrayList(distances.entrySet());
Collections.sort(list,new Comparator(){
public int compare(Object obj1, Object obj2){
return ((Comparable)((Entry)(obj1)).getValue()).compareTo(((Entry)(obj2)).getValue());
}
});
if(list.size() > 3)
list.subList(4, list.size()).clear();
Log.e("list","the values " + list.get(0) + list.get(1)+list.get(2));
}
}
答案 0 :(得分:0)
根据你的列表有两个元素,你得到第三个元素,所以它在t3.setText(list.get(2).toString());