我正在使用Googe Map API V2和android制作一个Android应用程序。这是我放置标记的代码:
private void addBusStopMarker(){
final Intent intent1;
map.addMarker(new MarkerOptions().position(new LatLng(1.28213,103.81721)).title("10009 - Bt Merah Ctrl").icon(BitmapDescriptorFactory.fromResource(R.drawable.busstopicon)).snippet("Average Commuters: 9,940"));
map.addMarker(new MarkerOptions().position(new LatLng(1.28294,103.82166)).title("10089 - Jln Bt Merah - B08").icon(BitmapDescriptorFactory.fromResource(R.drawable.busstopicon)).snippet("Average Commuters: 2,050"));
map.setOnMarkerClickListener(new OnMarkerClickListener()
{
@Override
public boolean onMarkerClick(Marker arg0) {
if(arg0.getTitle().equals("10009 - Bt Merah Ctrl"))
intent1 = new Intent(context, PopulationCharts.class);
startActivity(intent1);
return true;
}
});
}
基本上我要做的是当选择标记时,它将显示来自上面代码的信息窗口。然后,当我选择片段时,它将执行onMarkerClick事件。
但是,从上面的代码中,它显示了一条错误消息:The final local variable intent1 cannot be assigned, since it is defined in an enclosing type
。此外,我不知道如何设置标记片段onClick事件
。
提前谢谢。
已编辑的部分
map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
Intent intent = new Intent(context ,PopulationCharts.class);
startActivity(intent);
}
});
错误消息
07-29 12:24:12.774: E/AndroidRuntime(15704): FATAL EXCEPTION: main
07-29 12:24:12.774: E/AndroidRuntime(15704): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.googlemap/com.example.googlemap.PopulationCharts}: java.lang.NullPointerException
07-29 12:24:12.774: E/AndroidRuntime(15704): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1894)
07-29 12:24:12.774: E/AndroidRuntime(15704): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
07-29 12:24:12.774: E/AndroidRuntime(15704): at android.app.ActivityThread.access$600(ActivityThread.java:127)
07-29 12:24:12.774: E/AndroidRuntime(15704): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
07-29 12:24:12.774: E/AndroidRuntime(15704): at android.os.Handler.dispatchMessage(Handler.java:99)
07-29 12:24:12.774: E/AndroidRuntime(15704): at android.os.Looper.loop(Looper.java:137)
07-29 12:24:12.774: E/AndroidRuntime(15704): at android.app.ActivityThread.main(ActivityThread.java:4512)
07-29 12:24:12.774: E/AndroidRuntime(15704): at java.lang.reflect.Method.invokeNative(Native Method)
07-29 12:24:12.774: E/AndroidRuntime(15704): at java.lang.reflect.Method.invoke(Method.java:511)
07-29 12:24:12.774: E/AndroidRuntime(15704): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
07-29 12:24:12.774: E/AndroidRuntime(15704): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
07-29 12:24:12.774: E/AndroidRuntime(15704): at dalvik.system.NativeStart.main(Native Method)
07-29 12:24:12.774: E/AndroidRuntime(15704): Caused by: java.lang.NullPointerException
07-29 12:24:12.774: E/AndroidRuntime(15704): at com.example.googlemap.PopulationCharts.<init>(PopulationCharts.java:28)
07-29 12:24:12.774: E/AndroidRuntime(15704): at java.lang.Class.newInstanceImpl(Native Method)
07-29 12:24:12.774: E/AndroidRuntime(15704): at java.lang.Class.newInstance(Class.java:1319)
07-29 12:24:12.774: E/AndroidRuntime(15704): at android.app.Instrumentation.newActivity(Instrumentation.java:1026)
07-29 12:24:12.774: E/AndroidRuntime(15704): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1885)
07-29 12:24:12.774: E/AndroidRuntime(15704): ... 11 more
07-29 12:24:20.563: I/Process(15704): Sending signal. PID: 15704 SIG: 9
人口类
package com.example.googlemap;
public class PopulationCharts extends Activity{
Intent intent = getIntent();
String markerTitle= intent.getExtras().getString("markertitle");
//Pie chart
private GraphicalView chartPop;
private String[] ageGroupPop;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.population_charts);
openChartAgeGroupPop();
openChartPeakPop();
}
private void openChartAgeGroupPop(){
//codes to generate pie chart
}
//Line chart
private View lineChart;
private String[] time = new String[] {
"0700", "0800" , "0900", "1000", "1100", "1200", "1300", "1400", "1500", "1600", "1700", "1800"};
public void openChartPeakPop(){
//code to generate multiple line chart
}
}
答案 0 :(得分:10)
您可以使用OnInfoWindowClickListener
,因此当您点击标记时,它会首先显示Infowindow
,当点击该窗口时,它将开始活动
<强>样品:强>
map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
Intent intent1 = new Intent(context, PopulationCharts.class);
String title = marker.getTitle();
intent1.putExtra("markertitle", title);
startActivity(intent1);
}
});
获取PopulationCharts活动类中的值
Intent intent = getIntent();
String markerTitle= intent.getExtras().getString("markertitle");
答案 1 :(得分:0)
使用事件 setOnInfoWindowClickListener 的语法是:
map.setOnInfoWindowClickListener(new OnInfoWindowClickListener(){
@Override
public void onInfoWindowClick(Marker marker){
if(marker.getTitle().equals("10009 - Bt Merah Ctrl")){
Intent info = new Intent(getApplicationContext(), YourActivity.class);
startActivity(info);
}
}
});