0
我是android新手。運行時,我的應用在logCat上顯示以下錯誤。任何幫助表示讚賞。Android Studio-E/AndroidRuntime:致命例外:主-maps V2
我宣佈「家」類作爲導航抽屜的一部分。當我插入此代碼時,似乎存在問題。
在此先感謝。
Java代碼:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
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 java.util.ArrayList;
import java.util.HashMap;
public class Home extends Fragment
{
private GoogleMap mMap;
private ArrayList<MyMarker> mMyMarkersArray = new ArrayList<MyMarker>();
private HashMap<Marker, MyMarker> mMarkersHashMap;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Initialize the HashMap for Markers and MyMarker object
mMarkersHashMap = new HashMap<Marker, MyMarker>();
mMyMarkersArray.add(new MyMarker("loc1", "icon1", Double.parseDouble("44.637564"), Double.parseDouble("-63.5753207")));
mMyMarkersArray.add(new MyMarker("loc2", "icon2", Double.parseDouble("44.6462628"), Double.parseDouble("-63.5721407")));
mMyMarkersArray.add(new MyMarker("loc3", "icon3", Double.parseDouble("44.6470678"), Double.parseDouble("-63.5747943")));
mMyMarkersArray.add(new MyMarker("loc4", "icon4", Double.parseDouble("43.5643397"), Double.parseDouble("-65.5643972")));
mMyMarkersArray.add(new MyMarker("loc5", "icon5", Double.parseDouble("44.6556674"), Double.parseDouble("-63.6056877")));
mMyMarkersArray.add(new MyMarker("loc6", "icon6", Double.parseDouble("44.6892204"), Double.parseDouble("44.6892204")));
mMyMarkersArray.add(new MyMarker("loc7", "icon7", Double.parseDouble("44.6367517"), Double.parseDouble("-63.5839683")));
mMyMarkersArray.add(new MyMarker("loc8", "icondefault", Double.parseDouble("44.6508492"), Double.parseDouble("-63.592472")));
setUpMap();
plotMarkers(mMyMarkersArray);
}
private void plotMarkers(ArrayList<MyMarker> markers)
{
if(markers.size() > 0)
{
for (MyMarker myMarker : markers)
{
// Create user marker with custom icon and other options
MarkerOptions markerOption = new MarkerOptions().position(new LatLng(myMarker.getmLatitude(), myMarker.getmLongitude()));
markerOption.icon(BitmapDescriptorFactory.fromResource(R.drawable.smallicon1));
Marker currentMarker = mMap.addMarker(markerOption);
mMarkersHashMap.put(currentMarker, myMarker);
mMap.setInfoWindowAdapter(new MarkerInfoWindowAdapter());
}
}
}
private int manageMarkerIcon(String markerIcon)
{
if (markerIcon.equals("icon1"))
return R.drawable.smallicon1;
else if(markerIcon.equals("icon2"))
return R.drawable.smallicon1;
else if(markerIcon.equals("icon3"))
return R.drawable.smallicon1;
else if(markerIcon.equals("icon4"))
return R.drawable.smallicon1;
else if(markerIcon.equals("icon5"))
return R.drawable.smallicon1;
else if(markerIcon.equals("icon6"))
return R.drawable.smallicon1;
else if(markerIcon.equals("icon7"))
return R.drawable.smallicon1;
else
return R.drawable.smallicon1;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_feedback, container, false);
}
private void setUpMap()
{
// 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) getFragmentManager().findFragmentById(R.id.map)).getMap();
// Check if we were successful in obtaining the map.
if (mMap != null)
{
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener()
{
@Override
public boolean onMarkerClick(com.google.android.gms.maps.model.Marker marker)
{
marker.showInfoWindow();
return true;
}
});
}
else
Toast.makeText(getActivity().getApplicationContext(), "Unable to create Maps", Toast.LENGTH_SHORT).show();
}
}
public class MarkerInfoWindowAdapter implements GoogleMap.InfoWindowAdapter
{
public MarkerInfoWindowAdapter()
{
}
@Override
public View getInfoWindow(Marker marker)
{
return null;
}
@Override
public View getInfoContents(Marker marker)
{
View v = getActivity().getLayoutInflater().inflate(R.layout.infowindow_layout, null);
MyMarker myMarker = mMarkersHashMap.get(marker);
ImageView markerIcon = (ImageView) v.findViewById(R.id.marker_icon);
TextView markerLabel = (TextView)v.findViewById(R.id.marker_label);
TextView anotherLabel = (TextView)v.findViewById(R.id.another_label);
markerIcon.setImageResource(manageMarkerIcon(myMarker.getmIcon()));
markerLabel.setText(myMarker.getmLabel());
anotherLabel.setText("A custom text");
return v;
}
}
public void onDestroy()
{
super.onDestroy();
}
@Override
public void onDetach()
{
super.onDetach();
}
}
Tomcat的:
10-28 17:06:11.301 13300-13300/com.example.chid.parktrial1 W/dalvikvm﹕
threadid=1: thread exiting with uncaught exception (group=0x4181cc08)
10-28 17:06:11.306 13300-13300/com.example.chid.parktrial1 E/AndroidRuntime﹕
FATAL EXCEPTION: main Process: com.example.chid.parktrial1, PID: 13300 java.lang.RuntimeException: Unable to start activity ComponentInfo
{com.example.chid.parktrial1/com.example.chid.parktrial1.SuperPark}:
java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2447)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2507)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5692)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.chid.parktrial1.Home.setUpMap(Home.java:149)
at com.example.chid.parktrial1.Home.onCreate(Home.java:87)
at android.support.v4.app.Fragment.performCreate(Fragment.java:1942)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1259)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1624)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:330)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:547)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1177)
at android.app.Activity.performStart(Activity.java:5551)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2420)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2507)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5692)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
10-28 17:06:14.401 13300-13300/com.example.chid.parktrial1 I/Process﹕ Sending signal. PID: 13300 SIG: 9
我提交的編輯你的問題,但它可能不會馬上接受,所以你可以自己做。您的代碼塊的格式不正確。當他們格式正確時,人們會更加認真地回答問題。 –