gpt4 book ai didi

java - getMap() 方法可能返回 null?

转载 作者:行者123 更新时间:2023-11-30 03:12:15 27 4
gpt4 key购买 nike

我使用处理程序从支持 map fragment 中获取 GoogleMap。我真的迷路了,几天来一直在努力修复它。 map 加载正常,但我怀疑它返回的是空值。我知道可能有一些不好的做法,但这不是我的问题。我尝试使用 AsyncTask 和 Handler。我在各种移动设备上测试过,没有使用模拟器。这是我的代码,我删除了一些其他导入,但它们在那里(显然不是整个程序):

import com.codexmalta.mytravelbuddy.R;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;

import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.SupportMapFragment;

import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class Itinerary extends FragmentActivity implements View.OnClickListener {

//Google Map
GoogleMap map;

byte choice;

protected ProgressDialog dialog;

CameraPosition cameraPosition;

Timer timer = new Timer();

LinearLayout[] l = new LinearLayout[10];
TextView[] time = new TextView[10];
TextView[] desc = new TextView[10];
TextView[] loc = new TextView[10];
double[] lat = new double[10];
double[] lng = new double[10];

MarkerOptions[] marker = new MarkerOptions[10];
//Get Itinerary
GetItinerary gi = new GetItinerary();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting_point);
choice = getIntent().getByteExtra("choice", choice);


//Methods
getMapFragment();

getLayouts();
getFields();
gi.main(choice);
fillFields();
removeExtraLayouts(gi.giveNumDay1(),gi.giveNumDay2());
getMap();
displayMarkers();
animateCamera();



}


SupportMapFragment fm = new SupportMapFragment();



@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.starting_point, menu);
return true;
}

private void removeExtraMarkers(){
for(int i = 0; i < 10; i++){
if(lat[i] == 0 && lng[i] == 0){
marker[i].alpha(0);
}
}
}

private void displayMarkers(){
String[] s = new String[10];
for(int i = 0; i < 10; i++){
s[i] = (String) loc[i].getText();
map.addMarker(marker[i].position(new LatLng(lat[i], lng[1])).title(s[i])).setVisible(true);
}
}

public void getMapFragment(){
fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
}


Handler mHandler = new Handler();
public void getMap(){

mHandler.post(new Runnable() {
@Override
public void run() {
GoogleMap map = fm.getMap();
if (map != null) {
map.setMyLocationEnabled(true);
// INIT HERE
map.getUiSettings().setMyLocationButtonEnabled(false);
// ...

} else mHandler.post(this);
}
});

while(map == null){
map = fm.getMap();
if(map!= null)break;
}
}

private void removeExtraLayouts(int a, int b){

//Day One

switch(a){
case 5:
l[0].removeAllViews();
l[1].removeAllViews();
l[2].removeAllViews();
l[3].removeAllViews();
l[4].removeAllViews();
break;
case 4:
l[1].removeAllViews();
l[2].removeAllViews();
l[3].removeAllViews();
l[4].removeAllViews();
break;
case 3:
l[2].removeAllViews();
l[3].removeAllViews();
l[4].removeAllViews();
break;
case 2:
l[3].removeAllViews();
l[4].removeAllViews();
break;
case 1:
l[4].removeAllViews();
break;

}

//Day Two

switch(b){
case 5:
l[5].removeAllViews();
l[6].removeAllViews();
l[7].removeAllViews();
l[8].removeAllViews();
l[9].removeAllViews();
break;
case 4:
l[6].removeAllViews();
l[7].removeAllViews();
l[8].removeAllViews();
l[9].removeAllViews();
break;
case 3:
l[7].removeAllViews();
l[8].removeAllViews();
l[9].removeAllViews();
break;
case 2:
l[8].removeAllViews();
l[9].removeAllViews();
break;
case 1:
l[9].removeAllViews();
break;
}

}

private void getLayouts(){
l[0] = (LinearLayout)findViewById(R.id.a1);
l[0].setOnClickListener(this);
l[1] = (LinearLayout)findViewById(R.id.a2);
l[1].setOnClickListener(this);
l[2] = (LinearLayout)findViewById(R.id.a3);
l[2].setOnClickListener(this);
l[3] = (LinearLayout)findViewById(R.id.a4);
l[3].setOnClickListener(this);
l[4] = (LinearLayout)findViewById(R.id.a5);
l[4].setOnClickListener(this);
l[5] = (LinearLayout)findViewById(R.id.a6);
l[5].setOnClickListener(this);
l[6] = (LinearLayout)findViewById(R.id.a7);
l[6].setOnClickListener(this);
l[7] = (LinearLayout)findViewById(R.id.a8);
l[7].setOnClickListener(this);
l[8] = (LinearLayout)findViewById(R.id.a9);
l[8].setOnClickListener(this);
l[9] = (LinearLayout)findViewById(R.id.a10);
l[9].setOnClickListener(this);
}

private void getFields(){
time[0] = (TextView)findViewById(R.id.time1);
desc[0] = (TextView)findViewById(R.id.desc1);
loc[0] = (TextView)findViewById(R.id.loc1);

time[1] = (TextView)findViewById(R.id.time2);
desc[1] = (TextView)findViewById(R.id.desc2);
loc[1] = (TextView)findViewById(R.id.loc2);

time[2] = (TextView)findViewById(R.id.time3);
desc[2] = (TextView)findViewById(R.id.desc3);
loc[2] = (TextView)findViewById(R.id.loc3);

time[3] = (TextView)findViewById(R.id.time4);
desc[3] = (TextView)findViewById(R.id.desc4);
loc[3] = (TextView)findViewById(R.id.loc4);

time[4] = (TextView)findViewById(R.id.time5);
desc[4] = (TextView)findViewById(R.id.desc5);
loc[4] = (TextView)findViewById(R.id.loc5);

time[5] = (TextView)findViewById(R.id.time6);
desc[5] = (TextView)findViewById(R.id.desc6);
loc[5] = (TextView)findViewById(R.id.loc6);

time[6] = (TextView)findViewById(R.id.time7);
desc[6] = (TextView)findViewById(R.id.desc7);
loc[6] = (TextView)findViewById(R.id.loc7);

time[7] = (TextView)findViewById(R.id.time8);
desc[7] = (TextView)findViewById(R.id.desc8);
loc[7] = (TextView)findViewById(R.id.loc8);

time[8] = (TextView)findViewById(R.id.time9);
desc[8] = (TextView)findViewById(R.id.desc9);
loc[8] = (TextView)findViewById(R.id.loc9);

time[9] = (TextView)findViewById(R.id.time10);
desc[9] = (TextView)findViewById(R.id.desc10);
loc[9] = (TextView)findViewById(R.id.loc10);
}

private void fillFields(){
for(int i = 0; i < 10; i++){
time[i].setText(gi.giveTimeArray(i));
desc[i].setText(gi.giveDescriptionArray(i));
loc[i].setText(gi.giveLocationArray(i));
lat[i] = gi.giveLatitudeArray(i);
lng[i] = gi.giveLongitudeArray(i);
}
}


private void animateCamera(){
CameraPosition.builder().target(new LatLng(20, 20)).zoom(12).build();
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}


@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.a1:

//animateCamera(lat[0],lng[0]);
break;
case R.id.a2:
//animateCamera(lat[1],lng[1]);
break;
case R.id.a3:
//animateCamera(lat[2],lng[2]);
break;
case R.id.a4:
//animateCamera(lat[3],lng[3]);
break;
case R.id.a5:
//animateCamera(lat[4],lng[4]);
break;
case R.id.a6:
//animateCamera(lat[5],lng[5]);
break;
case R.id.a7:
//animateCamera(lat[6],lng[6]);
break;
case R.id.a8:
//animateCamera(lat[7],lng[7]);
break;
case R.id.a9:
//animateCamera(lat[8],lng[8]);
break;
case R.id.a10:
//animateCamera(lat[9],lng[9]);
break;
}
}
}

这是我的 list :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.codexmalta.mytravelbuddy"
android:versionCode="1"
android:versionName="0.5" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />

<application
android:allowBackup="true"
android:icon="@drawable/tb02"
android:label="Travel Buddy"
android:configChanges = "keyboardHidden|orientation"
>

<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

<activity
android:name="com.codexmalta.mytravelbuddy.MainActivity"
android:label="Travel Buddy"
android:theme="@android:style/Theme.NoTitleBar"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

</activity>


<activity
android:name="com.codexmalta.mytravelbuddy.Itinerary"
android:label="Itinerary"
android:theme="@android:style/Theme.NoTitleBar"
android:screenOrientation="portrait">
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="key"/>
</application>

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- The following two permissions are not required to use
Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<uses-permission android:name="android.permission.INTERNET" />

<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>

</manifest>

这是我在布局 xml 中保存 map 的 fragment :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="0dp"
android:layout_marginEnd="0dp"
android:layout_marginTop="-20dp"
android:background="@drawable/background"
android:measureAllChildren="false"
android:minHeight="75dp"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Itinerary" >

<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />

任何帮助将不胜感激,因为我刚刚碰壁。我搜索了其他问题,但没有一个有效/或者我不明白它们是如何工作的。

最佳答案

我完全不明白你为什么要在处理程序中这样做。这个:

GoogleMap gmap = ((SupportMapFragment).getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();

放入 onCreate() 方法将返回 GoogleMap 对象。

关于java - getMap() 方法可能返回 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20742078/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com