【问题标题】:launching mapview from main activity (button)从主要活动(按钮)启动地图视图
【发布时间】:2010-03-04 16:29:37
【问题描述】:

我想在这里绕圈子。

我有一个名为 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" />

每次我尝试加载它时,它都会强制关闭。

如果我将此作为我的 onClick 处理程序,那么它将启动一个工作 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));
         }

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

app FCs 为 Unexpected DEX 错误时的 logcat 错误。

任何人都可以在这里指出正确的方向吗?

【问题讨论】:

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


    【解决方案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

    【讨论】:

    • 即使这样做了,它仍然会强制关闭。我将在下面添加更多信息。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多