从主要活动(按钮)启动mapview

时间:2010-03-04 16:29:38

标签: android android-activity android-intent android-mapview

我认为在这里绕圈子。

我有一项名为Locate的活动;

public class Locate extends Activity {

public static String lat;
public static String lon;
public static String number;

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.locate);

    final Button buttonMaps = (Button) findViewById(R.id.ButtonMaps);
    buttonMaps.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
         Toast.makeText(getBaseContext(), "Button Pressed", Toast.LENGTH_SHORT).show();


        try {
         Intent i = new Intent(getBaseContext(), displayMap.class);
         i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         startActivity(i);
         }
         catch (ActivityNotFoundException e) {
          Toast.makeText(getBaseContext(), "Activity Not Found", Toast.LENGTH_SHORT).show(); 
         }



    }});

// Toast.makeText(getBaseContext(), "lat: " + lat + " long: " + lon + " from: " + testname, Toast.LENGTH_LONG).show();
}

如果我将displayMap类设置为普通的Activity,并且只显示一条确认已加载的toast消息,那么它可以正常工作。

如果我这样做的话;

public class displayMap extends MapActivity 
{    
/** Called when the activity is first created. */
public void onCreate()
{

    setContentView(R.layout.displaymap);
    Toast.makeText(getBaseContext(), "Display Map", Toast.LENGTH_SHORT).show();
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}

然后,一旦我点击按钮,我就会关闭一个力量。

我的清单中有正确的'uses-library'标签;

<application android:icon="@drawable/icon" android:label="@string/app_name"> 
<uses-library android:name="com.google.android.maps" />

每次我尝试加载它时,我都不会得到强制关闭的东西。

如果我将其设为myClick处理程序,那么它将启动一个正在运行的googlemaps / default mapview

        public void onClick(View v) {
         Toast.makeText(getBaseContext(), "Button Pressed", Toast.LENGTH_SHORT).show();

         Uri uri=Uri.parse("geo:"+Locate.lat+","+Locate.lon);
         StartActivity(new Intent(Intent.ACTION_VIEW, uri));
         }

但这不是我想要做的,我想要自己 - 所以我可以添加叠加等。但它确实证明了权限设置正确并且lib就在那里。

应用FC出现意外DEX错误时的logcat错误。

有人能指出正确的方向吗?

2 个答案:

答案 0 :(得分:1)

在你的displayMap中的onCreate中,你忘了调用超类

public class displayMap extends MapActivity 
{    
/** Called when the activity is first created. */
public void onCreate()
{
    super.onCreate(icicle); /************* Line to add ********/
    setContentView(R.layout.displaymap);
    Toast.makeText(getBaseContext(), "Display Map", Toast.LENGTH_SHORT).show();
}

不建议使用getBaseContext() - 使用getContext()this

stackoverflow post

中的更多内容

答案 1 :(得分:0)

好的,我发现了问题。出于某种原因,我有google apis,然后在eclipse中的buildpath / lib上列出了maps.jar。清理干净,它现在按预期工作。

相关问题