我是 android RESTlet 的新手。我正在尝试连接到 url 并在单击按钮时获取信息。但我收到错误“java.lang.IllegalStateException:无法执行活动的方法”和“原因:java.lang.reflect.InvocationTargetException”。请让我知道如何解决这个问题。
private EditText text1;
private EditText text2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text1 = (EditText) findViewById(R.id.editText1);
text2 = (EditText) findViewById(R.id.editText2);
}
public void onClick(View view) throws Exception {
double lat;
double lon;
String url = BASE_URL + "sites/" + SITE_ID + "/stations/" + STATION_MAC + "/";
ClientResource itsClient = new ClientResource(url);
itsClient.setChallengeResponse(ChallengeScheme.HTTP_BASIC, USERNAME, PASSWORD);
// Retrieve and parse the JSON representation
JsonRepresentation jsonRep = new JsonRepresentation(itsClient.get());
JSONObject jsonObj = jsonRep.getJsonObject();
// Output results
lat=jsonObj.getJSONObject("loc").getDouble("lat");
lon=jsonObj.getJSONObject("loc").getDouble("lng");
switch (view.getId()) {
case R.id.button1:
String lats = Double.toString(lat);
text1.setText(lats);
String lons = Double.toString(lon);
text2.setText(lons);
break;
}
}
下面是logcat:
03-22 18:25:38.834: E/Trace(755): error opening trace file: No such file or directory (2)
03-22 18:25:39.064: E/dalvikvm(755): Could not find class 'org.restlet.resource.ClientResource', referenced from method com.android.navigation.MainActivity.onClick
03-22 18:25:39.064: W/dalvikvm(755): VFY: unable to resolve new-instance 546 (Lorg/restlet/resource/ClientResource;) in Lcom/android/navigation/MainActivity;
03-22 18:25:39.076: D/dalvikvm(755): VFY: replacing opcode 0x22 at 0x0018
03-22 18:25:39.096: D/dalvikvm(755): DexOpt: unable to opt direct call 0x0d80 at 0x1a in Lcom/android/navigation/MainActivity;.onClick
03-22 18:25:39.126: D/dalvikvm(755): DexOpt: unable to opt direct call 0x0d7e at 0x2c in Lcom/android/navigation/MainActivity;.onClick
03-22 18:25:40.036: I/Choreographer(755): Skipped 40 frames! The application may be doing too much work on its main thread.
03-22 18:25:40.114: D/gralloc_goldfish(755): Emulator without GPU emulation detected.
03-22 18:25:58.035: I/Choreographer(755): Skipped 210 frames! The application may be doing too much work on its main thread.
03-22 18:26:00.554: D/AndroidRuntime(755): Shutting down VM
03-22 18:26:00.564: W/dalvikvm(755): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
03-22 18:26:00.654: E/AndroidRuntime(755): FATAL EXCEPTION: main
03-22 18:26:00.654: E/AndroidRuntime(755): java.lang.IllegalStateException: Could not execute method of the activity
03-22 18:26:00.654: E/AndroidRuntime(755): at android.view.View$1.onClick(View.java:3591)
03-22 18:26:00.654: E/AndroidRuntime(755): at android.view.View.performClick(View.java:4084)
03-22 18:26:00.654: E/AndroidRuntime(755): at android.view.View$PerformClick.run(View.java:16966)
03-22 18:26:00.654: E/AndroidRuntime(755): at android.os.Handler.handleCallback(Handler.java:615)
03-22 18:26:00.654: E/AndroidRuntime(755): at android.os.Handler.dispatchMessage(Handler.java:92)
03-22 18:26:00.654: E/AndroidRuntime(755): at android.os.Looper.loop(Looper.java:137)
03-22 18:26:00.654: E/AndroidRuntime(755): at android.app.ActivityThread.main(ActivityThread.java:4745)
03-22 18:26:00.654: E/AndroidRuntime(755): at java.lang.reflect.Method.invokeNative(Native Method)
03-22 18:26:00.654: E/AndroidRuntime(755): at java.lang.reflect.Method.invoke(Method.java:511)
03-22 18:26:00.654: E/AndroidRuntime(755): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-22 18:26:00.654: E/AndroidRuntime(755): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-22 18:26:00.654: E/AndroidRuntime(755): at dalvik.system.NativeStart.main(Native Method)
03-22 18:26:00.654: E/AndroidRuntime(755): Caused by: java.lang.reflect.InvocationTargetException
03-22 18:26:00.654: E/AndroidRuntime(755): at java.lang.reflect.Method.invokeNative(Native Method)
03-22 18:26:00.654: E/AndroidRuntime(755): at java.lang.reflect.Method.invoke(Method.java:511)
03-22 18:26:00.654: E/AndroidRuntime(755): at android.view.View$1.onClick(View.java:3586)
03-22 18:26:00.654: E/AndroidRuntime(755): ... 11 more
03-22 18:26:00.654: E/AndroidRuntime(755): Caused by: java.lang.NoClassDefFoundError: org.restlet.resource.ClientResource
03-22 18:26:00.654: E/AndroidRuntime(755): at com.android.navigation.MainActivity.onClick(MainActivity.java:36)
03-22 18:26:00.654: E/AndroidRuntime(755): ... 14 more
03-22 18:26:05.975: I/Process(755): Sending signal. PID: 755 SIG: 9
最初,我使用“添加外部 jar 文件”添加了 restlet jar 文件。后来,在谷歌搜索之后,我在 Libs 文件夹中添加了这些 jar 文件,并使用 Configure Build Path 中的 Add Jar files 添加了它们。当我尝试该方法时,我收到一条错误消息“运行时没有命令输出:'am start -n ....... 添加 jar 文件的正确方法是什么?我应该如何解决这个问题?