gpt4 book ai didi

java - 添加 TextChangedListener 时应用崩溃

转载 作者:行者123 更新时间:2023-11-30 11:38:02 26 4
gpt4 key购买 nike

我正在尝试将 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 引起的(我的应用程序实现了选项卡)。我应该添加哪些附加信息以更好地解释问题?

主要 Activity :

http://pastebin.com/NAiw8VFe

最佳答案

您的内容 View 是 R.layout.main,但您正在请求来自 tab1 的值。看来您在选项卡内设置了 View 。我建议您将所有设置 Action 监听器移动到选项卡界面,在 onCreateView 中。通过以下方式引用正确的 View :

statusText = (EditText) rootView.findViewById(R.id.statusText);

关于java - 添加 TextChangedListener 时应用崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13783893/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com