0

请,我一直在尝试将 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;
  }
} 
4

1 回答 1

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 
于 2013-01-13T20:56:22.960 回答