2014-01-31 337 views
0

需要挖掘集體智力在這裏。我正在嘗試一個非常簡單的BindService並與Messenger例子進行通信。由於我在Message.obj字段中傳遞消息對象,因此我在自己的Android庫項目中定義了要在服務和客戶端之間共享的消息。我可以通過將消息庫作爲depdendency來編譯,但在運行時dalvikvm抱怨找不到類。我已閱讀與出口和訂單有關的其他相關問題,但同樣的解決方案對我來說沒有幫助。這肯定是一個非常簡單的錯誤,但我現在正在拉我的頭髮。DalvikVm無法找到類的Android庫項目

應用程序依賴於SupportServiceMessageLibrary。

應用程序在項目的順序選項卡中的src之前有庫。因爲沒有人在消費應用

圖書館出口其src和根文件夾

庫不檢查在應用出口。

從日誌貓錯誤消息:

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 

這裏是代碼

MainActivity

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(); 
} 
} 
+0

首先提取APK內容,看手冊真的是這個類存在這裏。 APK只是zip存檔。 – Divers

+0

潛水員,謝謝我能夠在庫jar中找到類,但沒有在實際的應用程序的dex中看到。我終於發現我的問題,非常簡單,將其用作Android庫我需要去應用程序的項目屬性,並將其作爲Android下的庫添加到構建路徑下的項目依賴關係 – user3256732

+0

是的,現在它在apk以及。 – user3256732

回答

0

轉到項目公關片段操作的應用程序,並將其作爲Android下的庫添加到構建路徑下的項目依賴關係中。

相關問題