2015-09-16 53 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