Reputation: 688
Please, I've been trying to add ListView to the android app am developing but it always starts and then crashes when I add the ListView. I need help. I've tried googling for help but found none. I don't know why its not working. Below is my MainActivity.java
public class MainActivity extends FragmentActivity {
private GoogleMap maap;
static final String[] OptionS = new String[]{"for 1","for 2","for 3","for 4"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView listView= (ListView) findViewById(R.id.listView1);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(),
((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
setContentView(R.layout.activity_main);
getActionBar().setDisplayHomeAsUpEnabled(true);
GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
maap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
maap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
maap.getUiSettings().setMyLocationButtonEnabled(true);
maap.getUiSettings().setAllGesturesEnabled(true);
CameraPosition Position =new CameraPosition.Builder()
.target(new LatLng(33.53,10.08))
.zoom(7.2f)
.tilt(40)
.build();
CameraUpdate Update = CameraUpdateFactory.newCameraPosition(Position);
maap.moveCamera(Update);
maap.addMarker(new MarkerOptions()
.position(new LatLng(33.53,10.08))
.title("TEST")
.draggable(true)
.snippet("is good"));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
startActivity(new Intent(this, ActivityMenus.class));
case R.id.item2:
startActivity(new Intent(this, ActivityMenus.class));
case R.id.item3:
startActivity(new Intent(this, ActivityMenus.class));
case R.id.item4:
startActivity(new Intent(this, ActivityMenus.class));
}
return true;
}
}
Upvotes: 0
Views: 76
Reputation: 86958
You must call setContentView()
before trying to use findViewById()
.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // Move me here
ListView listView= (ListView) findViewById(R.id.listView1);
// etc
Upvotes: 2