I have this code:
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);
}
}
});
}
}
Which is supposed to make a spinner with numbers from the edittext string, however when run it give an error like this:
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)
I’m very confused with the error and have been trying to solve it for 3 hours with no progress, so I hope some members can give me a solution.
Problem: spinner with numbers item from the edittext string length
Answer thanks to user 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();
}
}
};
}
and thanks to other member aswell
Because you didn’t allocate memory for your array “numbers”. You should do following :
And in your afterTextChanged() method you should create new Array with needed length (You can take it from editable). And then you can initialize all of elements of array and call notifyDataSetChanged() from adapter.
UPD
}