拜托,我一直在尝试将ListView添加到正在开发的Android应用程序中但它总是启动然后在我添加ListView时崩溃。我需要帮助。我试过谷歌搜索帮助,但没有找到。我不知道为什么它不起作用。下面是我的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;
}
}
答案 0 :(得分:2)
在尝试使用setContentView()
之前,您必须先致电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