簡體   English   中英

android.widget.EditText.addTextChangedListener 的 java.lang.NullPointerException

[英]java.lang.NullPointerException for android.widget.EditText.addTextChangedListener

目前我有一個

android.widget.EditText.addTextChangedListener 的 java.lang.NullPointerException 錯誤

我想要做的是讓用戶將文本/字符串鍵入“EditText”字段。 一旦代碼檢測到“EditText”字段中的更改,它將從“EditText”字段中獲取文本/字符串並自動將其顯示為字段名稱“tv”。

我在網上搜索了一個解決方案,但沒有一個對我有用

以下是我嘗試過的

到目前為止我在網上發現的建議在 EditText 前面放一個 final,這對我不起作用

 final EditText filename1 = (EditText)findViewById(container.findViewById(R.id.editText1);

下面是 LogCat 反饋錯誤的代碼

  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
  {
                    EditText filename1 = (EditText) container.findViewById(R.id.editText1);

                    filename1.addTextChangedListener(new TextWatcher(){
                    public void afterTextChanged(Editable s){

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

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

                        SeeThroughTextView tv = new SeeThroughTextView(getActivity());                      
                        tv.setLayoutParams(new FrameLayout.LayoutParams(1000, 1000));
                        tv.setText(s);
                        tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 60);
                        tv.setBackgroundResource(R.drawable.view_bg);                       
                    }
                });     
                return filename1;       
         }
  }

完整的Java代碼:

public class MainActivity extends ActionBarActivity
{

    int counter;

    Button button1, button2, button3, button4;
    ImageView image;
    EditText filename1, filename2;
    FrameLayout digit;

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        Fabric.with(this, new Crashlytics());

        setContentView(R.layout.activity_main);


        button1 = (Button) findViewById(R.id.button1);      
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);
        button4 = (Button) findViewById(R.id.button4);

        image = (ImageView) findViewById(R.id.imageView1);
        filename1 = (EditText) findViewById(R.id.editText1);
        filename2 = (EditText) findViewById(R.id.editText2);




        NumberPicker np1 = (NumberPicker) findViewById(R.id.np1);
        NumberPicker np2 = (NumberPicker) findViewById(R.id.np2);
        np1.setMaxValue(9);
        np1.setMinValue(0);
        np1.setWrapSelectorWheel(true);
        np1.setOnValueChangedListener( new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {

            }
        });

        np2.setMaxValue(9);
        np2.setMinValue(0);
        np2.setWrapSelectorWheel(true);
        np2.setOnValueChangedListener( new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {

            }
        });


        if (savedInstanceState == null)
        {

            getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
        }
    }


    public static class PlaceholderFragment extends Fragment
    {


        public PlaceholderFragment()
        {
        }




        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {

                    View layout = inflater.inflate(R.layout.activity_main, container, false);
                    EditText filename1 = (EditText)layout.findViewById(R.id.editText1);

                    filename1.addTextChangedListener(new TextWatcher(){
                    public void afterTextChanged(Editable s){

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

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



                        SeeThroughTextView tv = new SeeThroughTextView(getActivity());
                        tv.setLayoutParams(new FrameLayout.LayoutParams(1000, 1000));
                        tv.setText(s);
                        tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 60);
                        tv.setBackgroundResource(R.drawable.view_bg);


                    }
                });

                return layout;
        }
    }

    final static class SeeThroughTextView extends TextView
    {
        Bitmap mMaskBitmap;
        Canvas mMaskCanvas;
        Paint mPaint;

        Drawable mBackground;
        Bitmap mBackgroundBitmap;
        Canvas mBackgroundCanvas;
        private boolean mSetBoundsOnSizeAvailable = false;

        public SeeThroughTextView(Context context)
        {
            super(context);

            mPaint = new Paint();
            mPaint.setXfermode(new PorterDuffXfermode(Mode.DST_OUT));
            super.setTextColor(Color.BLACK);
            super.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }

        @Override
        @Deprecated
        public void setBackgroundDrawable(Drawable bg)
        {
            mBackground = bg;
            int w = bg.getIntrinsicWidth();
            int h = bg.getIntrinsicHeight();


            if (w == -1 || h == -1)
            {
                w = getWidth();
                h = getHeight();
            }


            if (w == 0 || h == 0)
            {
                mSetBoundsOnSizeAvailable = true;
                return;
            }

            mBackground.setBounds(100, 100, w, h);
            invalidate();
        }

        @Override
        public void setBackgroundColor(int color)
        {
            setBackgroundDrawable(new ColorDrawable(color));
        }

        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh)
        {
            super.onSizeChanged(w, h, oldw, oldh);
            mBackgroundBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
            mBackgroundCanvas = new Canvas(mBackgroundBitmap);
            mMaskBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
            mMaskCanvas = new Canvas(mMaskBitmap);

            if (mSetBoundsOnSizeAvailable)
            {

                mBackground.setBounds(0, 0, w, h);
                mSetBoundsOnSizeAvailable = false;
            }
        }

        @Override
        protected void onDraw(Canvas canvas)
        {

            mBackground.draw(mBackgroundCanvas);
            mMaskCanvas.drawColor(Color.BLACK, PorterDuff.Mode.CLEAR);
            super.onDraw(mMaskCanvas);
            mBackgroundCanvas.drawBitmap(mMaskBitmap,100, 100, mPaint);
            canvas.drawBitmap(mBackgroundBitmap, 0.f, 0.f, null);
        }
    }
}

完整的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RelativeLayout

        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:orientation="vertical" >

        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="400dp"
            android:layout_toLeftOf="@+id/scrollView1"
            android:background="@drawable/activity_bg"
            app:context="com.example.crossover.MainActivity"
            app:ignore="MergeRootFrame" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="28dp" />
        </FrameLayout>

        <ScrollView
            android:id="@+id/scrollView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_gravity="right" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="end"
                android:orientation="vertical" >

                <Button
                    android:id="@+id/button1"
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:layout_margin="10dp"
                    android:text="Button" />

                <Button
                    android:id="@+id/button2"
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:layout_margin="10dp"
                    android:text="Button" />

                <Button
                    android:id="@+id/button3"
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:layout_margin="10dp"
                    android:text="Button" />

                <Button
                    android:id="@+id/button4"
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:layout_margin="10dp"
                    android:text="Button" />

                <Button
                    android:id="@+id/button5"
                    style="?android:attr/buttonStyleSmall"
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:layout_margin="10dp"
                    android:text="Button" />

            </LinearLayout>
        </ScrollView>

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <NumberPicker
            android:id="@+id/np1"
            android:layout_width="40dp"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/np2"
            android:descendantFocusability="blocksDescendants"
            android:width="50dip" />

        <NumberPicker
            android:id="@+id/np2"
            android:layout_width="40dp"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toLeftOf="@+id/button6"
            android:descendantFocusability="blocksDescendants"
            android:width="50dip" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginTop="22dp"
            android:layout_toLeftOf="@+id/np1"
            android:ems="10"
            android:maxLength="9"
            android:singleLine="true" />

        <EditText
            android:id="@+id/editText2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/editText1"
            android:layout_toLeftOf="@+id/np1"
            android:ems="10"
            android:maxLength="3"
            android:singleLine="true" >

            <requestFocus />
        </EditText>

        <Button
            android:id="@+id/button6"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_margin="10dp"
            android:text="Button" />

    </RelativeLayout>

</LinearLayout>

在您的onCreateView(...)方法中擴充您的xml布局並獲取您的id's.引用id's.

View view=inflater.inflate(R.layout.yourlayout, container,false);
EditText filename1 = (EditText) view.findViewById(R.id.editText1);

你需要在onCreateView()方法中將你的布局inflate到一個View中,你可以像下面這樣

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflating view layout
    View layout = inflater.inflate(R.layout.you, container, false);
     EditText filename1 = (EditText) layout.findViewById(R.id.editText1);

                filename1.addTextChangedListener(new TextWatcher(){
                public void afterTextChanged(Editable s){

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

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

                    SeeThroughTextView tv = new SeeThroughTextView(getActivity());                      
                    tv.setLayoutParams(new FrameLayout.LayoutParams(1000, 1000));
                    tv.setText(s);
                    tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 60);
                    tv.setBackgroundResource(R.drawable.view_bg);                       
                }
            });     
            return layout;   
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM