I am trying to create bunch buttons for a android program, but I kept getting NullPointerException when I try to "connect" the buttons I created in the XML file to the buttons I created in the Java file. I been trying to figure out this error, was wondering if any of you would be able to shed some light
public class AddActivity extends ActionBarActivity {
private ExerciseLoader loader = new ExerciseLoader();
private ArrayList<Button> listofButton = new ArrayList<Button>();
private Button button=new Button(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
Intent intent = getIntent();
createButtons();
textButtons();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void createButtons() {
for (int i = 0; i < loader.getList().size(); i++) {
String buttonID = "btn" + i;
int resID = getResources().getIdentifier(buttonID, "id",
getPackageName());
listofButton.add(((Button) findViewById(resID)));
// listofButton.get(i).setText("This is a test");
}
}
}
Here is the error code
java.lang.RuntimeException: Unable to instantiate activity
ComponentInfo{com.example.alert/com.***.alert.AddActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method
'android.content.res.Resources android.content.Context.getResources()'
on a null object reference