Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.s
Bonjour,
Voici une erreur que j'ai rencontré et dont je n'arrive pas à me débarrasser :
Code:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
Que dois-je faire ? Je vous poste mon code ci dessous, Merci ! ( problème ligne 18 )
Code:
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| public class entery_names extends AppCompatActivity {
RelativeLayout layout;
RelativeLayout.LayoutParams params;
Button mBtn_add_et;
int id;
int i;
static int previousid;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Submit = (Button)loginDialog.findViewById(R.id.Submit);
mBtn_add_et = findViewById(R.id.btn_add_et);
mBtn_add_et.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (id <= 4) {
EditText et = new EditText(entery_names.this);
id = View.generateViewId();
et.setId(id);
layout = findViewById(R.id.relativeLayout);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, id);
if (previousid == 0)
params.addRule(RelativeLayout.BELOW, R.id.et3);
else
params.addRule(RelativeLayout.BELOW, previousid);
previousid = et.getId();
et.setHint("Enter Name");
et.setHintTextColor(getResources().getColor(R.color.colorTransparentWhite));
et.setTextColor(getResources().getColor(R.color.colorTransparentWhite));
et.setX(findViewById(R.id.et1).getX());
layout.addView(et, params);
}
}
});
}
} |