1

以下にコードを示します。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>

アラート.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>

文字列.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 Chemistry’s interactive periodic table which </string>

</resources>
4

5 に答える 5

4

これを使用してテキストビューを見つけます:

TextView t=(TextView)dialog.findViewById(R.id.tv1);
于 2012-05-22T04:22:51.487 に答える
2
TextView t=(TextView)dialog.findViewById(R.id.tv1);

これはあなたの解決策です

于 2012-05-22T04:16:37.573 に答える
2

textView の初期化を変更します..ダイアログ ビューが見つからないため、これを実行します--

TextView t=(TextView)dialog.findViewById(R.id.tv1);

これがあなたを助けることを願っています..

于 2012-05-22T04:22:20.053 に答える
1

if t.setText(getString(R.string.h)); 例外 NullPointerException をスローしています。これは、t または getString(R.string.h) のいずれかが null であることを意味します。main.xml に ID @+id/tv1 の TextView がない場合、t は null になる可能性があります。

于 2012-05-22T04:07:30.730 に答える
0

TextView text = (TextView)findviewByid(R.id.text1);

于 2012-05-22T04:52:42.317 に答える