2

我正在尝试将 TextChangedListener 添加到我的 EditText:

        statusText = (EditText) findViewById(R.id.statusText);
        //Everythig is ok here...
        statusText.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable s) {
            //After this moment app crushes with Null pointer error
            }

            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
            }

            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
            }
        });

我的日志:

    12-09 02:29:25.198: E/AndroidRuntime(5167): FATAL EXCEPTION: main
12-09 02:29:25.198: E/AndroidRuntime(5167): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bresiu.elemelek/com.bresiu.elemelek.MainActivity}: java.lang.NullPointerException
12-09 02:29:25.198: E/AndroidRuntime(5167):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-09 02:29:25.198: E/AndroidRuntime(5167):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-09 02:29:25.198: E/AndroidRuntime(5167):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-09 02:29:25.198: E/AndroidRuntime(5167):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-09 02:29:25.198: E/AndroidRuntime(5167):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-09 02:29:25.198: E/AndroidRuntime(5167):     at android.os.Looper.loop(Looper.java:137)
12-09 02:29:25.198: E/AndroidRuntime(5167):     at android.app.ActivityThread.main(ActivityThread.java:5039)
12-09 02:29:25.198: E/AndroidRuntime(5167):     at java.lang.reflect.Method.invokeNative(Native Method)
12-09 02:29:25.198: E/AndroidRuntime(5167):     at java.lang.reflect.Method.invoke(Method.java:511)
12-09 02:29:25.198: E/AndroidRuntime(5167):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
12-09 02:29:25.198: E/AndroidRuntime(5167):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-09 02:29:25.198: E/AndroidRuntime(5167):     at dalvik.system.NativeStart.main(Native Method)
12-09 02:29:25.198: E/AndroidRuntime(5167): Caused by: java.lang.NullPointerException
12-09 02:29:25.198: E/AndroidRuntime(5167):     at com.bresiu.elemelek.MainActivity.onTabSelected(MainActivity.java:213)
12-09 02:29:25.198: E/AndroidRuntime(5167):     at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:570)
12-09 02:29:25.198: E/AndroidRuntime(5167):     at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:499)
12-09 02:29:25.198: E/AndroidRuntime(5167):     at com.android.internal.app.ActionBarImpl.addTab(ActionBarImpl.java:485)
12-09 02:29:25.198: E/AndroidRuntime(5167):     at com.bresiu.elemelek.MainActivity.onCreate(MainActivity.java:107)
12-09 02:29:25.198: E/AndroidRuntime(5167):     at android.app.Activity.performCreate(Activity.java:5104)
12-09 02:29:25.198: E/AndroidRuntime(5167):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-09 02:29:25.198: E/AndroidRuntime(5167):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-09 02:29:25.198: E/AndroidRuntime(5167):     ... 11 more 
12-09 02:29:27.068: E/GetJar SDK(5181): PackageMonitor: doOnReceive(): failed
12-09 02:29:27.068: E/GetJar SDK(5181): java.lang.IllegalStateException: Unable to access the application key
12-09 02:29:27.068: E/GetJar SDK(5181):     at com.getjar.sdk.data.metadata.PackageMonitor.doOnReceive(PackageMonitor.java:113)
12-09 02:29:27.068: E/GetJar SDK(5181):     at com.getjar.sdk.data.metadata.PackageMonitor.access$000(PackageMonitor.java:62)
12-09 02:29:27.068: E/GetJar SDK(5181):     at com.getjar.sdk.data.metadata.PackageMonitor$1.run(PackageMonitor.java:87)
12-09 02:29:27.068: E/GetJar SDK(5181):     at java.lang.Thread.run(Thread.java:856)
12-09 02:29:30.628: E/GetJar SDK(5576): PackageMonitor: doOnReceive(): failed
12-09 02:29:30.628: E/GetJar SDK(5576): java.lang.IllegalStateException: Unable to access the application key
12-09 02:29:30.628: E/GetJar SDK(5576):     at com.getjar.sdk.data.metadata.PackageMonitor.doOnReceive(PackageMonitor.java:113)
12-09 02:29:30.628: E/GetJar SDK(5576):     at com.getjar.sdk.data.metadata.PackageMonitor.access$000(PackageMonitor.java:62)
12-09 02:29:30.628: E/GetJar SDK(5576):     at com.getjar.sdk.data.metadata.PackageMonitor$1.run(PackageMonitor.java:87)
12-09 02:29:30.628: E/GetJar SDK(5576):     at java.lang.Thread.run(Thread.java:856)

我检查了我的 layout.xml,一切正常。我认为这是由 FragmentActivity (我的应用程序实现选项卡)引起的。我应该添加哪些附加信息来更好地解释问题?

主要活动:

http://pastebin.com/NAiw8VFe

4

1 回答 1

1

您的内容视图是R.layout.main,但您正在从 tab1 请求一个值。似乎您在选项卡内设置了视图。我建议您将所有设置操作侦听器移动到您的选项卡界面,在onCreateView. 通过以下方式引用正确的视图:

statusText = (EditText) rootView.findViewById(R.id.statusText);
于 2012-12-09T02:42:39.363 回答