2015-01-31 47 views
0

我有一個應用程序,其中有兩個文本框和和按鈕。 在這裏,他們都在XML:Java android應用程序致命異常點擊按鈕

<EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="phone" 
     android:ems="10" 
     android:id="@+id/editText" android:layout_gravity="center_horizontal" android:phoneNumber="true" 
     android:hint="Номер телефона"/> 
<EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="textPassword" 
     android:ems="10" 
     android:id="@+id/editText2" android:password="true" android:hint="Пароль"/> 
<Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Войти" 
     android:onClick="sendMessage" 
     android:id="@+id/button" android:layout_gravity="center_horizontal"/> 

這裏也是和的sendMessage功能generateJSON:

public void sendMessage(View view) 
{ 
    Intent intent = new Intent(TestActivity.this, RegActivity.class); 
    startActivity(intent); 
    String userDataJSON=this.generateJson(this.login.getText().toString(),this.password.getText().toString()) ; 
    System.out.println(userDataJSON); 
} 

private String generateJson(String login, String password){ 
    JSONObject j = new JSONObject(); 
    j.put("login",login); 
    j.put("password",password); 
    String res=j.toString(); 
    return res; 
} 

如果我只輸出this.login.getText().toString()this.password.getText().toString()它輸出的罰款。但是,如果我嘗試在點擊時輸出userDataJSON,就會出現如下錯誤:

01-31 22:19:16.137 2043-2043/com.example.test E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: com.example.test, PID: 2043 
    java.lang.IllegalStateException: Could not execute method of the activity 
      at android.view.View$1.onClick(View.java:4007) 
      at android.view.View.performClick(View.java:4756) 
      at android.view.View$PerformClick.run(View.java:19749) 
      at android.os.Handler.handleCallback(Handler.java:739) 
      at android.os.Handler.dispatchMessage(Handler.java:95) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5221) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:372) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
    Caused by: java.lang.reflect.InvocationTargetException 
      at java.lang.reflect.Method.invoke(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:372) 
      at android.view.View$1.onClick(View.java:4002) 
            at android.view.View.performClick(View.java:4756) 
            at android.view.View$PerformClick.run(View.java:19749) 
            at android.os.Handler.handleCallback(Handler.java:739) 
            at android.os.Handler.dispatchMessage(Handler.java:95) 
            at android.os.Looper.loop(Looper.java:135) 
            at android.app.ActivityThread.main(ActivityThread.java:5221) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:372) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
    Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lnet/sf/json/JSONObject; 
      at com.example.test.TestActivity.generateJson(TestActivity.java:30) 
      at com.example.test.TestActivity.sendMessage(TestActivity.java:25) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:372) 
            at android.view.View$1.onClick(View.java:4002) 
            at android.view.View.performClick(View.java:4756) 
            at android.view.View$PerformClick.run(View.java:19749) 
            at android.os.Handler.handleCallback(Handler.java:739) 
            at android.os.Handler.dispatchMessage(Handler.java:95) 
            at android.os.Looper.loop(Looper.java:135) 
            at android.app.ActivityThread.main(ActivityThread.java:5221) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:372) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
    Caused by: java.lang.ClassNotFoundException: Didn't find class "net.sf.json.JSONObject" on path: DexPathList[[zip file "/data/app/com.example.test-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]] 
      at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56) 
      at java.lang.ClassLoader.loadClass(ClassLoader.java:511) 
      at java.lang.ClassLoader.loadClass(ClassLoader.java:469) 
            at com.example.test.TestActivity.generateJson(TestActivity.java:30) 
            at com.example.test.TestActivity.sendMessage(TestActivity.java:25) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:372) 
            at android.view.View$1.onClick(View.java:4002) 
            at android.view.View.performClick(View.java:4756) 
            at android.view.View$PerformClick.run(View.java:19749) 
            at android.os.Handler.handleCallback(Handler.java:739) 
            at android.os.Handler.dispatchMessage(Handler.java:95) 
            at android.os.Looper.loop(Looper.java:135) 
            at android.app.ActivityThread.main(ActivityThread.java:5221) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:372) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
    Suppressed: java.lang.ClassNotFoundException: net.sf.json.JSONObject 
      at java.lang.Class.classForName(Native Method) 
      at java.lang.BootClassLoader.findClass(ClassLoader.java:781) 
      at java.lang.BootClassLoader.loadClass(ClassLoader.java:841) 
      at java.lang.ClassLoader.loadClass(ClassLoader.java:504) 
      ... 16 more 
    Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available 

問題是什麼?在應用程序中也有這樣的錯誤: App screen 那麼我的應用程序出了什麼問題?一切似乎都是對的。

+0

移除此字符串userDataJSON = this.generateJson(this.login.getText()的toString(),this.password.getText()的toString(。 )); – sandeepmaaram 2015-01-31 17:45:40

回答

1

你是否做過所有正確的導入?您在嘗試撥打net.sf.json.JSONObject時收到ClassNotFoundException

按照JSON-lib的主頁,你必須,至少,在你的類路徑以下依賴性:

  • 的Jakarta Commons琅2.5
  • 的Jakarta Commons-BeanUtils的1.8.0
  • 的Jakarta Commons的集合3.2.1
  • 雅加達共享記錄1.1.1
  • ezmorph 1.0.6

有一個現有的交,討論這個問題進一步:Java runtime ClassNotFoundException