EditText.setText错误

时间:2012-06-10 07:51:25

标签: java android

解决!问题是我使用了this.findViewById,但我将使用dialog.findViewById。 TNX TO ALL和I BEG你的PARDON!

我有一个奇怪的(对我来说)问题。我尝试使用从游标中获取的字符串在edittext中编写,我知道这是正确的方法:

EditText myText;

     final Dialog dialog = new Dialog(this);
     dialog.setContentView(R.layout.login);
     dialog.setTitle("Login");
     dialog.setCancelable(true);

     Cursor lista=db.listaParametri();

     try
        {
            while (lista.moveToNext())
            {

                if (lista.getString(0).equals("username"))
                {

                    myText =  (EditText) this.findViewById(R.id.username);

                    Log.d("log", lista.getString(1));

                    myText.setText( lista.getString(1) );




                }



            }
        }
        finally
        {
            lista.close();
        }

但是当尝试编写whit settext()时会返回此错误:

06-10 09:43:01.829: E/AndroidRuntime(279): FATAL EXCEPTION: main
06-10 09:43:01.829: E/AndroidRuntime(279): java.lang.NullPointerException
06-10 09:43:01.829: E/AndroidRuntime(279):  at it.axiomatic.epsoft.sms.EpsoftSMSActivity.setup_parametri(EpsoftSMSActivity.java:147)
06-10 09:43:01.829: E/AndroidRuntime(279):  at it.axiomatic.epsoft.sms.EpsoftSMSActivity.onOptionsItemSelected(EpsoftSMSActivity.java:73)
06-10 09:43:01.829: E/AndroidRuntime(279):  at android.app.Activity.onMenuItemSelected(Activity.java:2195)
06-10 09:43:01.829: E/AndroidRuntime(279):  at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:730)
06-10 09:43:01.829: E/AndroidRuntime(279):  at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
06-10 09:43:01.829: E/AndroidRuntime(279):  at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
06-10 09:43:01.829: E/AndroidRuntime(279):  at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:532)
06-10 09:43:01.829: E/AndroidRuntime(279):  at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
06-10 09:43:01.829: E/AndroidRuntime(279):  at android.view.View$PerformClick.run(View.java:8816)
06-10 09:43:01.829: E/AndroidRuntime(279):  at android.os.Handler.handleCallback(Handler.java:587)
06-10 09:43:01.829: E/AndroidRuntime(279):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-10 09:43:01.829: E/AndroidRuntime(279):  at android.os.Looper.loop(Looper.java:123)
06-10 09:43:01.829: E/AndroidRuntime(279):  at android.app.ActivityThread.main(ActivityThread.java:4627)
06-10 09:43:01.829: E/AndroidRuntime(279):  at java.lang.reflect.Method.invokeNative(Native Method)
06-10 09:43:01.829: E/AndroidRuntime(279):  at java.lang.reflect.Method.invoke(Method.java:521)
06-10 09:43:01.829: E/AndroidRuntime(279):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-10 09:43:01.829: E/AndroidRuntime(279):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-10 09:43:01.829: E/AndroidRuntime(279):  at dalvik.system.NativeStart.main(Native Method)

怎么了? TNX!

3 个答案:

答案 0 :(得分:0)

简单的解决方案是调试它:) myText是否空? 你能发布完整的功能定义setup_parametri吗? 你可以在你指定用户名TextField的地方分享xml吗?

答案 1 :(得分:0)

或者:

  1. 你的textedit没有被正确定义(但如果你可以用它来做其他的事情那么它应该没问题)
  2. lista.getString(1)为空,尝试使用catlog打印它,说Log.d("log", lista.getString(1));应该输出字符串的值。

答案 2 :(得分:0)

解决!问题是我使用了this.findViewById,但我将使用dialog.findViewById。 TNX TO ALL和I BEG你的PARDON!