我目前有一个使用片段从列表中选择布局的工作项目,我现在尝试做的是从另一个项目添加1个类和布局fragfrant并且我收到此错误。
android.support.v4.app.Fragment;当我点亮线条时。
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends ActionBarActivity {
// drawer layout
private DrawerLayout drawerLayout;
// drawer listview
private ListView drawerListView;
// slide menu items
private String[] navMenuTitles;
// used to store app title
private CharSequence mTitle;
// nav drawer title
private CharSequence mDrawerTitle;
// toggle that open and close the drawer
private ActionBarDrawerToggle mDrawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle();
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerListView = (ListView) findViewById(R.id.left_drawer);
// Set the adapter for the list view
drawerListView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, navMenuTitles));
// Set the list's click listener
drawerListView.setOnItemClickListener(new SlideMenuClickListener());
// enabling action bar app icon and behaving it as toggle button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(
this, /* Context */
drawerLayout, /* DrawerLayout object */
R.string.app_name, /* nav drawer open - description for accessibility */
R.string.app_name /* nav drawer close - description for accessibility */
);
// {
//
// /** Called when a drawer has settled in a completely closed state. */
// public void onDrawerClosed(View view) {
// setTitle(mTitle);
//// calling onPrepareOptionsMenu() to show action bar icons
// invalidateOptionsMenu();
// }
//
// /** Called when a drawer has settled in a completely open state. */
// public void onDrawerOpened(View drawerView) {
// setTitle(mDrawerTitle);
//// calling onPrepareOptionsMenu() to hide action bar icons
// invalidateOptionsMenu();
// }
// };
// Set the drawer toggle as the DrawerListener
drawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
// on first time display view for first nav item
selectFragment(0);
}
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getSupportActionBar().setTitle(mTitle);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Pass the event to ActionBarDrawerToggle, if it returns
// true, then it has handled the app icon touch event
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/***
* Called when invalidateOptionsMenu() is triggered
*/
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// if nav drawer is opened, hide the action items
boolean drawerOpen = drawerLayout.isDrawerOpen(drawerListView);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
private class SlideMenuClickListener
implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
selectFragment(position);
}
}
private void selectFragment(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new InboxFragment();
break;
case 2:
fragment = new SentFragment();
break;
case 3:
fragment = new DraftFragment();
break;
case 4:
fragment = new Maaidah();
break;
case 5:
fragment = new anaam();
break;
case 6:
fragment = new araaf();
break;
case 7:
fragment = new anfal();
break;
case 8:
fragment = new taubah();
break;
case 9:
fragment = new yunus();
break;
case 10:
fragment = new Hood();
break;
case 11:
fragment = new yusuf();
break;
case 12:
fragment = new raad();
break;
case 13:
fragment = new ibrahim();
break;
case 14:
fragment = new hijr();
break;
case 15:
fragment = new nahl();
break;
case 16:
fragment = new israil();
break;
case 17:
fragment = new kahfi();
break;
case 18:
fragment = new mariyam();
break;
case 19:
fragment = new taha();
break;
case 20:
fragment = new anbia();
break;
case 21:
fragment = new haj();
break;
case 22:
fragment = new muminun();
break;
case 23:
fragment = new nur();
break;
case 24:
fragment = new furqan();
break;
case 25:
fragment = new shuara();
break;
case 26:
fragment = new naml();
break;
case 27:
fragment = new qasas();
break;
case 28:
fragment = new ankabut();
break;
case 29:
fragment = new roam();
break;
case 30:
fragment = new luqman();
break;
case 31:
fragment = new sajdah();
break;
case 32:
fragment = new ahzaab();
break;
case 33:
fragment = new saba();
break;
case 34:
fragment = new fatir();
break;
case 35:
fragment = new yaasin();
break;
case 36:
fragment = new safaat();
break;
case 37:
fragment = new soaad();
break;
case 38:
fragment = new zumar();
break;
case 39:
fragment = new mumin();
break;
case 40:
fragment = new sajadah();
break;
case 41:
fragment = new shuara();
break;
case 42:
fragment = new zukhraf();
break;
case 43:
fragment = new dukhan();
break;
case 44:
fragment = new jaasiah();
break;
case 45:
fragment = new ahqaaf();
break;
case 46:
fragment = new muhammad();
break;
case 47:
fragment = new fath();
break;
case 48:
fragment = new hujurat();
break;
case 49:
fragment = new qaaf();
break;
case 50:
fragment = new zaariyat();
break;
case 51:
fragment = new toor();
break;
case 52:
fragment = new najm();
break;
case 53:
fragment = new qamar();
break;
case 54:
fragment = new rahmaan();
break;
case 55:
fragment = new waqiah();
break;
case 56:
fragment = new hadeed();
break;
case 57:
fragment = new mujadilah();
break;
case 58:
fragment = new hashar();
break;
case 59:
fragment = new mumtahnah();
break;
case 60:
fragment = new saf();
break;
case 61:
fragment = new jumuah();
break;
case 62:
fragment = new munafiqun();
break;
case 63:
fragment = new taghabun();
break;
case 64:
fragment = new talaq();
break;
case 65:
fragment = new tahrim();
break;
case 66:
fragment = new mulk();
break;
case 67:
fragment = new qalam();
break;
case 68:
fragment = new haaqah();
break;
case 69:
fragment = new meaarij();
break;
case 70:
fragment = new nooh();
break;
case 71:
fragment = new jin();
break;
case 72:
fragment = new muzammil();
break;
case 73:
fragment = new mudassir();
break;
case 74:
fragment = new qiyamah();
break;
case 75:
fragment = new dahar();
break;
case 76:
fragment = new mursalaat();
break;
case 77:
fragment = new naba();
break;
case 78:
fragment = new naziaat();
break;
case 79:
fragment = new abas();
break;
case 80:
fragment = new takwir();
break;
case 81:
fragment = new lnfitar();
break;
case 82:
fragment = new mutaffifin();
break;
case 83:
fragment = new inshiqaq();
break;
case 84:
fragment = new buruj();
break;
case 85:
fragment = new talaq();
break;
case 86:
fragment = new aala();
break;
case 87:
fragment = new ghashiyah();
break;
case 88:
fragment = new fajr();
break;
case 89:
fragment = new balad();
break;
case 90:
fragment = new shams();
break;
case 91:
fragment = new lail();
break;
case 92:
fragment = new duha();
break;
case 93:
fragment = new nashrah();
break;
case 94:
fragment = new teen();
break;
case 95:
fragment = new alaq();
break;
case 96:
fragment = new qadr();
break;
case 97:
fragment = new bayyinah();
break;
case 98:
fragment = new zilzali();
break;
case 99:
fragment = new adiyaat();
break;
case 100:
fragment = new qariah();
break;
case 101:
fragment = new takasur();
break;
case 102:
fragment = new asr();
break;
case 103:
fragment = new humazah();
break;
case 104:
fragment = new feel();
break;
case 105:
fragment = new quraish();
break;
case 106:
fragment = new maoon();
break;
case 107:
fragment = new kausar();
break;
case 108:
fragment = new kafirun();
break;
case 109:
fragment = new nasr();
break;
case 110:
fragment = new tabbat();
break;
case 111:
fragment = new ikhlas();
break;
case 112:
fragment = new falaq();
break;
case 113:
fragment = new naas();
break;
case 114:
fragment = new KaabaLocatorFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
drawerListView.setItemChecked(position, true);
drawerListView.setSelection(position);
setTitle(navMenuTitles[position]);
drawerLayout.closeDrawer(drawerListView);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
}
我在案例114 KaabaLocatorFragment()上收到错误 当我突出这一行时,它说
需要android.support.v4.app.Fragment 找到了it.bellotti.navigationdrawer.compass.KaabaLocatorFragment
案例114也是我刚刚添加到这个项目中的新行,我想给这个项目添加一个指南针布局和类,所以这里是我遇到问题的一行 这里是kaabalocatorFragment
import android.Manifest;
import android.app.Activity;
import android.app.Fragment;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMapOptions;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
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 com.google.android.gms.maps.model.PolylineOptions;
import it.bellotti.navigationdrawer.BuildConfig;
import it.bellotti.navigationdrawer.R;
/**
* A simple {@link Fragment} subclass.
*/
public class KaabaLocatorFragment extends Fragment implements Constants, OnMapReadyCallback, SensorEventListener {
Location mLastLocation;
MapFragment mMapFragment;
GoogleMap mMap;
// private float currentDegree = 0f;
private SensorManager mSensorManager;
private float[] mRotationMatrix = new float[16];
private float[] mValues = new float[3];
private boolean mRegistered = false;
private static boolean sWriterExternalPermissionDenied;
public KaabaLocatorFragment() {
// Required empty public constructor
}
public static KaabaLocatorFragment newInstance(Location location) {
KaabaLocatorFragment fragment = new KaabaLocatorFragment();
Bundle args = new Bundle();
args.putParcelable(EXTRA_LAST_LOCATION, location);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mLastLocation = (Location) getArguments().getParcelable(EXTRA_LAST_LOCATION);
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mSensorManager = (SensorManager) activity.getSystemService(Activity.SENSOR_SERVICE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_kaaba_locator, container, false);
checkPermissions();
return view;
}
private boolean checkPermissions() {
if (!PermissionUtil.hasSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
if (!sWriterExternalPermissionDenied) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_EXTERNAL);
} else {
// Perhaps inform the user why they aren't seeing anything.
}
return false;
}
return true;
}
public void showMap() {
if (!PermissionUtil.hasSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
throw new IllegalStateException("WRITE_EXTERNAL_STORAGE permission should be granted before this method is called.");
}
if (mMapFragment == null) {
mMapFragment = (MapFragment) getFragmentManager().findFragmentByTag("map_fragment");
}
if (mMapFragment == null) {
GoogleMapOptions options = new GoogleMapOptions()
.rotateGesturesEnabled(false)
.tiltGesturesEnabled(false)
.compassEnabled(true)
.zoomControlsEnabled(false)
.zoomGesturesEnabled(true)
.scrollGesturesEnabled(true);
mMapFragment = MapFragment.newInstance(options);
}
if (mMap == null && !mMapFragment.isAdded()) {
((ViewGroup) getView().findViewById(R.id.map_container)).removeAllViews();
getFragmentManager().beginTransaction().add(R.id.map_container, mMapFragment, "map_fragment").commit();
mMapFragment.getMapAsync(this);
} else {
registerRotationListener();
}
}
public void hideMap() {
unregisterRotationListener();
}
@Override
public void onMapReady(GoogleMap map) {
mMap = map;
if (mLastLocation != null) {
initMap();
}
}
private void initMap() {
if (mMap == null && mLastLocation == null) {
Log.w("KabaaLocatorFragment", "Ignoring since mMap or mLastLocation is null");
return;
}
registerRotationListener();
LatLng startPosition = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
//21.4224698,39.8262066
LatLng kaaba = new LatLng(21.4224698, 39.8262066);
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(true);
mMap.getUiSettings().setCompassEnabled(false);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(startPosition, 13));
//mMap.getUiSettings().setRotateGesturesEnabled(false);
mMap.clear();
mMap.addMarker(new MarkerOptions()
.title(getString(R.string.kaaba))
.position(kaaba));
mMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.compass))
.title(getString(R.string.current_location))
.position(startPosition));
// Polylines are useful for marking paths and routes on the map.
mMap.addPolyline(new PolylineOptions().geodesic(true)
.add(startPosition) // user position
.add(kaaba)
.color(Color.RED)); // Kaabah
}
public void setLocation(Location location) {
mLastLocation = location;
initMap();
}
@Override
public void onResume() {
super.onResume();
registerRotationListener();
}
@Override
public void onPause() {
super.onPause();
unregisterRotationListener();
}
private void registerRotationListener() {
if (mMap != null && mLastLocation != null && !mRegistered) {
mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR), SensorManager.SENSOR_DELAY_UI);
mRegistered = true;
}
}
private void unregisterRotationListener() {
if (mRegistered) {
mSensorManager.unregisterListener(this);
mRegistered = false;
}
}
@Override
public void onSensorChanged(SensorEvent event) {
if (mLastLocation == null) {
return;
}
SensorManager.getRotationMatrixFromVector(mRotationMatrix, event.values);
SensorManager.getOrientation(mRotationMatrix, mValues);
/*
if (BuildConfig.DEBUG) {
System.out.println("sensorChanged (" + Math.toDegrees(mValues[0]) + ", " + Math.toDegrees(mValues[1]) + ", " + Math.toDegrees(mValues[2]) + ")");
}
*/
float bearing = 0f;
if (mMap != null) {
bearing = Double.valueOf(mMap.getCameraPosition().bearing).floatValue();
}
// get the angle around the z-axis rotated
float degree = Double.valueOf(Math.toDegrees(mValues[0])).floatValue();
if (Math.round(bearing) == Math.round(degree)) {
System.out.println("bearing and degrees are the same.");
return;
}
if (BuildConfig.DEBUG) {
System.out.println("degrees " + degree + ", bearing " + bearing);
}
//tvHeading.setText("Heading: " + Float.toString(degree) + " degrees");
CameraPosition cameraPosition = mMap.getCameraPosition();
CameraPosition newPosition = new CameraPosition(cameraPosition.target, cameraPosition.zoom, cameraPosition.tilt, degree);
mMap.moveCamera(CameraUpdateFactory.newCameraPosition(newPosition));
// create a rotation animation (reverse turn degree degrees)
/*
RotateAnimation ra = new RotateAnimation(
currentDegree,
-degree-180,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF,
0.5f);
// how long the animation will take place
ra.setDuration(210);
// set the animation after the end of the reservation status
ra.setFillAfter(true);
// Start the animation
mCompass.startAnimation(ra);
currentDegree = -degree-180;
*/
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
/**
* Callback received when a permissions request has been completed.
*/
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
if (requestCode == REQUEST_WRITE_EXTERNAL) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
showMap();
} else {
Log.i("BaseActivity", "LOCATION permission was NOT granted.");
sWriterExternalPermissionDenied = true;
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
答案 0 :(得分:0)
您正在使用MainActivity上的支持库v4,但片段KaabaLocatorFragment正在使用本机片段。
片段KaabaLocatorFragment需要扩展support.v4库
在KaabaLocatorFragment中,尝试更改:
import android.app.Fragment;
到这个
import android.support.v4.app.Fragment;