2012-02-29 32 views
0

我無法讓EditText在不是活動的類中工作。我得到堆棧跟蹤錯誤:我無法讓EditText在不屬於活動的類中工作

02-29 10:07:00.742: E/AndroidRuntime(1108): FATAL EXCEPTION: main 
02-29 10:07:00.742: E/AndroidRuntime(1108): java.lang.IllegalStateException: Could not execute method of the activity 
02-29 10:07:00.742: E/AndroidRuntime(1108):  at android.view.View$1.onClick(View.java:2683) 
02-29 10:07:00.742: E/AndroidRuntime(1108):  at android.view.View.performClick(View.java:3110) 
02-29 10:07:00.742: E/AndroidRuntime(1108):  at android.view.View$PerformClick.run(View.java:11934) 
02-29 10:07:00.742: E/AndroidRuntime(1108):  at android.os.Handler.handleCallback(Handler.java:587) 
02-29 10:07:00.742: E/AndroidRuntime(1108):  at android.os.Handler.dispatchMessage(Handler.java:92) 
02-29 10:07:00.742: E/AndroidRuntime(1108):  at android.os.Looper.loop(Looper.java:132) 
02-29 10:07:00.742: E/AndroidRuntime(1108):  at android.app.ActivityThread.main(ActivityThread.java:4123) 
02-29 10:07:00.742: E/AndroidRuntime(1108):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-29 10:07:00.742: E/AndroidRuntime(1108):  at java.lang.reflect.Method.invoke(Method.java:491) 
02-29 10:07:00.742: E/AndroidRuntime(1108):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 
02-29 10:07:00.742: E/AndroidRuntime(1108):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 
02-29 10:07:00.742: E/AndroidRuntime(1108):  at dalvik.system.NativeStart.main(Native Method) 
02-29 10:07:00.742: E/AndroidRuntime(1108): Caused by: java.lang.reflect.InvocationTargetException 
02-29 10:07:00.742: E/AndroidRuntime(1108):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-29 10:07:00.742: E/AndroidRuntime(1108):  at java.lang.reflect.Method.invoke(Method.java:491) 
02-29 10:07:00.742: E/AndroidRuntime(1108):  at android.view.View$1.onClick(View.java:2678) 
02-29 10:07:00.742: E/AndroidRuntime(1108):  ... 11 more 
02-29 10:07:00.742: E/AndroidRuntime(1108): Caused by: java.lang.NullPointerException 
02-29 10:07:00.742: E/AndroidRuntime(1108):  at com.lifeApp.RegisterProcessing.createUser(RegisterProcessing.java:57) 
02-29 10:07:00.742: E/AndroidRuntime(1108):  at com.lifeApp.RegisterActivity.onClick(RegisterActivity.java:69) 
02-29 10:07:00.742: E/AndroidRuntime(1108):  ... 14 more 

我的代碼是: 我有塊引用錯誤行

public void createUser(View v) { 

      Date cal=Calendar.getInstance().getTime(); 

      EditText firstname = (EditText) v.findViewById(R.id.firstname); 
      EditText lastname = (EditText) v.findViewById(R.id.lastname); 
      EditText email = (EditText) v.findViewById(R.id.email); 
      EditText phone = (EditText) v.findViewById(R.id.phone); 
      EditText address = (EditText) v.findViewById(R.id.address); 
      EditText password = (EditText) v.findViewById(R.id.password); 
      EditText confPassword = (EditText) v.findViewById(R.id.confPassword); 


      //need to error check!! 

      ContentValues values = new ContentValues(); 
  Log.i("firstname", ""+firstname.getText().toString()); 
  values.put(MySQLiteHelper.COLUMN_FIRSTNAME, firstname.getText().toString()); 
      values.put(MySQLiteHelper.COLUMN_LASTNAME, lastname.getText().toString()); 
      values.put(MySQLiteHelper.COLUMN_EMAIL, email.getText().toString()); 
      values.put(MySQLiteHelper.COLUMN_PHONE, phone.getText().toString()); 
      values.put(MySQLiteHelper.COLUMN_ADDRESS, address.getText().toString()); 
      values.put(MySQLiteHelper.COLUMN_PASSWORD, password.getText().toString()); 
      values.put(MySQLiteHelper.COLUMN_DATE, cal.toLocaleString()); 

      long insertId = database.insert(MySQLiteHelper.TABLE_REGISTER, null, 
        values); 
      // To show how to query 
      Cursor cursor = database.query(MySQLiteHelper.TABLE_REGISTER, 
        registerColumns, MySQLiteHelper.COLUMN_ID + " = ?", new String[] {""+insertId}, 
        null, null, null); 
      cursor.moveToFirst(); 
      //return cursorToREgister(cursor,policyId); 
     } 
+0

com.lifeApp.RegisterProcessing.createUser(RegisterProcessing.java:57)參見RegisterProcessing.java中的第57行Something is null ... – 2012-02-29 10:25:36

+0

請指出'RegisterProcessing.java'的第57行。 – Felix 2012-02-29 10:26:14

+0

嗨,大家好,抱歉,因爲缺少這些信息,Log.i(「firstname」,「」+ firstname.getText()。toString()); – user997101 2012-02-29 10:27:06

回答

0

堆棧跟蹤顯示問題是與上下文你應該傳遞你的活動上下文到這個類,並使用它的UI進程 和編輯文本使用ge tEditableText()。toString()而不是getText()。toString()

+0

嗨,感謝您的回覆,我試圖使用findViewById()的上下文,但它不工作。我現在修改了getEditableText()。謝謝 – user997101 2012-02-29 10:40:03

+0

嘿,如果它有幫助,那麼爲什麼你已經投下了它 任何方式在你的活動從你從哪裏調用這個類傳遞該活動的上下文並使用該類 – vipin 2012-02-29 10:48:34

+0

抱歉,我的手指滑過:)我已經改變了findViewById )EditText firstname =(EditText)((Activity)context).findViewById(R.id.firstname);但我得到錯誤02-29 10:44:48.592:E/AndroidRuntime(1278):引起:java.lang.ClassCastException:android.app.Application無法轉換爲android.app.Activity – user997101 2012-02-29 10:54:36