DalvikVm找不到类android库项目
这里需要挖掘集体的脑力。我正在尝试一个非常简单的BindService和Messenger通信示例。因为我在message.obj字段中传递message对象,所以我在自己的Android库项目中定义了要在服务和客户端之间共享的消息。通过将消息库作为depdency包含,我可以很好地编译,但在运行时dalvikvm抱怨它找不到类。我读过关于出口和订单的其他相关问题,但同样的解决方案对我的情况没有帮助。这一定是个很简单的错误,但我现在正在努力 应用程序依赖于SupportServiceMessageLibrary 在项目的“订单”选项卡中,应用程序的src前面有一个库 由于没有人在使用应用程序,因此未在应用程序中检查Lib是否导出 库正在导出其src和gen文件夹 来自日志cat的错误消息:DalvikVm找不到类android库项目,android,Android,这里需要挖掘集体的脑力。我正在尝试一个非常简单的BindService和Messenger通信示例。因为我在message.obj字段中传递message对象,所以我在自己的Android库项目中定义了要在服务和客户端之间共享的消息。通过将消息库作为depdency包含,我可以很好地编译,但在运行时dalvikvm抱怨它找不到类。我读过关于出口和订单的其他相关问题,但同样的解决方案对我的情况没有帮助。这一定是个很简单的错误,但我现在正在努力 应用程序依赖于SupportServiceMessag
01-31 01:53:41.134: E/dalvikvm(22886): Could not find class 'com.example.service.RequestStatus', referenced from method com.example.MainActivity$ServiceReplyHandler.handleMessage
下面是一段代码
主要活动
package com.example;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.view.Menu;
import android.widget.TextView;
import com.example.appserver.R;
import com.example.service.MessageType;
import com.example.service.RequestStatus;
public class MainActivity extends Activity {
private TextView serverStatus;
/** Messenger for sending message to service. */
Messenger mServiceMessenger = null;
/** Flag indicating whether we have called bind on the service. */
boolean mBound;
/** Messenger for receiving message from service */
Messenger mClientMessenger = null;
class ServiceReplyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
if (msg.what == MessageType.MSG_REPLY) {
RequestStatus reqStatus = (RequestStatus) msg.obj;
}
}
}
/**
* Class for interacting with the main interface of the service.
*/
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
// This is called when the connection with the service has been
// established, giving us the object we can use to
// interact with the service. We are communicating with the
// service using a Messenger, so here we get a client-side
// representation of that from the raw IBinder object.
mServiceMessenger = new Messenger(service);
Message regMsg = Message.obtain(null,
MessageType.MSG_REGISTER);
regMsg.replyTo = mClientMessenger;
try {
mServiceMessenger.send(regMsg);
} catch (RemoteException e) {
}
mBound = true;
}
public void onServiceDisconnected(ComponentName className) {
// This is called when the connection with the service has been
// unexpectedly disconnected -- that is, its process crashed.
mServiceMessenger = null;
mBound = false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_server);
serverStatus = (TextView) findViewById(R.id.server_status);
Intent startServiceIntent = new Intent();
startServiceIntent.setComponent(new ComponentName(
"com.example.supportservices",
"com.example.service.EntitlementService"));
bindService(startServiceIntent, mConnection, Context.BIND_AUTO_CREATE);
// Initialize messenger to receiver service messages;
mClientMessenger = new Messenger(new ServiceReplyHandler());
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onStart() {
super.onStart();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.server, menu);
return true;
}
}
以及信息本身
package com.example.service;
import org.json.JSONException;
import org.json.JSONObject;
import android.text.TextUtils;
public class RequestStatus {
public static int REQUEST_SUCCESS = 0;
public static int REQUEST_FAILURE = 1;
public static String TYPE = "request_status";
public static String STATUS_JSON_KEY = "status";
private int status;
public RequestStatus(int status) {
this.status = status;
}
public static RequestStatus fromJson(String json) {
try {
JSONObject msg = new JSONObject(json);
String type = msg.getString("type");
if (TextUtils.isEmpty(type) || TYPE.equals(type) == false) {
return null;
}
return(new RequestStatus(msg.getJSONObject("body").getInt(STATUS_JSON_KEY)));
} catch (JSONException e) {
return null;
}
}
public String toJson() {
JSONObject msg = new JSONObject();
try {
msg.put("type", TYPE);
JSONObject body = new JSONObject();
body.put(STATUS_JSON_KEY, status);
msg.put("body", body);
} catch (JSONException e) {
return null;
}
return msg.toString();
}
}
转到应用程序的project属性,将其添加为Android下的库,以及build path下的project dependency。首先提取APK内容,并查看手册。该类是否确实存在于此处。APK只是zip存档。谢谢,我能够在库jar中找到该类,但在实际应用程序的dex中没有看到。我终于发现了我的问题,非常简单,要将其用作android库,我需要转到应用程序的project属性,并将其添加为android下的库。除了build pathYup下的project dependency,现在它也在apk中。很好!要正式结束这个问题,请将我的答案标记为正确答案。