gpt4 book ai didi

java - 启动新 Activity ,获取不是封闭类错误

转载 作者:行者123 更新时间:2023-12-01 22:06:43 25 4
gpt4 key购买 nike

首先,我是 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);
}
}

和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"));
}
}

最佳答案

您的 Activity 名为 MainActivity,但您尝试使用 MapActivity.this 作为 new Intent(MapActivity.this, MapActivity.class) 的第一个参数。将其更改为 MainActivity.this

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

关于java - 启动新 Activity ,获取不是封闭类错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32606153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com