Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android.widget.EditText.addTextChangedListener的java.lang.NullPointerException_Java_Android_Nullpointerexception_Android Edittext - Fatal编程技术网

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

android.widget.EditText.addTextChangedListener的java.lang.NullPointerException,java,android,nullpointerexception,android-edittext,Java,Android,Nullpointerexception,Android Edittext,目前我有一个 的java.lang.NullPointerException错误 android.widget.EditText.addTextChangedListener 我试图做的是让用户在“EditText”字段中输入文本/字符串。一旦代码检测到“EditText”字段中的更改,它将从“EditText”字段中获取文本/字符串,并自动将其显示到字段名“tv”中 我在网上搜索了一个解决方案,但没有一个适合我 final EditText filename1 = (EditText)fin

目前我有一个

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

我试图做的是让用户在“EditText”字段中输入文本/字符串。一旦代码检测到“EditText”字段中的更改,它将从“EditText”字段中获取文本/字符串,并自动将其显示到字段名“tv”中

我在网上搜索了一个解决方案,但没有一个适合我

 final EditText filename1 = (EditText)findViewById(container.findViewById(R.id.editText1);
下面是我尝试过的

到目前为止,我在网上找到的建议是在编辑文本前放一个期末考试,这对我来说并不适用

 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的引用。

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

您需要在
onCreateView()
方法中将布局充气到
视图中,您可以执行如下操作

@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;   
}

container.findViewById()
将不起作用,您需要将您的“EditText”所在的布局充气。先生,下面一行是R.layout。您是否参考了容器所在的布局文件名?请参阅此处的container指的是视图组,即我们的父布局,在本例中,它指的是您的活动布局,其中添加了片段,希望您得到它,并且您的问题得到了解决??在某种程度上是的,但它产生了另一个问题。一旦我膨胀了布局。我左边的按钮看起来是复制的,出现在容器里。亲爱的先生,谢谢你的帮助。根据Satyen Udensh爵士的说法,我说我正在尝试。但我不确定“yourlayout”是否是指存放容器的布局文件名,您在这里定义了
editText1
id?您是否有任何
.xml
文件包含
EditText
?我已经在我的activity_main.xml(目前唯一的xml文件)中定义了editText1。在my activity_main.xml中,还有另一个editText调用editText2。请在问题中添加您的全部代码,以便我们能够轻松地找到它来解决您的问题。