正如我所理解的(从这里),LoopJ AndroidAsyncHttp不使用UI线程,因此我可以在主线程中执行它的方法。但当我初始化AsyncHttpClient的新实例时,应用程序崩溃(异常:java.lang.IollegalStateException:无法执行活动的方法)
代码:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void Send(View view) {
EditText editText = (EditText) findViewById(R.id.editText1);
String str = editText.getText().toString();
TextView div = (TextView) findViewById(R.id.textView1);
div.setText(str);
//crash
AsyncHttpClient client = new AsyncHttpClient();
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
错误:
09-25 09:58:07.427: W/dalvikvm(2514): threadid=1: thread exiting with uncaught exception (group=0xb1cb2b20)
09-25 09:58:07.477: E/AndroidRuntime(2514): FATAL EXCEPTION: main
09-25 09:58:07.477: E/AndroidRuntime(2514): Process: com.example.mysecondapp, PID: 2514
09-25 09:58:07.477: E/AndroidRuntime(2514): java.lang.IllegalStateException: Could not execute method of the activity
09-25 09:58:07.477: E/AndroidRuntime(2514): at android.view.View$1.onClick(View.java:3823)
09-25 09:58:07.477: E/AndroidRuntime(2514): at android.view.View.performClick(View.java:4438)
09-25 09:58:07.477: E/AndroidRuntime(2514): at android.view.View$PerformClick.run(View.java:18422)
09-25 09:58:07.477: E/AndroidRuntime(2514): at android.os.Handler.handleCallback(Handler.java:733)
09-25 09:58:07.477: E/AndroidRuntime(2514): at android.os.Handler.dispatchMessage(Handler.java:95)
09-25 09:58:07.477: E/AndroidRuntime(2514): at android.os.Looper.loop(Looper.java:136)
09-25 09:58:07.477: E/AndroidRuntime(2514): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-25 09:58:07.477: E/AndroidRuntime(2514): at java.lang.reflect.Method.invokeNative(Native Method)
09-25 09:58:07.477: E/AndroidRuntime(2514): at java.lang.reflect.Method.invoke(Method.java:515)
09-25 09:58:07.477: E/AndroidRuntime(2514): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-25 09:58:07.477: E/AndroidRuntime(2514): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-25 09:58:07.477: E/AndroidRuntime(2514): at dalvik.system.NativeStart.main(Native Method)
09-25 09:58:07.477: E/AndroidRuntime(2514): Caused by: java.lang.reflect.InvocationTargetException
09-25 09:58:07.477: E/AndroidRuntime(2514): at java.lang.reflect.Method.invokeNative(Native Method)
09-25 09:58:07.477: E/AndroidRuntime(2514): at java.lang.reflect.Method.invoke(Method.java:515)
09-25 09:58:07.477: E/AndroidRuntime(2514): at android.view.View$1.onClick(View.java:3818)
09-25 09:58:07.477: E/AndroidRuntime(2514): ... 11 more
09-25 09:58:07.477: E/AndroidRuntime(2514): Caused by: java.lang.NoClassDefFoundError: com.loopj.android.http.AsyncHttpClient
09-25 09:58:07.477: E/AndroidRuntime(2514): at com.example.mysecondapp.MainActivity.Send(MainActivity.java:46)
09-25 09:58:07.477: E/AndroidRuntime(2514): ... 14 more
您将获得com.loopj.android.http.AsyncHttpClient 的NoClassDefFoundError
如果您是从IDE运行的,请确保将exported="true"添加到.classpath文件中android-async-http-1.4.6.jar的classpathentry中。
我不得不将jar文件移到"libs"文件夹中,然后将jar添加到项目中。。。谢谢大家!