i am trying to add a textview and a button programmatically in a if statement (if name == ""), but when i try to do so i get an error.
if(name == ""){
RelativeLayout rl = (RelativeLayout) findViewById(R.id.main);
TextView txt1 = new TextView(MyActivity.this);
txt1.setText("add");
Button add = new Button(MyActivity.this);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MyActivity.this, fourm.class));
}
});
rl.addView(txt1);
rl.addView(add);
}
else {
}
error code
Caused by: java.lang.NullPointerException
at app.com.pickup.MyActivity.onCreate(MyActivity.java:58)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
line 58 of MainActivity?