我有这个代码:
public class SentSMSActivity extends Activity {
private Button btnSend;
private EditText etPlain, etCipher, etDest, key2;
private String plaintext, tempCipher = null, ciphertext = "";
private int keyRF, keyCaesar;
private CheckBox cbEncrypt;
private String dest, msgSend;
private Spinner sp;
private String numbers[];
private ArrayAdapter<String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sentsms_activity);
btnSend = (Button) findViewById(R.id.btnSent);
etPlain = (EditText) findViewById(R.id.etPlainSent);
etDest = (EditText) findViewById(R.id.etDestSent);
key2 = (EditText) findViewById(R.id.etKey2Sent);
cbEncrypt = (CheckBox) findViewById(R.id.cbEnc);
sp = (Spinner) findViewById(R.id.spKey1);
// String numbers[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
// "10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
// "20", "21", "22", "23", "24", "25" };
key2.setEnabled(false);
keyRF = 2;
final TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i,
int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1,
int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
for (int k = 0; k < etPlain.length() - 1; k++) {
numbers[k] = String.valueOf(k);
}
}
};
//third, we must add the textWatcher to our EditText
sp.setClickable(false);
cbEncrypt
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (cbEncrypt.isChecked()) {
key2.setEnabled(true);
sp.setClickable(true);
etPlain.addTextChangedListener(textWatcher);
adapter = new ArrayAdapter<String>(
SentSMSActivity.this,
android.R.layout.simple_spinner_item,
numbers);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
} else {
key2.setText("");
key2.setEnabled(false);
sp.setClickable(false);
}
}
});
}
}
它应该用edittext
字符串中的数字制作一个微调器,但是在运行时它会给出如下错误:
02-06 20:02:21.205: E/AndroidRuntime(513): FATAL EXCEPTION: main
02-06 20:02:21.205: E/AndroidRuntime(513): java.lang.NullPointerException
02-06 20:02:21.205: E/AndroidRuntime(513): at java.util.Arrays$ArrayList.<init>(Arrays.java:47)
02-06 20:02:21.205: E/AndroidRuntime(513): at java.util.Arrays.asList(Arrays.java:169)
02-06 20:02:21.205: E/AndroidRuntime(513): at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:125)
02-06 20:02:21.205: E/AndroidRuntime(513): at com.hery.androidkripto.SentSMSActivity$2.onCheckedChanged(SentSMSActivity.java:97)
02-06 20:02:21.205: E/AndroidRuntime(513): at android.widget.CompoundButton.setChecked(CompoundButton.java:124)
02-06 20:02:21.205: E/AndroidRuntime(513): at android.widget.CompoundButton.toggle(CompoundButton.java:86)
02-06 20:02:21.205: E/AndroidRuntime(513): at android.widget.CompoundButton.performClick(CompoundButton.java:98)
02-06 20:02:21.205: E/AndroidRuntime(513): at android.view.View$PerformClick.run(View.java:9080)
02-06 20:02:21.205: E/AndroidRuntime(513): at android.os.Handler.handleCallback(Handler.java:587)
02-06 20:02:21.205: E/AndroidRuntime(513): at android.os.Handler.dispatchMessage(Handler.java:92)
02-06 20:02:21.205: E/AndroidRuntime(513): at android.os.Looper.loop(Looper.java:123)
02-06 20:02:21.205: E/AndroidRuntime(513): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-06 20:02:21.205: E/AndroidRuntime(513): at java.lang.reflect.Method.invokeNative(Native Method)
02-06 20:02:21.205: E/AndroidRuntime(513): at java.lang.reflect.Method.invoke(Method.java:507)
02-06 20:02:21.205: E/AndroidRuntime(513): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-06 20:02:21.205: E/AndroidRuntime(513): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-06 20:02:21.205: E/AndroidRuntime(513): at dalvik.system.NativeStart.main(Native Method)
我对这个错误很困惑,已经尝试解决了3个小时没有任何进展,所以我希望一些成员能给我一个解决方案。
问题:带有来自edittext字符串长度的数字项的微调器
感谢用户 vetal-lebed 回答:
public class MyActivity extends Activity {
private ArrayList <String> list = new ArrayList<String>();
private EditText et;
private Spinner spinner;
private ArrayAdapter<String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
spinner = (Spinner)findViewById(R.id.spinner);
et = (EditText)findViewById(R.id.et);
et.addTextChangedListener(watcher);
adapter = new ArrayAdapter<String>(MyActivity.this,android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
private TextWatcher watcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
public void afterTextChanged(Editable s) {
for (int i = 0; i < s.length(); i ++){
list.add(String.valueOf(s.charAt(i)));
adapter.notifyDataSetChanged();
}
}
};
}
并感谢其他成员