Java 启动新活动时,获取不是封闭类错误

Java 启动新活动时,获取不是封闭类错误,java,android,Java,Android,首先,我是android和java的新手 我有一个使用facebook登录的MainActivity,然后我希望OnSuccess启动一个Map.Activity类,但它不会,它说该类不是封闭的。我不知道它是什么意思,我也没有足够的知识将它与其他已回答的问题联系起来 我的两个班级是这样的 MainActivity.java package com.example.nan.spymap; import android.app.Activity; import android.content.In

首先,我是android和java的新手

我有一个使用facebook登录的MainActivity,然后我希望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);
    }
}
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"));
    }
}
package com.example.nan.spymap;
导入android.app.Activity;
导入android.content.Intent;
导入android.os.Bundle;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.widget.TextView;
导入com.facebook.CallbackManager;
导入com.facebook.facebook回调;
导入com.facebook.facebook异常;
导入com.facebook.FacebookSdk;
导入com.facebook.login.LoginResult;
导入com.facebook.login.widget.LoginButton;
公共类MainActivity扩展了活动{
私人CallbackManager CallbackManager;
私有文本视图信息;
私人登录按钮登录按钮;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
sdkinInitialize(getApplicationContext());
callbackManager=callbackManager.Factory.create();
setContentView(R.layout.activity_main);
setContentView(R.layout.activity_main);
info=(TextView)findViewById(R.id.info);
loginButton=(loginButton)findviewbyd(R.id.login_按钮);
registerCallback(callbackManager,newfacebookcallback()){
@凌驾
成功时公共无效(LoginResult LoginResult){
startActivity(新意图(MapActivity.this、MapActivity.class));
}
@凌驾
公开作废{
info.setText(“登录尝试已取消”);
}
@凌驾
公共无效onError(FaceBook例外e){
info.setText(“登录尝试失败”);
}
});
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
callbackManager.onActivityResult(请求代码、结果代码、数据);
}
}
以及MapActivity.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);
    }
}
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"));
    }
}
package com.example.nan.spymap;
导入android.support.v4.app.FragmentActivity;
导入android.os.Bundle;
导入com.google.android.gms.maps.GoogleMap;
导入com.google.android.gms.maps.MapFragment;
导入com.google.android.gms.maps.SupportMapFragment;
导入com.google.android.gms.maps.UiSettings;
导入com.google.android.gms.maps.model.LatLng;
导入com.google.android.gms.maps.model.Marker;
导入com.google.android.gms.maps.model.MarkerOptions;
导入android.os.Bundle;
导入android.support.v4.app.FragmentActivity;
导入com.google.android.gms.maps.CameraUpdateFactory;
导入com.google.android.gms.maps.GoogleMap;
导入com.google.android.gms.maps.OnMapReadyCallback;
导入com.google.android.gms.maps.SupportMapFragment;
导入com.google.android.gms.maps.model.LatLng;
导入com.google.android.gms.maps.model.MarkerOptions;
公共类MapActivity扩展了FragmentActivity在MapReadyCallback上的实现{
私有GoogleMap mMap;//如果Google Play services APK不可用,则可能为空。
私人地图碎片;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_映射);
setupmapifneed();
/*MapFragment MapFragment=(MapFragment)getFragmentManager()
.findFragmentById(R.id.map);
getMapAsync(这个);
*/}
@凌驾
受保护的void onResume(){
super.onResume();
setupmapifneed();
}
@凌驾
已于4月1日公开作废(谷歌地图){
//在澳大利亚悉尼添加一个标记,然后移动相机。
addMarker(新的MarkerOptions()
.位置(新车床(10,10))
.title(“Hello world”).draggable(真实));
mMap.setMyLocationEnabled(true);//false禁用
}
/**
*如果可能的话,设置地图(例如,Google Play services APK已正确启动)
*已安装)并且映射尚未实例化。这将确保
*当{@link#mMap}不为null时,调用{@link#setUpMap()}一次。
*

*如果未安装{@link SupportMapFragment}(和 *{@link com.google.android.gms.maps.MapView MapView})将提示用户 *在其设备上安装/更新Google Play services APK。 *

*用户可以按照提示正确返回此FragmentActivity *安装/更新/启用Google Play服务。因为碎片活动可能不会 *在这一过程中被完全破坏(很可能 *停止或暂停),{@link#onCreate(Bundle)}可能不会被再次调用,因此我们应该调用它 *方法来保证将调用它。 */ 私有void setUpMapIfNeeded(){ //执行空检查以确认尚未实例化映射。 如果(mMap==null){ //尝试从SupportMapFragment获取映射。 mMap=((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)) .getMap(); //检查我们是否成功获得地图。 如果(mMap!=null){ setUpMap(); } } } /** *在这里,我们可以添加标记或线条、添加侦听器或移动摄影机 *在非洲附近加一个标记。 *

*当我们确定{@link#mMap}不是null时,应该只调用一次。 */ 私有void setUpMap(){ mMap.addMarker(新标记选项().position(新板条(0,0)).title(“标记”)