为什么我在扩展EditText时遇到错误?

时间:2018-03-22 10:55:40

标签: android android-edittext

实际上,我试图通过引用this question来扩展editText以添加cursorChangedListener,但我不知道为什么我在使用时会收到错误在活动中。我试图引用一些相关的问题,但没有一个是有帮助的。我想知道问题出在哪里以及为什么它会出现错误。当我从EditTextSelectable类中删除了侦听器部分时,它没有给出任何错误,但添加了selectionChange部分给出错误。

MainActivity:

protected void onCreate(Bundle savedInstanceState) {
EditTextSelectable editTxt=findViewById(R.id.editTxt);
 editTxt.addOnSelectionChangedListener(new 
 EditTextSelectable.onSelectionChangedListener() {
        @Override
        public void onSelectionChanged(int selStart, int selEnd) {
          Toast.makeText(getApplicationContext(),selStart+"",Toast.LENGTH_SHORT).show();
        }
    });
}

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/tools"
app:ignore="NamespaceTypo">


<community.testing.EditTextSelectable
    android:id="@+id/editTxt"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="#fff"/>
</RelativeLayout>

代码:

public class EditTextSelectable extends EditText {

public interface onSelectionChangedListener {

     void onSelectionChanged(int selStart, int selEnd);
}



private List<onSelectionChangedListener> listeners;

public EditTextSelectable(Context context) {
    super(context);
    listeners = new ArrayList<onSelectionChangedListener>();
}

public EditTextSelectable(Context context, AttributeSet attrs) {
    super(context, attrs);
    listeners = new ArrayList<onSelectionChangedListener>();
}

public EditTextSelectable(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    listeners = new ArrayList<onSelectionChangedListener>();
}

public void addOnSelectionChangedListener(onSelectionChangedListener o) {
    listeners.add(o);
}

protected void onSelectionChanged(int selStart, int selEnd) {
    for (onSelectionChangedListener l : listeners)
        l.onSelectionChanged(selStart, selEnd);

}

}

错误:

FATAL EXCEPTION: main
                                                               java.lang.RuntimeException: Unable to start activity ComponentInfo{community.testing/community.testing.MainActivity}: android.view.InflateException: Binary XML file line #0: Error inflating class community.testing.EditTextSelectable
                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
                                                                   at android.app.ActivityThread.access$600(ActivityThread.java:141)
                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
                                                                   at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                   at android.os.Looper.loop(Looper.java:137)
                                                                   at android.app.ActivityThread.main(ActivityThread.java:5041)
                                                                   at java.lang.reflect.Method.invokeNative(Native Method)
                                                                   at java.lang.reflect.Method.invoke(Method.java:511)
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
                                                                   at dalvik.system.NativeStart.main(Native Method)
                                                                Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class community.testing.EditTextSelectable
                                                                   at android.view.LayoutInflater.createView(LayoutInflater.java:613)
                                                                   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
                                                                   at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
                                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
                                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
                                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
                                                                   at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
                                                                   at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
                                                                   at community.testing.MainActivity.onCreate(MainActivity.java:65)
                                                                   at android.app.Activity.performCreate(Activity.java:5104)
                                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
                                                                   at android.app.ActivityThread.access$600(ActivityThread.java:141) 
                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
                                                                   at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                   at android.os.Looper.loop(Looper.java:137) 
                                                                   at android.app.ActivityThread.main(ActivityThread.java:5041) 
                                                                   at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                   at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
                                                                   at dalvik.system.NativeStart.main(Native Method) 
                                                                Caused by: java.lang.reflect.InvocationTargetException
                                                                   at java.lang.reflect.Constructor.constructNative(Native Method)
                                                                   at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
                                                                   at android.view.LayoutInflater.createView(LayoutInflater.java:587)
                                                                   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687) 
                                                                   at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 
                                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
                                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
                                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
                                                                   at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) 
                                                                   at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
                                                                   at community.testing.MainActivity.onCreate(MainActivity.java:65) 
                                                                   at android.app.Activity.performCreate(Activity.java:5104) 
                                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
                                                                   at android.app.ActivityThread.access$600(ActivityThread.java:141) 
                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
                                                                   at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                   at android.os.Looper.loop(Looper.java:137) 
                                                                   at android.app.ActivityThread.main(ActivityThread.java:5041) 
                                                                   at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                   at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
                                                                   at dalvik.system.NativeStart.main(Native Method) 
                                                                Caused by: java.lang.NullPointerException
                                                                   at community.testing.EditTextSelectable.onSelectionChanged(EditTextSelectable.java:47)
                                                                   at android.widget.TextView.spanChange(TextView.java:7340)
                                                                   at android.widget.TextView$ChangeWatcher.onSpanAdded(TextView.java:8907)
                                                                   at android.text.SpannableStringBuilder.sendSpanAdded(SpannableStringBuilder.java:979)
                                                                   at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:688)
                                                                   at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:588)
                                                                   at android.text.Selection.setSelection(Selection.java:78)
                                                                   at android.text.Selection.setSelection(Selection.java:87)
                                                                   at android.text.method.ArrowKeyMovementMethod.initialize(ArrowKeyMovementMethod.java:302)
                                                                   at android.widget.TextView.setText(TextView.java:3684)
                                                                   at android.widget.TextView.setText(TextView.java:3554)
                                                                   at android.widget.EditText.setText(EditText.java:80)
                                                                   at android.widget.TextView.<init>(TextView.java:1213)
                                                                   at android.widget.EditText.<init>(EditText.java:60)
                                                                   at android.widget.EditText.<init>(EditText.java:56)
                                                                   at community.testing.EditTextSelectable.<init>(EditTextSelectable.java:33)
                                                                   at java.lang.reflect.Constructor.constructNative(Native Method) 
                                                                   at java.lang.reflect.Constructor.newInstance(Constructor.java:417) 
                                                                   at android.view.LayoutInflater.createView(LayoutInflater.java:587) 
                                                                   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687) 
                                                                   at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 
                                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
                                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
                                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
                                                                   at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) 
                                                                   at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
                                                                   at community.testing.MainActivity.onCreate(MainActivity.java:65) 
                                                                   at android.app.Activity.performCreate(Activity.java:5104) 
                                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
                                                                   at android.app.ActivityThread.access$600(ActivityThread.java:141) 
                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
                                                                   at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                   at android.os.Looper.loop(Looper.java:137) 
                                                                   at android.app.ActivityThread.main(ActivityThread.java:5041) 
                                                                   at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                   at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
                                                                   at dalvik.system.NativeStart.main(Native Method) 

0 个答案:

没有答案