2015-09-16 95 views
0

首先關閉在Android和Java的新的。開始新的活動,獲取不是封閉的類錯誤

我有一個MainActivity,登錄到Facebook,然後我想要OnSuccess來啓動一個Map.Activity類,但它不會,它說這個類不是封閉的。我無法找出它的含義,我沒有足夠的知識將它與關於此問題的其他答案聯繫起來。

我的兩個類看起來像這樣;

MainActivity.java

package com.example.nan.spymap; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.TextView; 

import com.facebook.CallbackManager; 
import com.facebook.FacebookCallback; 
import com.facebook.FacebookException; 
import com.facebook.FacebookSdk; 
import com.facebook.login.LoginResult; 
import com.facebook.login.widget.LoginButton; 

public class MainActivity extends Activity { 

    private CallbackManager callbackManager; 

    private TextView info; 
    private LoginButton loginButton; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     FacebookSdk.sdkInitialize(getApplicationContext()); 
     callbackManager = CallbackManager.Factory.create(); 

     setContentView(R.layout.activity_main); 



     setContentView(R.layout.activity_main); 
     info = (TextView)findViewById(R.id.info); 
     loginButton = (LoginButton)findViewById(R.id.login_button); 

     loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { 
      @Override 
      public void onSuccess(LoginResult loginResult) { 
       startActivity(new Intent(MapActivity.this, MapActivity.class)); 

      } 

      @Override 
      public void onCancel() { 
       info.setText("Login attempt cancelled."); 
      } 

      @Override 
      public void onError(FacebookException e) { 
       info.setText("Login attempt failed."); 
      } 
     }); 

    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     callbackManager.onActivityResult(requestCode, resultCode, data); 
    } 
} 

和MapActivity.java

package com.example.nan.spymap; 

import android.support.v4.app.FragmentActivity; 
import android.os.Bundle; 

import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.MapFragment; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.UiSettings; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.Marker; 
import com.google.android.gms.maps.model.MarkerOptions; 
import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.OnMapReadyCallback; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 

public class MapActivity extends FragmentActivity implements OnMapReadyCallback { 

    private GoogleMap mMap; // Might be null if Google Play services APK is not available. 
    private MapFragment mMapFragment; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_maps); 
     setUpMapIfNeeded(); 

     /* MapFragment mapFragment = (MapFragment) getFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 
    */} 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     setUpMapIfNeeded(); 
    } 
    @Override 
    public void onMapReady(GoogleMap map) { 
     // Add a marker in Sydney, Australia, and move the camera. 
     map.addMarker(new MarkerOptions() 
       .position(new LatLng(10, 10)) 
       .title("Hello world").draggable(true)); 
     mMap.setMyLocationEnabled(true); // false to disable 
    } 
    /** 
    * Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly 
    * installed) and the map has not already been instantiated.. This will ensure that we only ever 
    * call {@link #setUpMap()} once when {@link #mMap} is not null. 
    * <p/> 
    * If it isn't installed {@link SupportMapFragment} (and 
    * {@link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to 
    * install/update the Google Play services APK on their device. 
    * <p/> 
    * A user can return to this FragmentActivity after following the prompt and correctly 
    * installing/updating/enabling the Google Play services. Since the FragmentActivity may not 
    * have been completely destroyed during this process (it is likely that it would only be 
    * stopped or paused), {@link #onCreate(Bundle)} may not be called again so we should call this 
    * method in {@link #onResume()} to guarantee that it will be called. 
    */ 
    private void setUpMapIfNeeded() { 
     // Do a null check to confirm that we have not already instantiated the map. 
     if (mMap == null) { 
      // Try to obtain the map from the SupportMapFragment. 
      mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) 
        .getMap(); 
      // Check if we were successful in obtaining the map. 
      if (mMap != null) { 
       setUpMap(); 
      } 
     } 
    } 

    /** 
    * This is where we can add markers or lines, add listeners or move the camera. In this case, we 
    * just add a marker near Africa. 
    * <p/> 
    * This should only be called once and when we are sure that {@link #mMap} is not null. 
    */ 
    private void setUpMap() { 
     mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker")); 
    } 
} 

回答

3

你的活動被稱爲MainActivity,但你要使用MapActivity.this作爲第一個參數爲new Intent(MapActivity.this, MapActivity.class)。更改爲MainActivity.this

startActivity(new Intent(MainActivity.this, MapActivity.class)); 
+0

那麼修復編譯錯誤..但現在我的應用程序將不會登錄。你有什麼建議嗎?它設置正確的錯誤壽,登錄嘗試失敗 – NicklasN

+0

不知道對不起。你可能想檢查文檔 – Blackbelt