gpt4 book ai didi

java - Android 2.3 设备的 EditText TextWatcher java.lang.NullPointerException

转载 作者:行者123 更新时间:2023-11-30 01:53:18 25 4
gpt4 key购买 nike

我使用 edittext 和 textwatcher。我的应用程序在 android 2.3 上运行时崩溃

我使用以下代码:

MyEditText.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {


}

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


}

@Override
public void afterTextChanged(Editable s) {
if(salam!=null){

Mybit = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),Config.ARGB_8888);
Canvas canvas = new Canvas(Mybit);
canvas.drawBitmap(bitmap, 0, 0, null);
Drawtext = s.toString();
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.BLACK);
paint.setTextSize(320);
paint.setStyle(Style.FILL);
paint.setShadowLayer(10f, 10f, 10f, Color.BLACK);
paint.setTextAlign(Align.CENTER);
Rect rectText = new Rect();
paint.getTextBounds(Drawtext, 0, Drawtext.length(), rectText);
int x = (canvas.getWidth() / 2);
int y = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2)) ;
canvas.drawText(Drawtext, x, y, paint);


}

}
});

一位用户向我发送了以下日志:

java.lang.RuntimeException: Unable to start activity ComponentInfo{**************************************.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1816)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1837)
at android.app.ActivityThread.access$1500(ActivityThread.java:132)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:4196)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at ********************************.afterTextChanged(MainActivity.java:195)
at android.widget.TextView.sendAfterTextChanged(TextView.java:6561)
at android.widget.TextView.setText(TextView.java:2821)
at android.widget.TextView.setText(TextView.java:2682)
at android.widget.EditText.setText(EditText.java:84)
at android.widget.TextView.setText(TextView.java:2657)
at android.widget.TextView.onRestoreInstanceState(TextView.java:2557)
at android.view.View.dispatchRestoreInstanceState(View.java:6325)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:1279)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:1279)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:1279)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:1279)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:1279)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:1279)
at android.view.View.restoreHierarchyState(View.java:6304)
at com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:1538)
at android.app.Activity.onRestoreInstanceState(Activity.java:882)
at android.app.Activity.performRestoreInstanceState(Activity.java:854)
at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1162)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1794)
... 11 more

我还没有找到任何解决方案。我希望我可以在不改变结果的情况下做一些事情。提前致谢!

最佳答案

尝试改变

 if(photoPath!=null){ ... code}
to
if(null != photoPath && null != s){ ..... code}

我认为 Editable 可能为 null

关于java - Android 2.3 设备的 EditText TextWatcher java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32682472/

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