Я дал свой код ниже. Я просто хочу создать пользовательский диалог в android.когда я нажимаю кнопку, он покажет собственный диалог. Я создал xml "alert.xml". После нажатия кнопки я покажу содержимое из файла string.xml
public class TriangleActivity extends Activity implements View.OnClickListener{
/** Called when the activity is first created. */
private Button bt;
private Dialog dialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt=(Button) findViewById(R.id.button1);
bt.setOnClickListener(this);
//Context mContext = getApplicationContext();
dialog = new Dialog(TriangleActivity.this);
dialog.setContentView(R.layout.alert);
dialog.setTitle("This is my custom dialog box");
TextView t=(TextView) findViewById(R.id.tv1);
t.setText(getString(R.string.h));
}
public void onClick(View v)
{
dialog.show();
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<Button
android:id="@+id/button1"
android:layout_width="60dp"
android:layout_height="60dp"
/>
</LinearLayout>
alert.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbarAlwaysDrawVerticalTrack="true" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv1" >
</TextView>
</LinearLayout>
</ScrollView>
string.xml
<?xml version="1.0" encoding="utf-8"?>I am getting
<resources>
<string name="hello">Hello World, TriangleActivity!</string>
<string name="app_name">Triangle</string>
<string name="h">The Royal Society of Chemistrys interactive periodic table which </string>
</resources>
используйте это, чтобы найти текстовое представление:
TextView t=(TextView)dialog.findViewById(R.id.tv1);
Измените вашу инициализацию textView.. потому что ее невозможно найти диалоговое окно.. также this--
TextView t=(TextView)dialog.findViewById(R.id.tv1);
Надеюсь, что это поможет вам..
TextView t=(TextView)dialog.findViewById(R.id.tv1);
Это чувак ур решения
если t.setText(getString (R.string.h)); исключает исключение NullPointerException, это означает, что либо t, либо getString (R.string.h) имеет значение null. t может быть null, если main.xml не имеет TextView с id, @+id/tv1.
TextView text = (TextView) findviewByid (R.id.text1);
this.getResources().getString((R.string.h)
вместоgetString(R.string.h)