1

为 Android 构建一个原生扩展,我正在尝试集成谷歌地图视图,当我尝试从布局访问属性时,它运行良好。错误出现在扩展地图视图的活动类中:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Remove title bar
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.activity_map);

    mapView = (MapView)findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true); // <- this crashes, same for textview etc.

加载的 xml 如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"  >

<TextView
    android:id="@+id/textfield1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="0dp"
    android:text="@string/hello_world"
    android:textColor="#ff0000"  />

<com.google.android.maps.MapView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapview"
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:clickable="true" 
    android:apiKey="0cy94dNyuBcfF0aNZhB_JKpF4dQtxgDWhCXppRw" />

<Button
    android:id="@+id/button_clear"
    android:layout_marginTop="20dp"
    android:layout_marginLeft="20dp"
    android:layout_width="100dp"
    android:layout_height="40dp"
    android:background="@android:color/black"
    android:text="@string/clear"
    android:textColor="@android:color/white" />

<Button
    android:id="@+id/button_add"
    android:layout_marginTop="20dp"
    android:layout_marginLeft="20dp"
    android:layout_width="100dp"
    android:layout_height="40dp"
    android:background="@android:color/black"
    android:text="@string/add"
    android:textColor="@android:color/white" />

到目前为止我发现并检查了什么:

  • 扩展正确加载,因为 xml 布局中的地图视图正确显示
  • res-文件夹被复制并编译到 ANE(解压缩检查)以及 APK(解压缩检查)
  • 所有布局元素都存在问题,应用程序在访问 textview 时也会崩溃。所以这不是特定于地图的问题
  • 项目在独立运行时工作正常,所以它必须与扩展有关

我已经阅读了一些关于使用 getRessourceId 的帖子,但这意味着需要将 FREContext 注入到活动中,这对我来说听起来很奇怪。

任何提示都非常感谢!谢谢,M.

4

1 回答 1

1

想通了第一部分。实际上,您的活动中有一个静态 FREContext。在您的扩展函数中,将此上下文设置为一个,您将获得:

    @Override
    public FREObject call(FREContext context, FREObject[] args) {

         MyActivity.freContext = context;

在您的活动中,您通过它的 resourceId 设置布局:

    setContentView( freContext.getResourceId("layout.my_layout_xml"));

希望这可以帮助某人。

于 2012-11-28T07:54:15.970 回答