无法解析所需的android.app.fragment Android Studio

时间:2017-10-09 15:01:27

标签: java android

我有AppointmentView ClassFragmentMap Class。我想使用FragmentManager和FragmentTransaction将片段插入到我的AppointmentView活动中,但我有一个错误,我不知道如何修复它。

Image that shows the error

谁能告诉我我做错了什么。

PS:下面是AppointmentView and FragmentMap

的源代码

AppointmentView活动:

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;

import org.w3c.dom.Text;

public class AppointmentView extends AppCompatActivity implements 
OnMapReadyCallback {
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_appointment_view);

          FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    FragmentMap fragmentMap = new FragmentMap();
    fragmentTransaction.add(R.id.fragment_container, fragmentMap, "FragmentiDy");
    fragmentTransaction.commit();



    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    TextView text=(TextView)findViewById(R.id.infoFirstname);
    TextView txtlastname=(TextView)findViewById(R.id.infoLastname);
    TextView txtAddress=(TextView)findViewById(R.id.infoAddress);


    Intent i=getIntent();
    String firstname=i.getStringExtra("Name");
    String lastname=i.getStringExtra("Lastname");
    String address=i.getStringExtra("Address");
    text.setText(firstname);
    txtlastname.setText(lastname);
    txtAddress.setText(address);


    btn=(Button)findViewById(R.id.btn);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent=new Intent(AppointmentView.this,Feedback.class);
            startActivity(intent);

        }
    });


    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            //Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
            //      .setAction("Action", null).show();
            Intent i=new Intent(AppointmentView.this,MapActivity.class);
            startActivity(i);
        }
    });


}




@Override
public void onMapReady(GoogleMap googleMap) {

}}

FragmentMap FragmentActivity:

import android.content.Context;
 import android.content.pm.PackageManager;
 import android.location.Address;
 import android.location.Geocoder;
 import android.location.Location;
 import android.os.Build;
 import android.os.Bundle;
 import android.app.Fragment;
 import android.support.annotation.NonNull;
 import android.support.annotation.Nullable;
 import android.support.v4.app.ActivityCompat;
 import android.support.v4.app.FragmentActivity;
 import android.util.AttributeSet;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.LinearLayout;
 import android.widget.Toast;

 import com.google.android.gms.common.ConnectionResult;
 import com.google.android.gms.common.api.GoogleApiClient;
 import com.google.android.gms.location.LocationListener;
 import com.google.android.gms.location.LocationRequest;
 import com.google.android.gms.location.LocationServices;
 import com.google.android.gms.maps.CameraUpdate;
 import com.google.android.gms.maps.CameraUpdateFactory;
 import com.google.android.gms.maps.GoogleMap;
 import com.google.android.gms.maps.MapView;
 import com.google.android.gms.maps.MapsInitializer;
 import com.google.android.gms.maps.OnMapReadyCallback;
 import com.google.android.gms.maps.SupportMapFragment;
 import com.google.android.gms.maps.UiSettings;
 import com.google.android.gms.maps.model.CameraPosition;
 import com.google.android.gms.maps.model.LatLng;
 import com.google.android.gms.maps.model.MarkerOptions;

 import java.io.IOException;
 import java.util.List;


/**
* A simple {@link Fragment} subclass.
*/
public class FragmentMap extends FragmentActivity implements 
OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, 
GoogleApiClient.OnConnectionFailedListener, LocationListener {

GoogleMap mMap;
UiSettings mUiSettings;
GoogleApiClient mGoogleApiClient;
MapView mMapView;
View mView;

public FragmentMap() {
    // Required empty public constructor
}


@Override
public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
    mView = super.onCreateView(parent, name, context, attrs);
    return mView;
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;



  /*  // Add a marker in Sydney, Australia, and move the camera.
    LatLng sydney = new LatLng(-34, 151);
    mMap.getUiSettings().setZoomControlsEnabled(true);
    mMap.getUiSettings().setCompassEnabled(true);
    mMap.getUiSettings().setMyLocationButtonEnabled(true);
    mMap.getUiSettings().setIndoorLevelPickerEnabled(true);
    mMap.addMarker(new MarkerOptions().position(getLocationFromAddress(getApplicationContext(),"Gnjilane")).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(getLocationFromAddress(getApplicationContext(),"Gnjilane")));
    mMap.animateCamera(CameraUpdateFactory.zoomTo(12.0f)); */

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

    mGoogleApiClient.connect();

}

public LatLng getLocationFromAddress(Context context, String strAddress) {

    Geocoder coder = new Geocoder(context);
    List<Address> address;
    LatLng p1 = null;

    try {
        // May throw an IOException
        address = coder.getFromLocationName(strAddress, 5);
        if (address == null) {
            return null;
        }
        Address location = address.get(0);
        location.getLatitude();
        location.getLongitude();

        p1 = new LatLng(location.getLatitude(), location.getLongitude());

    } catch (IOException ex) {

        ex.printStackTrace();
    }

    return p1;
}

LocationRequest mLocationRequest;

@Override
public void onConnected(@Nullable Bundle bundle) {
    mLocationRequest = LocationRequest.create();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(1000);

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

@Override
public void onLocationChanged(Location location) {
    if(location==null) {
        Toast.makeText(this, "Cant get current location", Toast.LENGTH_LONG).show();
    }
    else {
        LatLng ll = new LatLng(location.getLatitude(), location.getLongitude());
        CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll,15);
        mMap.animateCamera(update);

    }
}}

1 个答案:

答案 0 :(得分:1)

根据the documentation

FragmentTransaction add (
                int containerViewId, 
                Fragment fragment, 
                String tag) {

} 

需要将Fragment添加为第二个参数。您正尝试将FragmentMap类添加到其中,从而扩展了FragmentActivity。

FragmentFragmentActivity是两回事。

也许,您的FragmentMap课程应该扩展适当的Fragment