我試圖啓動一個方面的android服務。我的項目是basicly執行以下操作:Android啓動服務的一個方面
- 簡單的活動了)和
- 在我Test.aj看點稱爲callMethodA(一個方法,我想之前開始我senderServicecallMethodA()調用。
但是,每次嘗試啓動服務時,都會引發InvocationTargetException。顯然,我的問題是在我的方面,因爲老實說,我不知道如何開始一個方面的服務。
這裏是我的Sender1Activity.java
package com.example.sender;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class Sender1Activity extends Activity {
Button btn_send;
Intent serviceIntent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sender1);
btn_send = (Button) findViewById(R.id.buttonSend);
}
public void callMethodA(View v){
System.out.println("Method A called");
}
@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;
}
}
和這裏的senderService.java:
package com.example.sender;
import java.util.Random;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
public class senderService extends Service {
String value = String.valueOf("a");
@Override
public void onCreate() {
super.onCreate();
}
public int onStartCommand(Intent intent, int flags, int startId) {
mSendValue.removeCallbacks(hMyValueTask);
mSendValue.post(hMyValueTask);
return startId;
}
public Handler mSendValue = new Handler();
public Runnable hMyValueTask = new Runnable() {
public void run() {
int n = new Random().nextInt(3000);
System.out.println("A random delay : " + n);
publishBuiltinAccelResults(value);
mSendValue.postDelayed(hMyValueTask, (long) n);
}
};
@Override
public void onDestroy() {
}
public void publishBuiltinAccelResults(String value) {
Intent intent = new Intent("ResultsA");
intent.putExtra("resultA", value);
sendBroadcast(intent);
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
最後,這裏的Test.aj看點代碼:
package com.example.sender;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public aspect Test extends Activity {
int i = 1;
protected void onCreate(Bundle savedInstanceState) {
};
pointcut pointcutCatchMethod() : execution(* callMethodA(*))
&& within(com.example.sender.Sender1Activity);
before() : pointcutCatchMethod() {
Intent sendIntent = new Intent(this, senderService.class);
startService(sendIntent);
System.out.println(" sendBroadcast cought by Aspect , aspect No: " + i);
}
}
logcat的:
03-07 15:20:27.015: E/AndroidRuntime(6714): FATAL EXCEPTION: main
03-07 15:20:27.015: E/AndroidRuntime(6714): java.lang.IllegalStateException: Could not execute method of the activity
03-07 15:20:27.015: E/AndroidRuntime(6714): at android.view.View$1.onClick(View.java:3660)
03-07 15:20:27.015: E/AndroidRuntime(6714): at android.view.View.performClick(View.java:4162)
03-07 15:20:27.015: E/AndroidRuntime(6714): at android.view.View$PerformClick.run(View.java:17082)
03-07 15:20:27.015: E/AndroidRuntime(6714): at android.os.Handler.handleCallback(Handler.java:615)
03-07 15:20:27.015: E/AndroidRuntime(6714): at android.os.Handler.dispatchMessage(Handler.java:92)
03-07 15:20:27.015: E/AndroidRuntime(6714): at android.os.Looper.loop(Looper.java:137)
03-07 15:20:27.015: E/AndroidRuntime(6714): at android.app.ActivityThread.main(ActivityThread.java:4867)
03-07 15:20:27.015: E/AndroidRuntime(6714): at java.lang.reflect.Method.invokeNative(Native Method)
03-07 15:20:27.015: E/AndroidRuntime(6714): at java.lang.reflect.Method.invoke(Method.java:511)
03-07 15:20:27.015: E/AndroidRuntime(6714): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
03-07 15:20:27.015: E/AndroidRuntime(6714): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
03-07 15:20:27.015: E/AndroidRuntime(6714): at dalvik.system.NativeStart.main(Native Method)
03-07 15:20:27.015: E/AndroidRuntime(6714): Caused by: java.lang.reflect.InvocationTargetException
03-07 15:20:27.015: E/AndroidRuntime(6714): at java.lang.reflect.Method.invokeNative(Native Method)
03-07 15:20:27.015: E/AndroidRuntime(6714): at java.lang.reflect.Method.invoke(Method.java:511)
03-07 15:20:27.015: E/AndroidRuntime(6714): at android.view.View$1.onClick(View.java:3655)
03-07 15:20:27.015: E/AndroidRuntime(6714): ... 11 more
03-07 15:20:27.015: E/AndroidRuntime(6714): Caused by: java.lang.NullPointerException
03-07 15:20:27.015: E/AndroidRuntime(6714): at android.content.ContextWrapper.startService(ContextWrapper.java:390)
03-07 15:20:27.015: E/AndroidRuntime(6714): at com.example.sender.Test.ajc$before$com_example_sender_Test$1$61afa7eb(Test.aj:23)
03-07 15:20:27.015: E/AndroidRuntime(6714): at com.example.sender.Sender1Activity.callMethodA(Sender1Activity.java:25)
03-07 15:20:27.015: E/AndroidRuntime(6714): ... 14 more
始終包括堆棧跟蹤。 –
我想你應該在onCreate中調用super。 (在該方面) – njzk2
沒有機會,存在同樣的問題。所以,在我的代碼中,我仍然不確定是否將我的方面作爲Activity的正確選擇。正如我所解釋的,我不知道如何在一個方面啓動服務,所以我所做的只是一個無望的嘗試。我在等你的指導。再次,我認爲我唯一的問題是找到一種方法來啓動任何服務的方面。 –