1

ユーザーテキストを入力として受け入れて返す非常に単純なアプリがあります。返されたユーザー入力の下にボタンが必要な場合を除いて、アプリは機能しているようです。アクティビティのxmlファイルにボタンを追加したにもかかわらず、今のところボタンのないテキストを返すだけです。xml ファイルのグラフィック ビューにもボタンが表示されるので、問題は xml ファイルを DisplayMessageActivity.java ファイルに接続する方法を見つけることであることがわかります。以下は私の DisplayMessageActivity.java ファイルのスニペットで、何か間違ったことをしていると思います。おそらく、setcontentview 関数を呼び出すべきではありませんか?

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);      
    // Get the message from the intent
    Intent intention1=getIntent();
    final String message = intention1.getStringExtra(MainActivity.EXTRA_MESSAGE);


    // Create the text view
    TextView textView = new TextView(this);
    textView.setTextSize(40);

   // Set the text view as the activity layout
      textView.setText(message);
      setContentView(textView);



    }
4

2 に答える 2

1

setContentView(TextView) を呼び出すのはなぜですか? アクティビティ レイアウトを表す完全なレイアウト ファイルを拡張する必要があります。

setContentView(R.layout.activity_layout).  //Inflate the layout of your activity

次に、その親レイアウトからボタンを膨らませる必要があるため、次のようなものになります

Button button = (Button) findViewById(R.id.button1);  //Inflate the button that is inside 
                                                      //that layout

あなたの onCreate は、このように見えるはずです

private Button button;

protected void onCreate(Bundle savedInstanceState){
   setContentView(R.layout.activity_layout);   //Call this  first

   button = findViewById(R.id.button_id);
   button.setOnClickListener(this);


   //Inflate whatever other buttons/views you have inside your activity here

アクティビティの同じレイアウト ファイルで自分自身も定義していることを確認してください。幸運を

于 2013-06-26T00:54:09.563 に答える