java.lang.IllegalStateException:无法执行活动的方法

时间:2013-03-22 12:14:24

标签: java android restlet

我是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文件添加它们。当我尝试这种方法时,我收到一条错误消息“运行时没有命令输出:'am start -n ..............  添加jar文件的正确方法是什么? 我该如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

Caused by: Unauthorized (401) - Unauthorized

所以你的错误很明显。您正尝试向某些Server发出请求,但很可能您提供了错误的登录名和密码,导致 401未经授权的访问

确保您传递了正确的LOGINPASSWORD