从另一个活动访问活动的控制

时间:2014-08-27 10:35:51

标签: android android-activity textview nullreferenceexception

你好,我想要做的是使用这个简单的代码访问控件

`contact_us_textView = (TextView)findViewById(R.id.contact_us_textView);`

然后在编译器到达上面提到的这行代码时更改此textView的值,变量contact_us_textview仍然为null,因此仍然无法访问没有引用的对象,这将导致Null引用异常..

<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.badrandesign.ContactUs" 
android:background="@drawable/gradient"
>

    <TextView
        android:id="@+id/contact_us_text_view"
        style="@style/TextViewStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="@string/Contact_Us_Text"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="25sp" 
        android:textAlignment="center"
        />

           <ImageButton
               android:id="@+id/CallImageButton_2"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_alignLeft="@+id/CallImageButton_1"
               android:contentDescription="Call"
               android:src="@drawable/ic_action_call"
               android:layout_marginTop="270dp"
               tools:ignore="HardCodedText" 
               android:onClick="callSecondNumber"

               />

           <ImageView
               android:id="@+id/ViberImageView_2"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_alignLeft="@+id/ViberImageView_1"
               android:layout_below="@+id/Phone_Number_2"
               android:contentDescription="Viber"
               android:src="@drawable/ic_viber"
               tools:ignore="HardCodedText" />


           <ImageView
               android:id="@+id/WhatsappIcon"
               android:layout_width="80dp"
               android:layout_height="80dp"
               android:layout_alignBottom="@+id/ViberImageView_2"
               android:layout_alignLeft="@+id/WhatsappIcon2"
               android:layout_marginBottom="30dp"
               android:contentDescription="WhatsappIcon"
               android:src="@drawable/ic_whatsapp"
               tools:ignore="HardcodedText" 
               />

           <ImageView
               android:id="@+id/ViberImageView_1"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_alignRight="@+id/WhatsappIcon2"
               android:layout_below="@+id/CallImageButton_1"
               android:layout_marginRight="14dp"
               android:contentDescription="Viber"
               android:src="@drawable/ic_viber"
               tools:ignore="HardCodedText"/>

           <ImageButton
               android:id="@+id/CallImageButton_1"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_below="@+id/contact_us_text_view"
               android:layout_toRightOf="@+id/Phone_Number_1"
               android:contentDescription="Call"
               android:onClick="callFirstNumber"
               android:src="@drawable/ic_action_call"
               tools:ignore="HardCodedText" />

           <TextView
               android:id="@+id/Phone_Number_2"
               style="@style/TextViewStyle"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_alignTop="@+id/CallImageButton_2"
               android:layout_toLeftOf="@+id/CallImageButton_2"
               android:text="70 605 051"
               android:textAppearance="?android:attr/textAppearanceLarge"
               android:textSize="25sp"
               tools:ignore="HardcodedText" />

           <TextView
               android:id="@+id/Phone_Number_1"
               style="@style/TextViewStyle"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_above="@+id/ViberImageView_1"
               android:layout_alignParentLeft="true"
               android:layout_marginLeft="15dp"
               android:text="70 605 051"
               android:textAppearance="?android:attr/textAppearanceLarge"
               android:textSize="25sp"
               tools:ignore="HardcodedText" />

           <ImageView
               android:id="@+id/WhatsappIcon2"
               android:layout_width="80dp"
               android:layout_height="80dp"
               android:layout_above="@+id/ViberImageView_1"
               android:layout_toRightOf="@+id/CallImageButton_1"
               android:contentDescription="WhatsappIcon"
               android:src="@drawable/ic_whatsapp"
               tools:ignore="HardcodedText" />

0 个答案:

没有答案