2017-01-29 58 views
4

我必须动态改变TypefaceEditText,并在字体改变时处理提示文本。我简单地做如下:以编程方式在android中获取提示文本值Edittext

<android.support.design.widget.TextInputLayout 
    android:id="@+id/input_layout_textBox" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    <EditText 
     android:id="@+id/textBox" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="@string/hint_date" /> 
</android.support.design.widget.TextInputLayout> 

我的代码片段如下:

EditText textBox = (EditText) findViewById(R.id.textBox); 
Typeface tf = Typeface.createFromAsset(this, "myfont.ttf"); 
textBox.setTypeface(tf); 
String hintText = textBox.getHint().toString(); //Got NullPointerException here.... 
String newText = processText(hintText); //This will process the hint text; 
textBox.setHint(newText); 

当我欠幅脉冲程序,我有以下异常:

java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String java.lang.CharSequence.toString()' on a null object reference

请帮助我解决这个问题。

+0

你试过getParent()。getHint()吗?您可能正在使用设计支持库。 – AnixPasBesoin

+0

@Anix PasBeson在EditText中没有getParent()。getHint()。 –

+0

张贴您的xml plz。 – AnixPasBesoin

回答

1

我们需要首先查看您的XML以确定,但您可能正在使用设计支持库。

如果是这样,你可以得到你提示如下:

((TextInputLayout)textBox.getParent()).getHint(); 

注意

这已经回答了here。 (虽然我们需要确保你使用设计支持lin来标记你的问题是重复的)

相关问题