2014-09-25 73 views
0

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

代码:

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 
+0

plz把你的'AsyncHttpClient'代码 – Rustam 2014-09-25 17:30:43

+2

检查你的'logcat'' 09-25 09:58:07.477:E/AndroidRuntime(2514):引起:java.lang.NoClassDefFoundError:com.loopj.android.http。 AsyncHttpClient' – Rustam 2014-09-25 17:34:33

+0

看起来你缺少一个依赖。请参阅[this](http://stackoverflow.com/questions/20327940/android-no-class-def-found-error-for-com-loopj-android-http-asynchttpclient)或[this](http:// stackoverflow.com/questions/16980159/no-class-def-found-error-android) – codeMagic 2014-09-25 17:34:40

回答

0

你得到一个NoClassDefFoundError为com.loopj.android.http.AsyncHttpClient

如果你运行在IDE中,确保在.classpath文件中为android-async-http-1.4.6.jar的classpatterntry添加exported =“true”。

0

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