初始化AndroidAsyncHttp时Android应用程序崩溃

时间:2014-09-25 17:25:11

标签: java android http android-async-http

据我所知(来自here),LoopJ AndroidAsyncHttp不使用UI线程,因此我可以在主线程中执行其方法。 但当我使用AsyncHttpClient的新实例时,应用程序崩溃了 (例外:java.lang.IllegalStateException:无法执行活动的方法)

代码:

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

2 个答案:

答案 0 :(得分:0)

您正在为com.loopj.android.http.AsyncHttpClient获取NoClassDefFoundError

如果您是从IDE运行,请确保添加exported =" true"到.classpath文件中的android-async-http-1.4.6.jar的classpathentry。

答案 1 :(得分:0)

我必须将jar文件移动到“libs”文件夹,然后将jar添加到项目中...谢谢大家!