0

我的应用程序当前有多个视图和布局,我正在使用滑动视图设计,并且我在不是主视图的视图中有一个“EditText”框。

现在我正在尝试为该 EditText 框设置一个新的 TextWatcher,如果我在“OnCreate”中尝试这样做,我会得到 nullPointerException。

虽然如果我将此操作分配给放置在我的主要活动中的按钮之一,它工作得很好,所以我不知道在应用程序启动后如何设置 TextWatcher。

((EditText) findViewById(R.id.EditText1)).addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            ((EditText) findViewById(R.id.EditText2)).setText("try");
        }
    });

这是“OnCreate”:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    mSectionsPagerAdapter = new SectionsPagerAdapter(
            getSupportFragmentManager());

    compPagerAdapter = new compSectionsPagerAdapter(getSupportFragmentManager());

    mViewPager = (ViewPager) findViewById(R.id.comp);
    mViewPager.setAdapter(mSectionsPagerAdapter);


    compViewPager = (ViewPager) findViewById(R.id.ScreenPager);
    compViewPager.setAdapter(compPagerAdapter);;
    compViewPager.setOnPageChangeListener(mMainPagerListener);


    ((EditText) findViewById(R.id.EditText1)).addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            ((EditText) findViewById(R.id.EditText2)).setText("0000");
        }
    });

}

和 LogCat:

    09-27 14:38:15.988: E/AndroidRuntime(8179): FATAL EXCEPTION: main
09-27 14:38:15.988: E/AndroidRuntime(8179): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.casio/com.example.casio.MainActivity}: java.lang.NullPointerException
09-27 14:38:15.988: E/AndroidRuntime(8179):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-27 14:38:15.988: E/AndroidRuntime(8179):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-27 14:38:15.988: E/AndroidRuntime(8179):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-27 14:38:15.988: E/AndroidRuntime(8179):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-27 14:38:15.988: E/AndroidRuntime(8179):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-27 14:38:15.988: E/AndroidRuntime(8179):     at android.os.Looper.loop(Looper.java:137)
09-27 14:38:15.988: E/AndroidRuntime(8179):     at android.app.ActivityThread.main(ActivityThread.java:5103)
09-27 14:38:15.988: E/AndroidRuntime(8179):     at java.lang.reflect.Method.invokeNative(Native Method)
09-27 14:38:15.988: E/AndroidRuntime(8179):     at java.lang.reflect.Method.invoke(Method.java:525)
09-27 14:38:15.988: E/AndroidRuntime(8179):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-27 14:38:15.988: E/AndroidRuntime(8179):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-27 14:38:15.988: E/AndroidRuntime(8179):     at dalvik.system.NativeStart.main(Native Method)
09-27 14:38:15.988: E/AndroidRuntime(8179): Caused by: java.lang.NullPointerException
09-27 14:38:15.988: E/AndroidRuntime(8179):     at com.example.casio.MainActivity.onCreate(MainActivity.java:73)
09-27 14:38:15.988: E/AndroidRuntime(8179):     at android.app.Activity.performCreate(Activity.java:5133)
09-27 14:38:15.988: E/AndroidRuntime(8179):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-27 14:38:15.988: E/AndroidRuntime(8179):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-27 14:38:15.988: E/AndroidRuntime(8179):     ... 11 more

有问题的行是:

 ((EditText) findViewById(R.id.EditText1)).addTextChangedListener(new TextWatcher() {

我认为这与布局创建或类似的东西有关,但我无法让它工作。

4

0 回答 0