2016-09-26 4 views
0

У меня есть TextView в пользовательском макете для диалога. Его текст должен быть изменен, когда появится диалоговое окно.Как использовать setText() для редактирования текста в диалоге настраиваемого макета

 <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/final_score" 
     /> 

Java код, который я использовал, чтобы установить диалог текста и шоу

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    LayoutInflater inflater = this.getLayoutInflater(); 
    builder.setView(inflater.inflate(R.layout.its_over, null)); 
    AlertDialog dialog = builder.create(); 
    dialog.show(); 
    TextView t = (TextView)findViewById(R.id.final_score); 
    t.setText(""+score); 

я также попробовал этот код.

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    LayoutInflater inflater = this.getLayoutInflater(); 
    builder.setView(inflater.inflate(R.layout.its_over, null)); 
    AlertDialog dialog = builder.create(); 
    TextView t = (TextView)dialog.findViewById(R.id.final_score); 
    t.setText(""+score); 
    dialog.show(); 

но приложение будет разбиваться, когда вызывается этот метод.

, но если убрать

TextView t = (TextView)dialog.findViewById(R.id.final_score); 
    t.setText(""+score); 

он не откажет.

+0

Что говорит ваш крах? Что выводит Logcat? Знать, почему это происходит, важно. Тогда мы можем помочь дальше. Мне кажется, что это может быть нулевая ссылка, это 'TextView t = (TextView) dialog.findViewById (R.id.final_score); 'возврат нулевой или нет? –

ответ

3

Попробуйте получить доступ к TextView от его родителей Referance

View view = inflater.inflate(R.layout.its_over, null); 
builder.setView(view); 
TextView t = (TextView) view.findViewById(R.id.final_score);