【问题标题】:Trouble importing class from the appEngine package从 appEngine 包导入类时遇到问题
【发布时间】:2014-10-05 07:14:39
【问题描述】:

这是我第一次尝试安卓应用的服务器-客户端交互。我为我正在尝试制作的一个小应用程序编写了这个 MainActivity.java 文件。我正在尝试实现 Google App Engine 数据库来存储地图上标记的信息。我的 addMarkerTask 类基于来自 google 的这个示例。

https://github.com/GoogleCloudPlatform/solutions-mobile-shopping-assistant-backend-java/blob/master/MobileAssistant-Tutorial/Phase1_Snippets/MainActivity.01.java

这是我的课

package com.example.rpialmanac;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.json.jackson2.JacksonFactory;


import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.os.AsyncTask;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    GoogleMap mMap;
    mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
    final LatLng FIELD = new LatLng(42.730357, -73.679951);
    Marker field = mMap.addMarker(new MarkerOptions().position(FIELD));
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
   * AsyncTask for adding a new marker to the database  */
private class addMarkerTask extends AsyncTask<Void, Void, Void> {
    /**
     * Calls appropriate CloudEndpoint to add a new marker to the database.
     *
     * @param params the place where the user is adding a marker
     */
    @Override
    protected Void doInBackground(Void... params) {
      addMarker newMarker = new addMarker();

      // Set the ID of the store where the user is. 
      // This would be replaced by the actual ID in the final version of the code. 
      newMarker.setMarkerName("86 field");
      newMarker.setMarkerType("recreation")
      newMarker.setMarkerLat(42.730357);
      newMarker.setMarkerLong(-73.679951);

      addMarkerEndpoint.Builder builder = new addMarkerEndpoint.Builder(
          AndroidHttp.newCompatibleTransport(), new JacksonFactory(),
          null);

      builder = CloudEndpointUtils.updateBuilder(builder);

      Checkinendpoint endpoint = builder.build();


      try {
        endpoint.insertCheckIn(checkin).execute();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }

      return null;

}
}

}

但是,eclipse 无法解析对象 addMarker,我知道这是因为我没有从我的 rpiAlmanac-AppEngine 包中的 addMarker.java 文件导入类,但是当我开始输入 com.example.rpiAlmanac.*。有谁知道是否可以从另一个包中导入一个类?如果有其他方法可以做到这一点,我很乐意听到任何帮助。

【问题讨论】:

    标签: java android eclipse google-app-engine


    【解决方案1】:

    只需将其添加到您的导入语句中。

    import com.example.rpiAlmanac.addMarker;
    

    请注意,按照惯例,您应该让类名以大写字母开头。即添加标记

    顺便问一下,您是否启用了内容辅助的自动激活功能? Eclipse -> 首选项 -> Java -> 编辑器 -> 内容辅助

    还要检查你的类是否在你的构建路径中。项目 -> 属性 -> Java Build Pathh -> 源代码

    【讨论】:

    • 谢谢!我让它识别另一个包。我仍然遇到的唯一错误是 eclipse 无法解析 Addmarkerendpoint.Builder (我更改了命名约定)来自 google 的示例对其 Checkin 类使用同一行,所以我不确定我从哪里得到错误跨度>
    • @Alex 仔细检查示例中的包和类名。例如,MainActivity 有“import com.google.samplesolutions.mobileassistant.checkinendpoint.Checkinendpoint;”但我只在实际的 src 中看到 com.google.samplesolutions.mobileassistant.CheckInEndpoint 。也许他们的样本已经过时了。
    • 我也注意到了这一点。我不确定我是否遗漏了什么,或者谷歌是否真的遗漏了存储库中的一个类。我会继续调查的。
    猜你喜欢
    • 2012-12-19
    • 1970-01-01
    • 1970-01-01
    • 2017-09-03
    • 2020-02-19
    • 2014-12-01
    • 2018-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多