我在片段1选项卡中使用android谷歌地图,从我的sqlite数据库中提取latlog。
我使用微调器在片段中选择选项和按钮。
我有3个片段。
片段1是“NormalMapFragment” 片段2是“HeatMapFragment” 片段3是“ClusterMapFragment”
我收到错误:
@Override
public void onResume() {
super.onResume();
mMapView.onResume();
}
错误是:
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.MapView.onResume()' on a null object reference
at com.example.bharat.plantnow.Maps.NormalMapFragment.onResume(NormalMapFragment.java:169)
at android.support.v4.app.Fragment.performResume(Fragment.java:2005)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1207)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1572)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:545)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1474)
at android.view.View.measure(View.java:17554)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:607)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:674)
at android.view.View.measure(View.java:17554)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
at android.view.View.measure(View.java:17554)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17554)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.view.View.measure(View.java:17554)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17554)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2871)
at android.view.View.measure(View.java:17554)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2015)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1173)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1379)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5891)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5289)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
我的法线贴图.xml文件在下面找到:
tools:context="com.example.bharat.plantnow.Maps.fragments.NormalMapFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/textheading2"
android:id="@+id/textView6"
android:textColor="#ffffffff"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/textView6"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<Spinner
android:layout_width="match_parent"
android:layout_height="46dp"
android:id="@+id/et_search"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:padding="8dp"
android:hint="@string/hint_treecond"
android:inputType="textAutoComplete"
android:background="#ffd8dcde"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Map"
android:id="@+id/MapButton"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:background="#ffb73a3b"
android:textColor="#ffffffff"
android:onClick="onSearch" />
</LinearLayout>
<com.google.android.gms.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="150dp"
/>
我的NormalMapFragment .java:我使用过Mapview,Button,Spinner,sqlite db。 让我知道我哪里出错,并根据......纠正。
public class NormalMapFragment extends Fragment implements AdapterView.OnItemSelectedListener{
MapView mMapView;
private Button btMap;
private TreeData db;
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
private Spinner inputSearch;
String[] conditions = {"All Locations",
"Balanced",
"Healthy",
"Imbalance",
"Dangerous",
"Transplanted",
"Dieseased"
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
final Bundle savedInstanceState) {
// TODO Auto-generated method stub
final View v = inflater.inflate(R.layout.normalmap, container,false);
inputSearch = (Spinner)v. findViewById(R.id.et_search);
ArrayAdapter adapter1 = new ArrayAdapter(getActivity().getBaseContext(), R.layout.spinner_text_layout, conditions);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
inputSearch.setAdapter(adapter1);
inputSearch.setOnItemSelectedListener(this);
btMap = (Button)v. findViewById(R.id.MapButton);
btMap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String search = String.valueOf(inputSearch.getSelectedItem()).toString();
mMapView = (MapView) v.findViewById(R.id.map);
mMapView.onCreate(savedInstanceState);
// mMapView.onResume();
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
mMap = mMapView.getMap();
mMap.clear();
// SQLite database handler
db = new TreeData(getActivity().getApplicationContext());
Cursor c = db.getSelectedLocations(search);
if(c.getCount()>0){
c.moveToFirst();
do {
double latitude1 = c.getDouble(c
.getColumnIndex(SQLiteHandler.KEY_TLATITUDE));
double longitude1 = c.getDouble(c
.getColumnIndex(SQLiteHandler.KEY_TLONGITUDE));
String marker = c.getString(c
.getColumnIndex(SQLiteHandler.KEY_TreeID));
LatLng location = new LatLng(latitude1, longitude1);
mMap.addMarker(new MarkerOptions().position(location).title(marker));
// Moving CameraPosition to last clicked position
mMap.moveCamera(CameraUpdateFactory.newLatLng(location));
// Setting the zoom level in the map on last position is clicked
mMap.animateCamera(CameraUpdateFactory.newLatLng(location));
mMap.animateCamera(CameraUpdateFactory.zoomTo(4));
} while (c.moveToNext());
Toast.makeText(getActivity().getApplicationContext(), "Locations of " + search + " trees", Toast.LENGTH_LONG).show();
}
else if(search.equals("All Locations")) {
double latitude = 0;
double longitude = 0;
String mark;
// SQLite database handler
db = new TreeData(getActivity().getApplicationContext());
Cursor locationCursor = db.getLocations();
locationCursor.moveToFirst();
do {
latitude = locationCursor.getDouble(locationCursor
.getColumnIndex(SQLiteHandler.KEY_TLATITUDE));
longitude = locationCursor.getDouble(locationCursor
.getColumnIndex(SQLiteHandler.KEY_TLONGITUDE));
mark = locationCursor.getString(locationCursor
.getColumnIndex(SQLiteHandler.KEY_TreeID));
LatLng location = new LatLng(latitude, longitude);
mMap.addMarker(new MarkerOptions().position(location).title(mark));
} while (locationCursor.moveToNext());
Toast.makeText(getActivity().getApplicationContext(), "All trees locations ", Toast.LENGTH_LONG).show();
db.close();
}
else{
Toast.makeText(getActivity().getApplicationContext(), "Location are not available ", Toast.LENGTH_LONG).show();
}
}
});
return v;
}
@Override
public void onResume() {
super.onResume();
mMapView.onResume();
}
@Override
public void onPause() {
super.onPause();
mMapView.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mMapView.onLowMemory();
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}