こんにちは、私は Android プログラミングに少し慣れていないので、問題があり、助けが必要です。
そこで、Android の公式サイトがトレーニングに使用する最初のアプリを使用します (http://developer.android.com/training/basics/firstapp/starting-activity.html)。
2番目の活動部分の作成について混乱しています。したがって、意図を渡した後、(XML の代わりに) Java コードを使用して新しい TextView を作成するので、xml を使用してその TextView を作成しようとしました。2 番目のアクティビティ用に xml に新しい TextView を作成し、次のような ID を付けました。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DisplayMessageActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:id="@+id/new_Text"/>
</LinearLayout>
2 番目のアクティビティの Java コードは次のとおりです。
package com.example.myfirstapp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.MenuItem;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
public class DisplayMessageActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = (TextView) findViewById(R.id.new_Text);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
// Show the Up button in the action bar.
// getActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
変更する前は、すべてのコードが正常に機能していました。私が変更され
TextView textView = new TextView(this);
に:
TextView textView = (TextView) findViewById(R.id.new_Text);
しかし、何らかの理由で new_Text が見つからず、Eclipse は main.xml からの ID のみを提案します。なぜそのようになっているのですか?R.id.blabla が main.xml からのみ ID を取得するためですか? では、レイアウトが main.xml からのものでない場合、Java コードを使用してレイアウトを作成する必要がありますか?