Здравствуйте! Есть непонимание работы из кода с разметкой.Прошу пояснить. Создал разметку main и SelectTemplate . Подключил их. Однако, не могу получить доступ к элементам SelectTemplate, в то время как к элементам main доступ есть. То есть, определив элемент ImageView в xml как android:id="@+id/Template1ImageMain" ,я не могу вызвать его как myImageView_Template1_Image_Main = (ImageView)findViewById(R.id.Template1ImageMain); (select_template.xml) В тоже время доступ к ,допустим, button1 (main.xml) у меня есть. Помогите пожалуйста решить эту проблему и понять где я ошибся. Спасибо. Код прилагается ниже. MainActivity.Java | Код |
import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView;
public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button bt1= (Button) this.findViewById(R.id.button1); bt1.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub //Intent intent=getIntent(); setContentView(R.layout.select_template); //setContentView(R.layout.gridlayout); //загружаем картинки из файлов ресурсов. ///////////// // Button myButton; ImageView myImageView_Template1_Image_Main; ImageView myImageView_Template1_Image_Left; ImageView myImageView_Template1_Image_Right; final int GALLERY_REQUEST = 1; myImageView_Template1_Image_Main = (ImageView)findViewById(R.id.Template1ImageMain); // myImageView_Template1_Image_Left = (ImageView)findViewById(R.id.Template1ImageMain); // myImageView_Template1_Image_Right = (ImageView)findViewById(R.id.Template1ImageMain); } }); } }
|
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:gravity="center" android:orientation="vertical" >
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" />
<Button android:id="@+id/button1" android:layout_width="251dp" android:layout_height="wrap_content" android:clickable="true" android:text="Создать Фотокнигу" />
<Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" />
</LinearLayout>
|
Select__Template.xml | Код |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#0000aa" android:orientation="vertical" >
<TextView android:layout_width="wrap_content" android:layout_height="57dp" android:gravity="center_horizontal" android:text="@string/SelectTamplate" android:textSize="15pt" />
<ImageView android:id="@+id/Template1ImageMain" android:layout_width="match_parent" android:contentDescription="@string/app_name" android:layout_height="wrap_content" /> <ImageView android:contentDescription="@string/desc" android:id="@+id/Template1_Image_Left" android:layout_width="match_parent" android:layout_height="wrap_content" /> <ImageView android:contentDescription="@string/desc" android:id="@+id/Template1_Image_Right" android:layout_width="match_parent" android:layout_height="wrap_content" />
</LinearLayout>
|
|