我已经创建了一个类导致按钮导致另一个类中的映射但是当我单击此按钮时它崩溃并获得错误(NullPointerException),我想在单击按钮时显示此地图,这是我的代码
package com.example.gmapsapp;
import java.util.Stack;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
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.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;
public class Airports extends FragmentActivity {
GoogleMap googleMap;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.airports);
createMapView();
TSPNearestNeighbour TSP = new TSPNearestNeighbour();
int[] shortest = new int[21];
String[] placeNames = {"Cairo International Airport","Alexandria International Airport","Borg El Arab Airport",
"Marsa Matrouh Airport","Sharm el-Sheikh International Airport","Taba International Airport","El Kharga Airport",
"Assiut Airport","Luxor International Airport","Aswan International Airport","El Arish International Airport",
"St. Catherine International Airport","Sharq Al-Owainat Airport","Abu Simbel Airport","Sohag International Airport",
"Port Said Airport","El Tor Airport","Dakhla Oasis Airport","Marsa Alam International Airport","Cairo West Air Base","Almaza Air Force Base"};
String[] placeNamesSnippet = {"Cairo International Airport1","Alexandria International Airport2","Borg El Arab Airport3", "Marsa Matrouh Airport4","Sharm el-Sheikh International Airport5","Taba International Airport6","El Kharga Airport7", "Assiut Airport8","Luxor International Airport9","Aswan International Airport10","El Arish International Airport11", "St. Catherine International Airport12","Sharq Al-Owainat Airport13","Abu Simbel Airport14","Sohag International Airport15", "Port Said Airport16","El Tor Airport17","Dakhla Oasis Airport18","Marsa Alam International Airpor19t","Cairo West Air Bas20e","Almaza Air Force Base21"};
Double[] placeLatitude = {30.111370, 31.192553, 30.917138,31.324435,27.978620,29.590988,27.188222,27.047695, 25.670264,
23.960397,31.076449,28.684537,22.580600,22.375813,26.331926,31.281150,28.208842,25.688581,25.558141,
30.116704,30.095975};
Double[] placeLongitude = {31.413910, 29.953141,29.693375, 27.222200,34.393354,34.778946 , 33.800840, 31.013473 , 32.704063,
32.821163,33.832256,34.062882, 28.720754, 31.611667,31.728437,32.242223,33.645257,28.972356,34.582821,
30.916667,31.362748};
Double[][] Matrix = new Double[21][21];
googleMap.setMyLocationEnabled(true);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(placeLatitude[0], placeLongitude[0]), 13));
for(int i = 0 ; i<21;i++)
{
googleMap.addMarker(new MarkerOptions()
.snippet(placeNamesSnippet[i])
.position(new LatLng(placeLatitude[i], placeLongitude[i]))
.title(placeNames[i]));
}
for(int i=0;i<21;i++)
{
for(int j=0;j<21;j++)
{
Matrix[i][j] = distance(placeLatitude[i],placeLongitude[i],placeLatitude[j],placeLongitude[j],'K');
}
}
shortest = TSP.tsp(Matrix);
for(int i=0; i < 20 ; i++)
{
googleMap.addPolyline(new PolylineOptions().geodesic(true)
.add(new LatLng(placeLatitude[shortest[i]],placeLongitude[shortest[i]]))
.add(new LatLng(placeLatitude[shortest[i+1]],placeLongitude[shortest[i+1]]))
);
}
}
private void createMapView(){
/**
*/
try {
if(null == googleMap){
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.mapView)).getMap();
/**
* If the map is still null after attempted initialisation,
* show an error to the user
*/
if(null == googleMap) {
Toast.makeText(getApplicationContext(),
"Error creating map", Toast.LENGTH_SHORT).show();
}
}
} catch (NullPointerException exception){
Log.e("mapApp", exception.toString());
}
}
private double rad2deg(double rad) {
return (rad * 180 / Math.PI);
}
private double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
private double distance(double lat1, double lon1, double lat2, double lon2, char unit)
{
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
if (unit == 'K') {
dist = dist * 1.609344;
} else if (unit == 'N') {
dist = dist * 0.8684;
}
return (dist);
}
public class TSPNearestNeighbour
{
private int numberOfNodes;
private Stack<Integer> stack;
public TSPNearestNeighbour()
{
stack = new Stack<Integer>();
}
public int[] tsp(Double adjacencyMatrix[][])
{
numberOfNodes = adjacencyMatrix[0].length ;
int[] result = new int[adjacencyMatrix[0].length];
int resultCounter = 1;
int[] visited = new int[numberOfNodes];
visited[0] = 1;
stack.push(0);
int element, dst = 0, i;
Double min = Double.MAX_VALUE;
boolean minFlag = false;
result[0] = 0;
//System.out.print(1 + "\t");
while (!stack.isEmpty())
{
element = stack.peek();
i = 0;
min = Double.MAX_VALUE;
while (i < numberOfNodes)
{
if (adjacencyMatrix[element][i] > 1 && visited[i] == 0)
{
if (min > adjacencyMatrix[element][i])
{
min = adjacencyMatrix[element][i];
dst = i;
minFlag = true;
}
}
i++;
}
if (minFlag)
{
visited[dst] = 1;
stack.push(dst);
result[resultCounter] = dst;
resultCounter++;
//System.out.print(dst + "\t");
minFlag = false;
continue;
}
stack.pop();
}
return result;
}
}
}
具有按钮
的类package com.example.gmapsapp;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class homepage extends MainActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout1);
}
public void onClick(View view)
{
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
public void onClick1(View view)
{
Intent intent = new Intent(this, Airports.class);
startActivity(intent);
}
}
并且logcat是
03-28 16:19:48.819: E/mapApp(4133): java.lang.NullPointerException
03-28 16:19:48.829: E/AndroidRuntime(4133): FATAL EXCEPTION: main
03-28 16:19:48.829: E/AndroidRuntime(4133): Process: com.example.gmapsapp, PID: 4133
03-28 16:19:48.829: E/AndroidRuntime(4133): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gmapsapp/com.example.gmapsapp.Airports}: java.lang.NullPointerException
03-28 16:19:48.829: E/AndroidRuntime(4133): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-28 16:19:48.829: E/AndroidRuntime(4133): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-28 16:19:48.829: E/AndroidRuntime(4133): at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-28 16:19:48.829: E/AndroidRuntime(4133): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-28 16:19:48.829: E/AndroidRuntime(4133): at android.os.Handler.dispatchMessage(Handler.java:102)
03-28 16:19:48.829: E/AndroidRuntime(4133): at android.os.Looper.loop(Looper.java:136)
03-28 16:19:48.829: E/AndroidRuntime(4133): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-28 16:19:48.829: E/AndroidRuntime(4133): at java.lang.reflect.Method.invokeNative(Native Method)
03-28 16:19:48.829: E/AndroidRuntime(4133): at java.lang.reflect.Method.invoke(Method.java:515)
03-28 16:19:48.829: E/AndroidRuntime(4133): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-28 16:19:48.829: E/AndroidRuntime(4133): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-28 16:19:48.829: E/AndroidRuntime(4133): at dalvik.system.NativeStart.main(Native Method)
03-28 16:19:48.829: E/AndroidRuntime(4133): Caused by: java.lang.NullPointerException
03-28 16:19:48.829: E/AndroidRuntime(4133): at com.example.gmapsapp.Airports.onCreate(Airports.java:42)
03-28 16:19:48.829: E/AndroidRuntime(4133): at android.app.Activity.performCreate(Activity.java:5231)
03-28 16:19:48.829: E/AndroidRuntime(4133): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-28 16:19:48.829: E/AndroidRuntime(4133): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-28 16:19:48.829: E/AndroidRuntime(4133): ... 11 more
抱歉英语不好
答案 0 :(得分:1)
您没有正确初始化googleMap对象。尝试在第41行之前初始化它。如果你看到你在createMapView中初始化其他GoogleMap对象,而不是使用稍后在oncreate Method中使用的那个。那么你的googleMap对象将始终为null。
你应该用这个:
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.mapView)).getMap();
答案 1 :(得分:0)
尝试更改此内容:
public void onClick(View view)
{
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
public void onClick1(View view)
{
Intent intent = new Intent(this, Airports.class);
startActivity(intent);
}
到此:
public void onClick(View view)
{
Intent intent = new Intent(getBaseContext(), MainActivity.class
startActivity(intent);
}
public void onClick1(View view)
{
Intent intent = new Intent(getBaseContext(), Airports.class);
startActivity(intent);
}