2017-01-29 56 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來標記你的問題是重複的)

相關問題