声明"mScs.getSuggestions(new TextInfo(without2), 1);"
不断获取
java.lang.NullPointerException在真实设备中运行但它有效 好在模拟器中。
我可以知道如何解决它吗?
MainActivity.java:
public class MainActivity extends Activity implements SpellCheckerSession.SpellCheckerSessionListener {
TextView tv1, tv2, tv3;
Button b1;
EditText et1;
private SpellCheckerSession mScs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.btn1);
tv1 = (TextView) findViewById(R.id.tv1);
tv2 = (TextView) findViewById(R.id.tv2);
tv3 = (TextView) findViewById(R.id.tv3);
et1 = (EditText) findViewById(R.id.et1);
final String[][] T9 = {
/* 0 */ {"a", "b", "c"},
/* 1 */ {"d", "e", "f"},
/* 2 */ {"g", "h", "i"},
/* 3 */ {"j", "k", "l"},
/* 4 */ {"m", "n", "o"},
/* 5 */ {"p", "q", "r", "s"},
/* 6 */ {"t", "u", "v"},
/* 7 */ {"w", "x", "y", "z"}
};
final ArrayList<String> prevalue = new ArrayList<String>();
final ArrayList<String> buff = new ArrayList<String>();
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
prevalue.clear();
buff.clear();
tv1.setText("");
tv2.setText("");
tv3.setText("");
for(int et = 0; et< et1.length(); et++) {
String subvalue = et1.getText().toString().substring(et, et + 1);
if(prevalue.size() !=0){
buff.add(prevalue.toString());
prevalue.clear();
}
for (int ii = 0; ii < T9[Integer.parseInt(subvalue)].length; ii++) {
if(buff.size() != 0){
String[] buffarray = buff.toString().split("\\s*,\\s*");
for(int ba = 0; ba<buffarray.length; ba++){
prevalue.add(buffarray[ba].toString() + T9[Integer.parseInt(subvalue)][ii].toString());
}
}else{
prevalue.add(T9[Integer.parseInt(subvalue)][ii]);
}
}
buff.clear();
}
tv1.append("prevalue size: " + prevalue.size() + '\n');
for(int lp = 0; lp < prevalue.size(); lp++){
String without1 = prevalue.get(lp).toString().replace("[", "");
String without2 = without1.replace("]", "");
tv1.append(without2 + '\n');
mScs.getSuggestions(new TextInfo(without2), 1);
}
}
});
}
public void onResume() {
super.onResume();
final TextServicesManager tsm = (TextServicesManager)
getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE);
***mScs = tsm.newSpellCheckerSession(null, null, this, true);***
}
public void onPause() {
super.onPause();
if (mScs != null) {
mScs.close();
}
}
public void onGetSuggestions(final SuggestionsInfo[] arg0) {
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < arg0.length; ++i) {
final int len = arg0[i].getSuggestionsCount();
sb.append('\n');
for (int j = 0; j < len; ++j) {
sb.append(arg0[i].getSuggestionAt(j));
}
//sb.append(" (" + len + ")");
}
runOnUiThread(new Runnable() {
public void run() {
tv2.append(sb.toString());
}
});
}
@Override
public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] arg0) {
// TODO Auto-generated method stub
}
}