2017-01-29 8 views
4

TypefaceEditTextに動的に変更する必要があります。また、書体が変更された場合はヒントテキストを処理する必要があります。私は単純に次のようにしてください:プログラムでヒントテキストの値をアンドロイドで取得する

<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> 

そして、私のコードスニペットは以下の通りです:

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

てください:

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); 

私はプログラムをラントとき、私は次の例外を得ましたこの問題を解決するのに役立ちます。

+0

getParent()。getHint()を試したことがありますか?あなたはおそらくデザインサポートlibを使用しています。 – AnixPasBesoin

+0

@Anix PasBeson EditTextにはgetParent()。getHint()はありません。 –

+0

あなたのXML plzを投稿してください。 – AnixPasBesoin

答えて

1

あなたのXMLを最初に調べる必要がありますが、これはおそらくデザインサポートlibを使用しています。

その場合は、次のように、あなたはあなたのヒントを得ることができます:これはすでにhereに答えてきた

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

注意。 (質問を重複としてマークするには、デザインサポートのlinを使用していることを確認する必要がありましたが)

関連する問題