Currently i am having a
java.lang.NullPointerException error for android.widget.EditText.addTextChangedListener
What i am trying to do is letting the user, key in a text/string into a "EditText" field. Once the code detect a change in the "EditText" field, it will get text/string from the "EditText" field and automatic display it out into a field name "tv".
i have search online for a solution but none work for me
below is what i have tried
what i found so far online suggested putting a final in front of the EditText which does not work for me
final EditText filename1 = (EditText)findViewById(container.findViewById(R.id.editText1);
Below is the code which the LogCat feedback the error
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;
}
}
Full Java Code:
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);
}
}
}
Full XML File:
<?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>
Inflate your xml
layout inside your onCreateView(...)
method and get the reference of your id's.
View view=inflater.inflate(R.layout.yourlayout, container,false);
EditText filename1 = (EditText) view.findViewById(R.id.editText1);
You need to inflate
your layout into a View
in onCreateView()
method, you can so something like below
@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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.